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