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