| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.print; |
| 18 |
|
| 19 |
import android.annotation.IntRange; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.os.Parcel; |
| 23 |
import android.os.Parcelable; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public final class PageRange implements Parcelable { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public static final PageRange ALL_PAGES = new PageRange(0, Integer.MAX_VALUE); |
| 35 |
|
| 36 |
|
| 37 |
public static final PageRange[] ALL_PAGES_ARRAY = new PageRange[]{PageRange.ALL_PAGES}; |
| 38 |
|
| 39 |
private final int mStart; |
| 40 |
private final int mEnd; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public PageRange(@IntRange(from = 0) int start, @IntRange(from = 0) int end) { |
| 52 |
if (start < 0) { |
| 53 |
throw new IllegalArgumentException("start cannot be less than zero."); |
| 54 |
} |
| 55 |
if (end < 0) { |
| 56 |
throw new IllegalArgumentException("end cannot be less than zero."); |
| 57 |
} |
| 58 |
if (start > end) { |
| 59 |
throw new IllegalArgumentException("start must be lesser than end."); |
| 60 |
} |
| 61 |
mStart = start; |
| 62 |
mEnd = end; |
| 63 |
} |
| 64 |
|
| 65 |
private PageRange(@NonNull Parcel parcel) { |
| 66 |
this(parcel.readInt(), parcel.readInt()); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public @IntRange(from = 0) int getStart() { |
| 75 |
return mStart; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public @IntRange(from = 0) int getEnd() { |
| 84 |
return mEnd; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public boolean contains(int pageIndex) { |
| 96 |
return (pageIndex >= mStart) && (pageIndex <= mEnd); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public int getSize() { |
| 108 |
return mEnd - mStart + 1; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public int describeContents() { |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void writeToParcel(Parcel parcel, int flags) { |
| 118 |
parcel.writeInt(mStart); |
| 119 |
parcel.writeInt(mEnd); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public int hashCode() { |
| 124 |
final int prime = 31; |
| 125 |
int result = 1; |
| 126 |
result = prime * result + mEnd; |
| 127 |
result = prime * result + mStart; |
| 128 |
return result; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public boolean equals(@Nullable Object obj) { |
| 133 |
if (this == obj) { |
| 134 |
return true; |
| 135 |
} |
| 136 |
if (obj == null) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
if (getClass() != obj.getClass()) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
PageRange other = (PageRange) obj; |
| 143 |
if (mEnd != other.mEnd) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
if (mStart != other.mStart) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public String toString() { |
| 154 |
if (mStart == 0 && mEnd == Integer.MAX_VALUE) { |
| 155 |
return "PageRange[<all pages>]"; |
| 156 |
} |
| 157 |
StringBuilder builder = new StringBuilder(); |
| 158 |
builder.append("PageRange[") |
| 159 |
.append(mStart) |
| 160 |
.append(" - ") |
| 161 |
.append(mEnd) |
| 162 |
.append("]"); |
| 163 |
return builder.toString(); |
| 164 |
} |
| 165 |
|
| 166 |
public static final @android.annotation.NonNull Parcelable.Creator<PageRange> CREATOR = |
| 167 |
new Creator<PageRange>() { |
| 168 |
@Override |
| 169 |
public PageRange createFromParcel(Parcel parcel) { |
| 170 |
return new PageRange(parcel); |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public PageRange[] newArray(int size) { |
| 175 |
return new PageRange[size]; |
| 176 |
} |
| 177 |
}; |
| 178 |
} |