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