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.annotation.IntRange; 20 import android.annotation.NonNull; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 /** 25 * Represents a range of pages. The start and end page indices of 26 * the range are zero based and inclusive. 27 */ 28 public final class PageRange implements Parcelable { 29 30 /** 31 * Constant for specifying all pages. 32 */ 33 public static final PageRange ALL_PAGES = new PageRange(0, Integer.MAX_VALUE); 34 35 /** @hide */ 36 public static final PageRange[] ALL_PAGES_ARRAY = new PageRange[]{PageRange.ALL_PAGES}; 37 38 private final int mStart; 39 private final int mEnd; 40 41 /** 42 * Creates a new instance. 43 * 44 * @param start The start page index (zero based and inclusive). 45 * @param end The end page index (zero based and inclusive). 46 * 47 * @throws IllegalArgumentException If start is less than zero or end 48 * is less than zero or start greater than end. 49 */ PageRange(@ntRangefrom = 0) int start, @IntRange(from = 0) int end)50 public PageRange(@IntRange(from = 0) int start, @IntRange(from = 0) int end) { 51 if (start < 0) { 52 throw new IllegalArgumentException("start cannot be less than zero."); 53 } 54 if (end < 0) { 55 throw new IllegalArgumentException("end cannot be less than zero."); 56 } 57 if (start > end) { 58 throw new IllegalArgumentException("start must be lesser than end."); 59 } 60 mStart = start; 61 mEnd = end; 62 } 63 PageRange(@onNull Parcel parcel)64 private PageRange(@NonNull Parcel parcel) { 65 this(parcel.readInt(), parcel.readInt()); 66 } 67 68 /** 69 * Gets the start page index (zero based and inclusive). 70 * 71 * @return The start page index. 72 */ getStart()73 public @IntRange(from = 0) int getStart() { 74 return mStart; 75 } 76 77 /** 78 * Gets the end page index (zero based and inclusive). 79 * 80 * @return The end page index. 81 */ getEnd()82 public @IntRange(from = 0) int getEnd() { 83 return mEnd; 84 } 85 86 /** 87 * Gets whether a page range contains a a given page. 88 * 89 * @param pageIndex The page index. 90 * @return True if the page is within this range. 91 * 92 * @hide 93 */ contains(int pageIndex)94 public boolean contains(int pageIndex) { 95 return (pageIndex >= mStart) && (pageIndex <= mEnd); 96 } 97 98 /** 99 * Get the size of this range which is the number of 100 * pages it contains. 101 * 102 * @return The size of the range. 103 * 104 * @hide 105 */ getSize()106 public int getSize() { 107 return mEnd - mStart + 1; 108 } 109 110 @Override describeContents()111 public int describeContents() { 112 return 0; 113 } 114 115 @Override writeToParcel(Parcel parcel, int flags)116 public void writeToParcel(Parcel parcel, int flags) { 117 parcel.writeInt(mStart); 118 parcel.writeInt(mEnd); 119 } 120 121 @Override hashCode()122 public int hashCode() { 123 final int prime = 31; 124 int result = 1; 125 result = prime * result + mEnd; 126 result = prime * result + mStart; 127 return result; 128 } 129 130 @Override equals(Object obj)131 public boolean equals(Object obj) { 132 if (this == obj) { 133 return true; 134 } 135 if (obj == null) { 136 return false; 137 } 138 if (getClass() != obj.getClass()) { 139 return false; 140 } 141 PageRange other = (PageRange) obj; 142 if (mEnd != other.mEnd) { 143 return false; 144 } 145 if (mStart != other.mStart) { 146 return false; 147 } 148 return true; 149 } 150 151 @Override toString()152 public String toString() { 153 if (mStart == 0 && mEnd == Integer.MAX_VALUE) { 154 return "PageRange[<all pages>]"; 155 } 156 StringBuilder builder = new StringBuilder(); 157 builder.append("PageRange[") 158 .append(mStart) 159 .append(" - ") 160 .append(mEnd) 161 .append("]"); 162 return builder.toString(); 163 } 164 165 public static final @android.annotation.NonNull Parcelable.Creator<PageRange> CREATOR = 166 new Creator<PageRange>() { 167 @Override 168 public PageRange createFromParcel(Parcel parcel) { 169 return new PageRange(parcel); 170 } 171 172 @Override 173 public PageRange[] newArray(int size) { 174 return new PageRange[size]; 175 } 176 }; 177 } 178