1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.print; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 /** 23 * Represents a range of pages. The start and end page indices of 24 * the range are zero based and inclusive. 25 */ 26 public final class PageRange implements Parcelable { 27 28 /** 29 * Constant for specifying all pages. 30 */ 31 public static final PageRange ALL_PAGES = new PageRange(0, Integer.MAX_VALUE); 32 33 private final int mStart; 34 private final int mEnd; 35 36 /** 37 * Creates a new instance. 38 * 39 * @param start The start page index (zero based and inclusive). 40 * @param end The end page index (zero based and inclusive). 41 * 42 * @throws IllegalArgumentException If start is less than zero or end 43 * is less than zero or start greater than end. 44 */ PageRange(int start, int end)45 public PageRange(int start, int end) { 46 if (start < 0) { 47 throw new IllegalArgumentException("start cannot be less than zero."); 48 } 49 if (end < 0) { 50 throw new IllegalArgumentException("end cannot be less than zero."); 51 } 52 if (start > end) { 53 throw new IllegalArgumentException("start must be lesser than end."); 54 } 55 mStart = start; 56 mEnd = end; 57 } 58 PageRange(Parcel parcel)59 private PageRange (Parcel parcel) { 60 this(parcel.readInt(), parcel.readInt()); 61 } 62 63 /** 64 * Gets the start page index (zero based and inclusive). 65 * 66 * @return The start page index. 67 */ getStart()68 public int getStart() { 69 return mStart; 70 } 71 72 /** 73 * Gets the end page index (zero based and inclusive). 74 * 75 * @return The end page index. 76 */ getEnd()77 public int getEnd() { 78 return mEnd; 79 } 80 81 /** 82 * Gets whether a page range contains a a given page. 83 * 84 * @param pageIndex The page index. 85 * @return True if the page is within this range. 86 * 87 * @hide 88 */ contains(int pageIndex)89 public boolean contains(int pageIndex) { 90 return (pageIndex >= mStart) && (pageIndex <= mEnd); 91 } 92 93 /** 94 * Get the size of this range which is the number of 95 * pages it contains. 96 * 97 * @return The size of the range. 98 * 99 * @hide 100 */ getSize()101 public int getSize() { 102 return mEnd - mStart + 1; 103 } 104 105 @Override describeContents()106 public int describeContents() { 107 return 0; 108 } 109 110 @Override writeToParcel(Parcel parcel, int flags)111 public void writeToParcel(Parcel parcel, int flags) { 112 parcel.writeInt(mStart); 113 parcel.writeInt(mEnd); 114 } 115 116 @Override hashCode()117 public int hashCode() { 118 final int prime = 31; 119 int result = 1; 120 result = prime * result + mEnd; 121 result = prime * result + mStart; 122 return result; 123 } 124 125 @Override equals(Object obj)126 public boolean equals(Object obj) { 127 if (this == obj) { 128 return true; 129 } 130 if (obj == null) { 131 return false; 132 } 133 if (getClass() != obj.getClass()) { 134 return false; 135 } 136 PageRange other = (PageRange) obj; 137 if (mEnd != other.mEnd) { 138 return false; 139 } 140 if (mStart != other.mStart) { 141 return false; 142 } 143 return true; 144 } 145 146 @Override toString()147 public String toString() { 148 if (mStart == 0 && mEnd == Integer.MAX_VALUE) { 149 return "PageRange[<all pages>]"; 150 } 151 StringBuilder builder = new StringBuilder(); 152 builder.append("PageRange[") 153 .append(mStart) 154 .append(" - ") 155 .append(mEnd) 156 .append("]"); 157 return builder.toString(); 158 } 159 160 public static final Parcelable.Creator<PageRange> CREATOR = 161 new Creator<PageRange>() { 162 @Override 163 public PageRange createFromParcel(Parcel parcel) { 164 return new PageRange(parcel); 165 } 166 167 @Override 168 public PageRange[] newArray(int size) { 169 return new PageRange[size]; 170 } 171 }; 172 } 173