blob: 57c7718c33706c8432936416b8dc9a0ba4f9177f [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
35 private final int mStart;
36 private final int mEnd;
37
38 /**
39 * Creates a new instance.
40 *
41 * @param start The start page index (zero based and inclusive).
42 * @param end The end page index (zero based and inclusive).
43 *
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070044 * @throws IllegalArgumentException If start is less than zero or end
45 * is less than zero or start greater than end.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070046 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080047 public PageRange(@IntRange(from = 0) int start, @IntRange(from = 0) int end) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070048 if (start < 0) {
49 throw new IllegalArgumentException("start cannot be less than zero.");
50 }
51 if (end < 0) {
52 throw new IllegalArgumentException("end cannot be less than zero.");
53 }
54 if (start > end) {
55 throw new IllegalArgumentException("start must be lesser than end.");
56 }
57 mStart = start;
58 mEnd = end;
59 }
60
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080061 private PageRange(@NonNull Parcel parcel) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070062 this(parcel.readInt(), parcel.readInt());
63 }
64
65 /**
66 * Gets the start page index (zero based and inclusive).
67 *
68 * @return The start page index.
69 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080070 public @IntRange(from = 0) int getStart() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070071 return mStart;
72 }
73
74 /**
75 * Gets the end page index (zero based and inclusive).
76 *
77 * @return The end page index.
78 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080079 public @IntRange(from = 0) int getEnd() {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070080 return mEnd;
81 }
82
Svet Ganov525a66b2014-06-14 22:29:00 -070083 /**
84 * Gets whether a page range contains a a given page.
85 *
86 * @param pageIndex The page index.
87 * @return True if the page is within this range.
88 *
89 * @hide
90 */
91 public boolean contains(int pageIndex) {
Svetoslav15cbc8a2014-07-11 09:45:07 -070092 return (pageIndex >= mStart) && (pageIndex <= mEnd);
Svet Ganov525a66b2014-06-14 22:29:00 -070093 }
94
95 /**
96 * Get the size of this range which is the number of
97 * pages it contains.
98 *
99 * @return The size of the range.
100 *
101 * @hide
102 */
103 public int getSize() {
104 return mEnd - mStart + 1;
105 }
106
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700107 @Override
108 public int describeContents() {
109 return 0;
110 }
111
112 @Override
113 public void writeToParcel(Parcel parcel, int flags) {
114 parcel.writeInt(mStart);
115 parcel.writeInt(mEnd);
116 }
117
118 @Override
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700119 public int hashCode() {
120 final int prime = 31;
121 int result = 1;
122 result = prime * result + mEnd;
123 result = prime * result + mStart;
124 return result;
125 }
126
127 @Override
128 public boolean equals(Object obj) {
129 if (this == obj) {
130 return true;
131 }
132 if (obj == null) {
133 return false;
134 }
135 if (getClass() != obj.getClass()) {
136 return false;
137 }
138 PageRange other = (PageRange) obj;
139 if (mEnd != other.mEnd) {
140 return false;
141 }
142 if (mStart != other.mStart) {
143 return false;
144 }
145 return true;
146 }
147
148 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700149 public String toString() {
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700150 if (mStart == 0 && mEnd == Integer.MAX_VALUE) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700151 return "PageRange[<all pages>]";
152 }
153 StringBuilder builder = new StringBuilder();
154 builder.append("PageRange[")
155 .append(mStart)
156 .append(" - ")
157 .append(mEnd)
158 .append("]");
159 return builder.toString();
160 }
161
162 public static final Parcelable.Creator<PageRange> CREATOR =
163 new Creator<PageRange>() {
164 @Override
165 public PageRange createFromParcel(Parcel parcel) {
166 return new PageRange(parcel);
167 }
168
169 @Override
170 public PageRange[] newArray(int size) {
171 return new PageRange[size];
172 }
173 };
174}