Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 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 | |
| 17 | package android.print; |
| 18 | |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 19 | import android.annotation.IntRange; |
| 20 | import android.annotation.NonNull; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | |
| 24 | /** |
| 25 | * Represents a range of pages. The start and end page indices of |
Svetoslav | fd90651 | 2013-06-24 09:04:48 -0700 | [diff] [blame] | 26 | * the range are zero based and inclusive. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 27 | */ |
| 28 | public 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. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 35 | /** @hide */ |
| 36 | public static final PageRange[] ALL_PAGES_ARRAY = new PageRange[]{PageRange.ALL_PAGES}; |
| 37 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 38 | 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 Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 47 | * @throws IllegalArgumentException If start is less than zero or end |
| 48 | * is less than zero or start greater than end. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 49 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 50 | public PageRange(@IntRange(from = 0) int start, @IntRange(from = 0) int end) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 51 | 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. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 64 | private PageRange(@NonNull Parcel parcel) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 65 | 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. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 73 | public @IntRange(from = 0) int getStart() { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 74 | 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. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 82 | public @IntRange(from = 0) int getEnd() { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 83 | return mEnd; |
| 84 | } |
| 85 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 86 | /** |
| 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) { |
Svetoslav | 15cbc8a | 2014-07-11 09:45:07 -0700 | [diff] [blame] | 95 | return (pageIndex >= mStart) && (pageIndex <= mEnd); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 96 | } |
| 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 Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 110 | @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 Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 122 | 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 Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 152 | public String toString() { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 153 | if (mStart == 0 && mEnd == Integer.MAX_VALUE) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 154 | 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 | } |