PageRange.java revision aec1417ca9eb63209668ac17da90cf8a07c6076c
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 17package android.print; 18 19import android.os.Parcel; 20import 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 */ 26public 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. 43 * @throws IllegalArgumentException If end is less than zero. 44 * @throws IllegalArgumentException If start greater than end. 45 */ 46 public PageRange(int start, int end) { 47 if (start < 0) { 48 throw new IllegalArgumentException("start cannot be less than zero."); 49 } 50 if (end < 0) { 51 throw new IllegalArgumentException("end cannot be less than zero."); 52 } 53 if (start > end) { 54 throw new IllegalArgumentException("start must be lesser than end."); 55 } 56 mStart = start; 57 mEnd = end; 58 } 59 60 private PageRange (Parcel parcel) { 61 this(parcel.readInt(), parcel.readInt()); 62 } 63 64 /** 65 * Gets the start page index (zero based and inclusive). 66 * 67 * @return The start page index. 68 */ 69 public int getStart() { 70 return mStart; 71 } 72 73 /** 74 * Gets the end page index (zero based and inclusive). 75 * 76 * @return The end page index. 77 */ 78 public int getEnd() { 79 return mEnd; 80 } 81 82 @Override 83 public int describeContents() { 84 return 0; 85 } 86 87 @Override 88 public void writeToParcel(Parcel parcel, int flags) { 89 parcel.writeInt(mStart); 90 parcel.writeInt(mEnd); 91 } 92 93 @Override 94 public int hashCode() { 95 final int prime = 31; 96 int result = 1; 97 result = prime * result + mEnd; 98 result = prime * result + mStart; 99 return result; 100 } 101 102 @Override 103 public boolean equals(Object obj) { 104 if (this == obj) { 105 return true; 106 } 107 if (obj == null) { 108 return false; 109 } 110 if (getClass() != obj.getClass()) { 111 return false; 112 } 113 PageRange other = (PageRange) obj; 114 if (mEnd != other.mEnd) { 115 return false; 116 } 117 if (mStart != other.mStart) { 118 return false; 119 } 120 return true; 121 } 122 123 @Override 124 public String toString() { 125 if (mStart == 0 && mEnd == Integer.MAX_VALUE) { 126 return "PageRange[<all pages>]"; 127 } 128 StringBuilder builder = new StringBuilder(); 129 builder.append("PageRange[") 130 .append(mStart) 131 .append(" - ") 132 .append(mEnd) 133 .append("]"); 134 return builder.toString(); 135 } 136 137 public static final Parcelable.Creator<PageRange> CREATOR = 138 new Creator<PageRange>() { 139 @Override 140 public PageRange createFromParcel(Parcel parcel) { 141 return new PageRange(parcel); 142 } 143 144 @Override 145 public PageRange[] newArray(int size) { 146 return new PageRange[size]; 147 } 148 }; 149} 150