blob: 294128345658ed4fbdf28c9d0d27a037fb00652b [file] [log] [blame]
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -07001/*
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
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080019import android.annotation.IntRange;
20import android.annotation.NonNull;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070021import android.os.Parcel;
22import android.os.Parcelable;
23
24/**
25 * Represents a range of pages. The start and end page indices of
Svetoslavfd906512013-06-24 09:04:48 -070026 * the range are zero based and inclusive.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070027 */
28public 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
Philip P. Moltmann4ef83c462016-03-24 15:27:45 -070035 /** @hide */
36 public static final PageRange[] ALL_PAGES_ARRAY = new PageRange[]{PageRange.ALL_PAGES};
37
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070038 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 *
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070047 * @throws IllegalArgumentException If start is less than zero or end
48 * is less than zero or start greater than end.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070049 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080050 public PageRange(@IntRange(from = 0) int start, @IntRange(from = 0) int end) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070051 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
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080064 private PageRange(@NonNull Parcel parcel) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070065 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 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080073 public @IntRange(from = 0) int getStart() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070074 return mStart;
75 }
76
77 /**
78 * Gets the end page index (zero based and inclusive).
79 *
80 * @return The end page index.
81 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080082 public @IntRange(from = 0) int getEnd() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070083 return mEnd;
84 }
85
Svet Ganov525a66b2014-06-14 22:29:00 -070086 /**
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 */
94 public boolean contains(int pageIndex) {
Svetoslav15cbc8a2014-07-11 09:45:07 -070095 return (pageIndex >= mStart) && (pageIndex <= mEnd);
Svet Ganov525a66b2014-06-14 22:29:00 -070096 }
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 */
106 public int getSize() {
107 return mEnd - mStart + 1;
108 }
109
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700110 @Override
111 public int describeContents() {
112 return 0;
113 }
114
115 @Override
116 public void writeToParcel(Parcel parcel, int flags) {
117 parcel.writeInt(mStart);
118 parcel.writeInt(mEnd);
119 }
120
121 @Override
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700122 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
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
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700152 public String toString() {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700153 if (mStart == 0 && mEnd == Integer.MAX_VALUE) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700154 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 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}