Path: blob/master/src/java.desktop/share/classes/javax/print/attribute/standard/PageRanges.java
41171 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.print.attribute.standard;2627import java.io.Serial;2829import javax.print.attribute.Attribute;30import javax.print.attribute.DocAttribute;31import javax.print.attribute.PrintJobAttribute;32import javax.print.attribute.PrintRequestAttribute;33import javax.print.attribute.SetOfIntegerSyntax;3435/**36* Class {@code PageRanges} is a printing attribute class, a set of integers,37* that identifies the range(s) of print-stream pages that the Printer object38* uses for each copy of each document which are to be printed. Nothing is39* printed for any pages identified that do not exist in the document(s). The40* attribute is associated with <i>print-stream</i> pages, not41* application-numbered pages (for example, the page numbers found in the42* headers and or footers for certain word processing applications).43* <p>44* In most cases, the exact pages to be printed will be generated by a device45* driver and this attribute would not be required. However, when printing an46* archived document which has already been formatted, the end user may elect to47* print just a subset of the pages contained in the document. In this case, if48* a page range of <code>"<i>n</i>-<i>m</i>"</code> is specified, the first page49* to be printed will be page <i>n.</i> All subsequent pages of the document50* will be printed through and including page <i>m.</i>51* <p>52* If a {@code PageRanges} attribute is not specified for a print job, all pages53* of the document will be printed. In other words, the default value for the54* {@code PageRanges} attribute is always {@code {{1, Integer.MAX_VALUE}}}.55* <p>56* The effect of a {@code PageRanges} attribute on a multidoc print job (a job57* with multiple documents) depends on whether all the docs have the same page58* ranges specified or whether different docs have different page ranges59* specified, and on the (perhaps defaulted) value of the60* {@link MultipleDocumentHandling MultipleDocumentHandling} attribute.61* <ul>62* <li>If all the docs have the same page ranges specified, then any value of63* {@link MultipleDocumentHandling MultipleDocumentHandling} makes sense, and64* the printer's processing depends on the65* {@link MultipleDocumentHandling MultipleDocumentHandling} value:66* <ul>67* <li>{@code SINGLE_DOCUMENT} -- All the input docs will be combined68* together into one output document. The specified page ranges of that69* output document will be printed.70* <li>{@code SINGLE_DOCUMENT_NEW_SHEET} -- All the input docs will be71* combined together into one output document, and the first impression of72* each input doc will always start on a new media sheet. The specified page73* ranges of that output document will be printed.74* <li>{@code SEPARATE_DOCUMENTS_UNCOLLATED_COPIES} -- For each separate75* input doc, the specified page ranges will be printed.76* <li>{@code SEPARATE_DOCUMENTS_COLLATED_COPIES} -- For each separate input77* doc, the specified page ranges will be printed.78* </ul>79* <ul>80* <li>{@code SEPARATE_DOCUMENTS_UNCOLLATED_COPIES} -- For each separate81* input doc, its own specified page ranges will be printed.82* <li>{@code SEPARATE_DOCUMENTS_COLLATED_COPIES} -- For each separate input83* doc, its own specified page ranges will be printed.84* </ul>85* </ul>86* <p>87* <b>IPP Compatibility:</b> The PageRanges attribute's canonical array form88* gives the lower and upper bound for each range of pages to be included in and89* IPP "page-ranges" attribute. See class90* {@link SetOfIntegerSyntax SetOfIntegerSyntax} for an explanation of canonical91* array form. The category name returned by {@code getName()} gives the IPP92* attribute name.93*94* @author David Mendenhall95* @author Alan Kaminsky96*/97public final class PageRanges extends SetOfIntegerSyntax98implements DocAttribute, PrintRequestAttribute, PrintJobAttribute {99100/**101* Use serialVersionUID from JDK 1.4 for interoperability.102*/103@Serial104private static final long serialVersionUID = 8639895197656148392L;105106/**107* Construct a new page ranges attribute with the given members. The members108* are specified in "array form;" see class109* {@link SetOfIntegerSyntax SetOfIntegerSyntax} for an explanation of array110* form.111*112* @param members set members in array form113* @throws NullPointerException if {@code members} is {@code null} or any114* element of {@code members} is {@code null}115* @throws IllegalArgumentException if any element of {@code members} is not116* a length-one or length-two array. Also if {@code members} is a117* zero-length array or if any member of the set is less than 1.118*/119public PageRanges(int[][] members) {120super (members);121if (members == null) {122throw new NullPointerException("members is null");123}124myPageRanges();125}126127/**128* Construct a new page ranges attribute with the given members in string129* form. See class {@link SetOfIntegerSyntax SetOfIntegerSyntax} for130* explanation of the syntax.131*132* @param members set members in string form133* @throws NullPointerException if {@code members} is {@code null} or any134* element of {@code members} is {@code null}135* @throws IllegalArgumentException if {@code members} does not obey the136* proper syntax. Also if the constructed set-of-integer is a137* zero-length array or if any member of the set is less than 1.138*/139public PageRanges(String members) {140super(members);141if (members == null) {142throw new NullPointerException("members is null");143}144myPageRanges();145}146147/**148* Validates the page ranges.149*/150private void myPageRanges() {151int[][] myMembers = getMembers();152int n = myMembers.length;153if (n == 0) {154throw new IllegalArgumentException("members is zero-length");155}156int i;157for (i = 0; i < n; ++ i) {158if (myMembers[i][0] < 1) {159throw new IllegalArgumentException("Page value < 1 specified");160}161}162}163164/**165* Construct a new page ranges attribute containing a single integer. That166* is, only the one page is to be printed.167*168* @param member set member169* @throws IllegalArgumentException if {@code member < 1}170*/171public PageRanges(int member) {172super (member);173if (member < 1) {174throw new IllegalArgumentException("Page value < 1 specified");175}176}177178/**179* Construct a new page ranges attribute containing a single range of180* integers. That is, only those pages in the one range are to be printed.181*182* @param lowerBound lower bound of the range183* @param upperBound upper bound of the range184* @throws IllegalArgumentException if a {@code null} range is specified or185* if a {@code non-null} range is specified with {@code lowerBound}186* less than 1187*/188public PageRanges(int lowerBound, int upperBound) {189super (lowerBound, upperBound);190if (lowerBound > upperBound) {191throw new IllegalArgumentException("Null range specified");192} else if (lowerBound < 1) {193throw new IllegalArgumentException("Page value < 1 specified");194}195}196197/**198* Returns whether this page ranges attribute is equivalent to the passed in199* object. To be equivalent, all of the following conditions must be true:200* <ol type=1>201* <li>{@code object} is not {@code null}.202* <li>{@code object} is an instance of class {@code PageRanges}.203* <li>This page ranges attribute's members and {@code object}'s members204* are the same.205* </ol>206*207* @param object {@code Object} to compare to208* @return {@code true} if {@code object} is equivalent to this page ranges209* attribute, {@code false} otherwise210*/211public boolean equals(Object object) {212return (super.equals(object) && object instanceof PageRanges);213}214215/**216* Get the printing attribute class which is to be used as the "category"217* for this printing attribute value.218* <p>219* For class {@code PageRanges}, the category is class220* {@code PageRanges} itself.221*222* @return printing attribute class (category), an instance of class223* {@link Class java.lang.Class}224*/225public final Class<? extends Attribute> getCategory() {226return PageRanges.class;227}228229/**230* Get the name of the category of which this attribute value is an231* instance.232* <p>233* For class {@code PageRanges}, the category name is {@code "page-ranges"}.234*235* @return attribute category name236*/237public final String getName() {238return "page-ranges";239}240}241242243