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