PageRange.java revision 4b9a4d16872bbb50712e007b419ac0b35ff1582d
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 are 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 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 String toString() { 95 if (this == ALL_PAGES) { 96 return "PageRange[<all pages>]"; 97 } 98 StringBuilder builder = new StringBuilder(); 99 builder.append("PageRange[") 100 .append(mStart) 101 .append(" - ") 102 .append(mEnd) 103 .append("]"); 104 return builder.toString(); 105 } 106 107 public static final Parcelable.Creator<PageRange> CREATOR = 108 new Creator<PageRange>() { 109 @Override 110 public PageRange createFromParcel(Parcel parcel) { 111 return new PageRange(parcel); 112 } 113 114 @Override 115 public PageRange[] newArray(int size) { 116 return new PageRange[size]; 117 } 118 }; 119} 120