1 /*
   2  * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.print.attribute.standard;
  27 
  28 import javax.print.attribute.Attribute;
  29 import javax.print.attribute.SetOfIntegerSyntax;
  30 import javax.print.attribute.SupportedValuesAttribute;
  31 
  32 /**
  33  * Class {@code NumberUpSupported} is a printing attribute class, a set of
  34  * integers, that gives the supported values for a {@link NumberUp NumberUp}
  35  * attribute.
  36  * <p>
  37  * <b>IPP Compatibility:</b> The NumberUpSupported attribute's canonical array
  38  * form gives the lower and upper bound for each range of number-up to be
  39  * included in an IPP "number-up-supported" attribute. See class
  40  * {@link SetOfIntegerSyntax SetOfIntegerSyntax} for an explanation of canonical
  41  * array form. The category name returned by {@code getName()} gives the IPP
  42  * attribute name.
  43  *
  44  * @author Alan Kaminsky
  45  */
  46 public final class NumberUpSupported    extends SetOfIntegerSyntax
  47         implements SupportedValuesAttribute {
  48 
  49     /**
  50      * Use serialVersionUID from JDK 1.4 for interoperability.
  51      */
  52     private static final long serialVersionUID = -1041573395759141805L;
  53 
  54     /**
  55      * Construct a new number up supported attribute with the given members. The
  56      * supported values for NumberUp are specified in "array form;" see class
  57      * {@link SetOfIntegerSyntax SetOfIntegerSyntax} for
  58      * an explanation of array form.
  59      *
  60      * @param  members set members in array form
  61      * @throws NullPointerException if {@code members} is {@code null} or any
  62      *         element of {@code members} is {@code null}
  63      * @throws IllegalArgumentException if any element of {@code members} is not
  64      *         a length-one or length-two array. Also if {@code members} is a
  65      *         zero-length array or if any member of the set is less than 1.
  66      */
  67     public NumberUpSupported(int[][] members) {
  68         super (members);
  69         if (members == null) {
  70             throw new NullPointerException("members is null");
  71         }
  72         int[][] myMembers = getMembers();
  73         int n = myMembers.length;
  74         if (n == 0) {
  75             throw new IllegalArgumentException("members is zero-length");
  76         }
  77         int i;
  78         for (i = 0; i < n; ++ i) {
  79             if (myMembers[i][0] < 1) {
  80                 throw new IllegalArgumentException
  81                     ("Number up value must be > 0");
  82             }
  83         }
  84     }
  85 
  86     /**
  87      * Construct a new number up supported attribute containing a single
  88      * integer. That is, only the one value of {@code NumberUp} is supported.
  89      *
  90      * @param  member set member
  91      * @throws IllegalArgumentException if {@code member < 1}
  92      */
  93     public NumberUpSupported(int member) {
  94         super (member);
  95         if (member < 1) {
  96             throw new IllegalArgumentException("Number up value must be > 0");
  97         }
  98     }
  99 
 100     /**
 101      * Construct a new number up supported attribute containing a single range
 102      * of integers. That is, only those values of {@code NumberUp} in the one
 103      * range are supported.
 104      *
 105      * @param  lowerBound lower bound of the range
 106      * @param  upperBound upper bound of the range
 107      * @throws IllegalArgumentException if a {@code null} range is specified or
 108      *         if a {@code non-null} range is specified with {@code lowerBound}
 109      *         less than 1
 110      */
 111     public NumberUpSupported(int lowerBound, int upperBound) {
 112         super (lowerBound, upperBound);
 113         if (lowerBound > upperBound) {
 114             throw new IllegalArgumentException("Null range specified");
 115         } else if (lowerBound < 1) {
 116             throw new IllegalArgumentException
 117                 ("Number up value must be > 0");
 118         }
 119     }
 120 
 121     /**
 122      * Returns whether this number up supported attribute is equivalent to the
 123      * passed in object. To be equivalent, all of the following conditions must
 124      * be true:
 125      * <ol type=1>
 126      *   <li>{@code object} is not {@code null}.
 127      *   <li>{@code object} is an instance of class {@code NumberUpSupported}.
 128      *   <li>This number up supported attribute's members and {@code object}'s
 129      *   members are the same.
 130      * </ol>
 131      *
 132      * @param  object {@code Object} to compare to
 133      * @return {@code true} if {@code object} is equivalent to this number up
 134      *         supported attribute, {@code false} otherwise
 135      */
 136     public boolean equals(Object object) {
 137         return (super.equals (object) &&
 138                 object instanceof NumberUpSupported);
 139     }
 140 
 141     /**
 142      * Get the printing attribute class which is to be used as the "category"
 143      * for this printing attribute value.
 144      * <p>
 145      * For class {@code NumberUpSupported}, the category is class
 146      * {@code NumberUpSupported} itself.
 147      *
 148      * @return printing attribute class (category), an instance of class
 149      *         {@link Class java.lang.Class}
 150      */
 151     public final Class<? extends Attribute> getCategory() {
 152         return NumberUpSupported.class;
 153     }
 154 
 155     /**
 156      * Get the name of the category of which this attribute value is an
 157      * instance.
 158      * <p>
 159      * For class {@code NumberUpSupported}, the category name is
 160      * {@code "number-up-supported"}.
 161      *
 162      * @return attribute category name
 163      */
 164     public final String getName() {
 165         return "number-up-supported";
 166     }
 167 }