2362N/A * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A * or visit www.oracle.com if you need additional information or have any 0N/A * Class SetOfIntegerSyntax is an abstract base class providing the common 0N/A * implementation of all attributes whose value is a set of nonnegative 0N/A * integers. This includes attributes whose value is a single range of integers 0N/A * and attributes whose value is a set of ranges of integers. 0N/A * You can construct an instance of SetOfIntegerSyntax by giving it in "string 0N/A * form." The string consists of zero or more comma-separated integer groups. 0N/A * Each integer group consists of either one integer, two integers separated by 0N/A * a hyphen (<CODE>-</CODE>), or two integers separated by a colon 0N/A * (<CODE>:</CODE>). Each integer consists of one or more decimal digits 0N/A * (<CODE>0</CODE> through <CODE>9</CODE>). Whitespace characters cannot 0N/A * appear within an integer but are otherwise ignored. For example: 0N/A * <CODE>""</CODE>, <CODE>"1"</CODE>, <CODE>"5-10"</CODE>, <CODE>"1:2, 0N/A * You can also construct an instance of SetOfIntegerSyntax by giving it in 0N/A * "array form." Array form consists of an array of zero or more integer groups 0N/A * where each integer group is a length-1 or length-2 array of 0N/A * <CODE>int</CODE>s; for example, <CODE>int[0][]</CODE>, 0N/A * <CODE>int[][]{{1}}</CODE>, <CODE>int[][]{{5,10}}</CODE>, 0N/A * <CODE>int[][]{{1,2},{4}}</CODE>. 0N/A * In both string form and array form, each successive integer group gives a 0N/A * range of integers to be included in the set. The first integer in each group 0N/A * gives the lower bound of the range; the second integer in each group gives 0N/A * the upper bound of the range; if there is only one integer in the group, the 0N/A * upper bound is the same as the lower bound. If the upper bound is less than 0N/A * the lower bound, it denotes a null range (no values). If the upper bound is 0N/A * equal to the lower bound, it denotes a range consisting of a single value. If 0N/A * the upper bound is greater than the lower bound, it denotes a range 0N/A * consisting of more than one value. The ranges may appear in any order and are 0N/A * allowed to overlap. The union of all the ranges gives the set's contents. 0N/A * Once a SetOfIntegerSyntax instance is constructed, its value is immutable. 0N/A * The SetOfIntegerSyntax object's value is actually stored in "<I>canonical</I> 0N/A * array form." This is the same as array form, except there are no null ranges; 0N/A * the members of the set are represented in as few ranges as possible (i.e., 0N/A * overlapping ranges are coalesced); the ranges appear in ascending order; and 0N/A * each range is always represented as a length-two array of <CODE>int</CODE>s 0N/A * in the form {lower bound, upper bound}. An empty set is represented as a 0N/A * zero-length array. 0N/A * Class 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 * This set's members in canonical array form. * Construct a new set-of-integer attribute with the given members in * @param members Set members in string form. If null, an empty set is * @exception IllegalArgumentException * (Unchecked exception) Thrown if <CODE>members</CODE> does not * obey the proper syntax. * Parse the given string, returning canonical array form. // Create vector to hold int[] elements, each element being one range // parsed out of members. // Run state machine over members. case 0:
// Before first integer in first group case 1:
// In first integer in a group }
else if (c ==
'-' || c ==
':') {
case 2:
// After first integer in a group else if (c ==
'-' || c ==
':') {
case 3:
// Before second integer in a group case 4:
// In second integer in a group case 5:
// After second integer in a group case 6:
// Before first integer in second or later group // Finish off the state machine. case 0:
// Before first integer in first group case 1:
// In first integer in a group case 2:
// After first integer in a group case 4:
// In second integer in a group case 5:
// After second integer in a group case 3:
// Before second integer in a group case 6:
// Before first integer in second or later group // Return canonical array form. * Accumulate the given range (lb .. ub) into the canonical array form * into the given vector of int[] objects. // Make sure range is non-null. // Stick range at the back of the vector. // Work towards the front of the vector to integrate the new range // with the existing ranges. // Get lower and upper bounds of the two ranges being compared. /* If the two ranges overlap or are adjacent, coalesce them. * The two ranges overlap if the larger lower bound is less * than or equal to the smaller upper bound. The two ranges * are adjacent if the larger lower bound is one greater * than the smaller upper bound. // The coalesced range is from the smaller lower bound to // the larger upper bound. /* If the two ranges don't overlap and aren't adjacent but * are out of order, swap them. /* If the two ranges don't overlap and aren't adjacent and * aren't out of order, we're done early. * Convert the given vector of int[] objects to canonical array form. * Construct a new set-of-integer attribute with the given members in * @param members Set members in array form. If null, an empty set is * @exception NullPointerException * (Unchecked exception) Thrown if any element of * <CODE>members</CODE> is null. * @exception IllegalArgumentException * (Unchecked exception) Thrown if any element of * <CODE>members</CODE> is not a length-one or length-two array or if * any non-null range in <CODE>members</CODE> has a lower bound less * Parse the given array form, returning canonical array form. // Create vector to hold int[] elements, each element being one range // parsed out of members. // Process all integer groups in members. for (
int i =
0; i < n; ++ i) {
// Get lower and upper bounds of the range. // Return canonical array form. * Construct a new set-of-integer attribute containing a single integer. * @param member Set member. * @exception IllegalArgumentException * (Unchecked exception) Thrown if <CODE>member</CODE> is less than * Construct a new set-of-integer attribute containing a single range of * integers. If the lower bound is greater than the upper bound (a null * range), an empty set is constructed. * @param lowerBound Lower bound of the range. * @param upperBound Upper bound of the range. * @exception IllegalArgumentException * (Unchecked exception) Thrown if the range is non-null and * <CODE>lowerBound</CODE> is less than zero. * Obtain this set-of-integer attribute's members in canonical array form. * The returned array is "safe;" the client may alter it without affecting * this set-of-integer attribute. * @return This set-of-integer attribute's members in canonical array form. int[][]
result =
new int[n][];
for (
int i =
0; i < n; ++ i) {
* Determine if this set-of-integer attribute contains the given value. * @param x Integer value. * @return True if this set-of-integer attribute contains the value * <CODE>x</CODE>, false otherwise. // Do a linear search to find the range that contains x, if any. for (
int i =
0; i < n; ++ i) {
* Determine if this set-of-integer attribute contains the given integer * @param attribute Integer attribute. * @return True if this set-of-integer attribute contains * <CODE>theAttribute</CODE>'s value, false otherwise. * Determine the smallest integer in this set-of-integer attribute that is * greater than the given value. If there are no integers in this * set-of-integer attribute greater than the given value, <CODE>-1</CODE> is * returned. (Since a set-of-integer attribute can only contain nonnegative * values, <CODE>-1</CODE> will never appear in the set.) You can use the * <CODE>next()</CODE> method to iterate through the integer values in a * set-of-integer attribute in ascending order, like this: * SetOfIntegerSyntax attribute = . . .; * while ((i = attribute.next (i)) != -1) * @param x Integer value. * @return The smallest integer in this set-of-integer attribute that is * greater than <CODE>x</CODE>, or <CODE>-1</CODE> if no integer in * this set-of-integer attribute is greater than <CODE>x</CODE>. // Do a linear search to find the range that contains x, if any. for (
int i =
0; i < n; ++ i) {
* Returns whether this set-of-integer attribute is equivalent to the passed * in object. To be equivalent, all of the following conditions must be * <CODE>object</CODE> is not null. * <CODE>object</CODE> is an instance of class SetOfIntegerSyntax. * This set-of-integer attribute's members and <CODE>object</CODE>'s * @param object Object to compare to. * @return True if <CODE>object</CODE> is equivalent to this * set-of-integer attribute, false otherwise. for (
int i =
0; i < m; ++ i) {
* 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. for (
int i =
0; i < n; ++ i) {
* 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 <CODE>"<I>i</I>"</CODE> if * the lower bound equals the upper bound or * <CODE>"<I>i</I>-<I>j</I>"</CODE> otherwise. for (
int i =
0; i < n; i++) {