/* * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.print.attribute; import java.io.Serializable; import java.util.Vector; /** * Class {@code SetOfIntegerSyntax} is an abstract base class providing the * common implementation of all attributes whose value is a set of nonnegative * integers. This includes attributes whose value is a single range of integers * and attributes whose value is a set of ranges of integers. *
* You can construct an instance of {@code SetOfIntegerSyntax} by giving it in * "string form." The string consists of zero or more comma-separated integer * groups. Each integer group consists of either one integer, two integers * separated by a hyphen ({@code -}), or two integers separated by a colon * ({@code :}). Each integer consists of one or more decimal digits ({@code 0} * through {@code 9}). Whitespace characters cannot appear within an integer but * are otherwise ignored. For example: {@code ""}, {@code "1"}, {@code "5-10"}, * {@code "1:2, 4"}. *
* You can also construct an instance of {@code SetOfIntegerSyntax} by giving it * in "array form." Array form consists of an array of zero or more integer * groups where each integer group is a length-1 or length-2 array of * {@code int}s; for example, {@code int[0][]}, {@code int[][]{{1}}}, * {@code int[][]{{5,10}}}, {@code int[][]{{1,2},{4}}}. *
* In both string form and array form, each successive integer group gives a * range of integers to be included in the set. The first integer in each group * gives the lower bound of the range; the second integer in each group gives * the upper bound of the range; if there is only one integer in the group, the * upper bound is the same as the lower bound. If the upper bound is less than * the lower bound, it denotes a {@code null} range (no values). If the upper * bound is equal to the lower bound, it denotes a range consisting of a single * value. If the upper bound is greater than the lower bound, it denotes a range * consisting of more than one value. The ranges may appear in any order and are * allowed to overlap. The union of all the ranges gives the set's contents. * Once a {@code SetOfIntegerSyntax} instance is constructed, its value is * immutable. *
* The {@code SetOfIntegerSyntax} object's value is actually stored in * "canonical array form." This is the same as array form, except there * are no {@code null} ranges; the members of the set are represented in as few * ranges as possible (i.e., overlapping ranges are coalesced); the ranges * appear in ascending order; and each range is always represented as a * length-two array of {@code int}s in the form {lower bound, upper bound}. An * empty set is represented as a zero-length array. *
* Class {@code SetOfIntegerSyntax} has operations to return the set's members
* in canonical array form, to test whether a given integer is a member of the
* set, and to iterate through the members of the set.
*
* @author David Mendenhall
* @author Alan Kaminsky
*/
public abstract class SetOfIntegerSyntax implements Serializable, Cloneable {
/**
* Use serialVersionUID from JDK 1.4 for interoperability.
*/
private static final long serialVersionUID = 3666874174847632203L;
/**
* This set's members in canonical array form.
*
* @serial
*/
private int[][] members;
/**
* Construct a new set-of-integer attribute with the given members in string
* form.
*
* @param members set members in string form. If {@code null}, an empty set
* is constructed.
* @throws IllegalArgumentException if {@code members} does not obey the
* proper syntax
*/
protected SetOfIntegerSyntax(String members) {
this.members = parse (members);
}
/**
* Parse the given string, returning canonical array form.
*
* @param members the string
* @return the canonical array form
*/
private static int[][] parse(String members) {
// Create vector to hold int[] elements, each element being one range
// parsed out of members.
Vector
* SetOfIntegerSyntax attribute = . . .;
* int i = -1;
* while ((i = attribute.next (i)) != -1)
* {
* foo (i);
* }
*
*
* @param x the Integer value
* @return the smallest integer in this set-of-integer attribute that is
* greater than {@code x}, or {@code -1} if no integer in this
* set-of-integer attribute is greater than {@code x}.
*/
public int next(int x) {
// Do a linear search to find the range that contains x, if any.
int n = members.length;
for (int i = 0; i < n; ++ i) {
if (x < members[i][0]) {
return members[i][0];
} else if (x < members[i][1]) {
return x + 1;
}
}
return -1;
}
/**
* Returns whether this set-of-integer attribute is equivalent to the passed
* in object. To be equivalent, all of the following conditions must be
* true:
*
*
*
* @param object {@code Object} to compare to
* @return {@code true} if {@code object} is equivalent to this
* set-of-integer attribute, {@code false} otherwise
*/
public boolean equals(Object object) {
if (object != null && object instanceof SetOfIntegerSyntax) {
int[][] myMembers = this.members;
int[][] otherMembers = ((SetOfIntegerSyntax) object).members;
int m = myMembers.length;
int n = otherMembers.length;
if (m == n) {
for (int i = 0; i < m; ++ i) {
if (myMembers[i][0] != otherMembers[i][0] ||
myMembers[i][1] != otherMembers[i][1]) {
return false;
}
}
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Returns a hash code value for this set-of-integer attribute. The hash
* code is the sum of the lower and upper bounds of the ranges in the
* canonical array form, or 0 for an empty set.
*/
public int hashCode() {
int result = 0;
int n = members.length;
for (int i = 0; i < n; ++ i) {
result += members[i][0] + members[i][1];
}
return result;
}
/**
* Returns a string value corresponding to this set-of-integer attribute.
* The string value is a zero-length string if this set is empty. Otherwise,
* the string value is a comma-separated list of the ranges in the canonical
* array form, where each range is represented as "i"
if
* the lower bound equals the upper bound or
* "i-j"
otherwise.
*/
public String toString() {
StringBuilder result = new StringBuilder();
int n = members.length;
for (int i = 0; i < n; i++) {
if (i > 0) {
result.append (',');
}
result.append (members[i][0]);
if (members[i][0] != members[i][1]) {
result.append ('-');
result.append (members[i][1]);
}
}
return result.toString();
}
}