PageRange.java revision c43639c3067dda5df189fb3cbf14f256c17e677d
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.annotation.IntRange;
20import android.annotation.NonNull;
21import android.os.Parcel;
22import android.os.Parcelable;
23
24/**
25 * Represents a range of pages. The start and end page indices of
26 * the range are zero based and inclusive.
27 */
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     *
44     * @throws IllegalArgumentException If start is less than zero or end
45     * is less than zero or start greater than end.
46     */
47    public PageRange(@IntRange(from = 0) int start, @IntRange(from = 0) int end) {
48        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
61    private PageRange(@NonNull Parcel parcel) {
62        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     */
70    public @IntRange(from = 0) int getStart() {
71        return mStart;
72    }
73
74    /**
75     * Gets the end page index (zero based and inclusive).
76     *
77     * @return The end page index.
78     */
79    public @IntRange(from = 0) int getEnd() {
80        return mEnd;
81    }
82
83    /**
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) {
92        return (pageIndex >= mStart) && (pageIndex <= mEnd);
93    }
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
107    @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
119    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
149    public String toString() {
150        if (mStart == 0 && mEnd == Integer.MAX_VALUE) {
151            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}
175