jdk/src/share/classes/javax/print/attribute/SetOfIntegerSyntax.java
author prr
Wed, 18 Jun 2014 13:14:15 -0700
changeset 25144 e2bf17cee34b
parent 25093 8ec5ec01740e
parent 24969 afa6934dd8e8
child 25770 d132697706ea
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.print.attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Class SetOfIntegerSyntax is an abstract base class providing the common
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * implementation of all attributes whose value is a set of nonnegative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * integers. This includes attributes whose value is a single range of integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and attributes whose value is a set of ranges of integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * You can construct an instance of SetOfIntegerSyntax by giving it in "string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * form." The string consists of zero or more comma-separated integer groups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Each integer group consists of either one integer, two integers separated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a hyphen (<CODE>-</CODE>), or two integers separated by a colon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (<CODE>:</CODE>). Each integer consists of one or more decimal digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * (<CODE>0</CODE> through <CODE>9</CODE>). Whitespace characters cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * appear within an integer but are otherwise ignored. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <CODE>""</CODE>, <CODE>"1"</CODE>, <CODE>"5-10"</CODE>, <CODE>"1:2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * 4"</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * You can also construct an instance of SetOfIntegerSyntax by giving it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * "array form." Array form consists of an array of zero or more integer groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * where each integer group is a length-1 or length-2 array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <CODE>int</CODE>s; for example, <CODE>int[0][]</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <CODE>int[][]{{1}}</CODE>, <CODE>int[][]{{5,10}}</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <CODE>int[][]{{1,2},{4}}</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * In both string form and array form, each successive integer group gives a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * range of integers to be included in the set. The first integer in each group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * gives the lower bound of the range; the second integer in each group gives
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the upper bound of the range; if there is only one integer in the group, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * upper bound is the same as the lower bound. If the upper bound is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * the lower bound, it denotes a null range (no values). If the upper bound is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * equal to the lower bound, it denotes a range consisting of a single value. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the upper bound is greater than the lower bound, it denotes a range
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * consisting of more than one value. The ranges may appear in any order and are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * allowed to overlap. The union of all the ranges gives the set's contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Once a SetOfIntegerSyntax instance is constructed, its value is immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * The SetOfIntegerSyntax object's value is actually stored in "<I>canonical</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * array form." This is the same as array form, except there are no null ranges;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * the members of the set are represented in as few ranges as possible (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * overlapping ranges are coalesced); the ranges appear in ascending order; and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * each range is always represented as a length-two array of <CODE>int</CODE>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * in the form {lower bound, upper bound}. An empty set is represented as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * zero-length array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Class SetOfIntegerSyntax has operations to return the set's members in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * canonical array form, to test whether a given integer is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * set, and to iterate through the members of the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @author  David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author  Alan Kaminsky
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public abstract class SetOfIntegerSyntax implements Serializable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final long serialVersionUID = 3666874174847632203L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * This set's members in canonical array form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private int[][] members;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Construct a new set-of-integer attribute with the given members in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * string form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param  members  Set members in string form. If null, an empty set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *                     constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @exception  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *     (Unchecked exception) Thrown if <CODE>members</CODE> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *    obey  the proper syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected SetOfIntegerSyntax(String members) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.members = parse (members);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Parse the given string, returning canonical array form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static int[][] parse(String members) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // Create vector to hold int[] elements, each element being one range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // parsed out of members.
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   115
        Vector<int[]> theRanges = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // Run state machine over members.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int n = (members == null ? 0 : members.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int lb = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int ub = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int digit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        while (i < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            c = members.charAt(i ++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            case 0: // Before first integer in first group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if (Character.isWhitespace(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                else if ((digit = Character.digit(c, 10)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    lb = digit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    state = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            case 1: // In first integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (Character.isWhitespace(c)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        state = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                } else if ((digit = Character.digit(c, 10)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    lb = 10 * lb + digit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    state = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                } else if (c == '-' || c == ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    state = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                } else if (c == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    accumulate (theRanges, lb, lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    state = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            case 2: // After first integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if (Character.isWhitespace(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    state = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                else if (c == '-' || c == ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    state = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                else if (c == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    accumulate(theRanges, lb, lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    state = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            case 3: // Before second integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (Character.isWhitespace(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    state = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                } else if ((digit = Character.digit(c, 10)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    ub = digit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    state = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            case 4: // In second integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                if (Character.isWhitespace(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    state = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                } else if ((digit = Character.digit(c, 10)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    ub = 10 * ub + digit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    state = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                } else if (c == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    accumulate(theRanges, lb, ub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    state = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            case 5: // After second integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (Character.isWhitespace(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    state = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                } else if (c == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    accumulate(theRanges, lb, ub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    state = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            case 6: // Before first integer in second or later group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (Character.isWhitespace(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    state = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                } else if ((digit = Character.digit(c, 10)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    lb = digit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    state = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // Finish off the state machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        case 0: // Before first integer in first group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        case 1: // In first integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        case 2: // After first integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            accumulate(theRanges, lb, lb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        case 4: // In second integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        case 5: // After second integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            accumulate(theRanges, lb, ub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        case 3: // Before second integer in a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        case 6: // Before first integer in second or later group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // Return canonical array form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return canonicalArrayForm (theRanges);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Accumulate the given range (lb .. ub) into the canonical array form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * into the given vector of int[] objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   246
    private static void accumulate(Vector<int[]> ranges, int lb,int ub) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // Make sure range is non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (lb <= ub) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // Stick range at the back of the vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            ranges.add(new int[] {lb, ub});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // Work towards the front of the vector to integrate the new range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // with the existing ranges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            for (int j = ranges.size()-2; j >= 0; -- j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // Get lower and upper bounds of the two ranges being compared.
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   256
                int[] rangea = ranges.elementAt (j);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                int lba = rangea[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                int uba = rangea[1];
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   259
                int[] rangeb = ranges.elementAt (j+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                int lbb = rangeb[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                int ubb = rangeb[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                /* If the two ranges overlap or are adjacent, coalesce them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                 * The two ranges overlap if the larger lower bound is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                 * than or equal to the smaller upper bound. The two ranges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                 * are adjacent if the larger lower bound is one greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                 * than the smaller upper bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (Math.max(lba, lbb) - Math.min(uba, ubb) <= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    // The coalesced range is from the smaller lower bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    // the larger upper bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    ranges.setElementAt(new int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                           {Math.min(lba, lbb),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                                Math.max(uba, ubb)}, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    ranges.remove (j+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                } else if (lba > lbb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    /* If the two ranges don't overlap and aren't adjacent but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                     * are out of order, swap them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    ranges.setElementAt (rangeb, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    ranges.setElementAt (rangea, j+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                /* If the two ranges don't overlap and aren't adjacent and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                 * aren't out of order, we're done early.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Convert the given vector of int[] objects to canonical array form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   296
    private static int[][] canonicalArrayForm(Vector<int[]> ranges) {
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   297
        return ranges.toArray (new int[ranges.size()][]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Construct a new set-of-integer attribute with the given members in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * array form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param  members  Set members in array form. If null, an empty set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *                     constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @exception  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *     (Unchecked exception) Thrown if any element of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *     <CODE>members</CODE> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @exception  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *     (Unchecked exception) Thrown if any element of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *     <CODE>members</CODE> is not a length-one or length-two array or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *     any non-null range in <CODE>members</CODE> has a lower bound less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *     than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    protected SetOfIntegerSyntax(int[][] members) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        this.members = parse (members);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Parse the given array form, returning canonical array form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private static int[][] parse(int[][] members) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // Create vector to hold int[] elements, each element being one range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // parsed out of members.
25093
8ec5ec01740e 8042864: Fix raw and unchecked warnings in javax.print
darcy
parents: 5506
diff changeset
   326
        Vector<int[]> ranges = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // Process all integer groups in members.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        int n = (members == null ? 0 : members.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        for (int i = 0; i < n; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // Get lower and upper bounds of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            int lb, ub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (members[i].length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                lb = ub = members[i][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            } else if (members[i].length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                lb = members[i][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                ub = members[i][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // Verify valid bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (lb <= ub && lb < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // Accumulate the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            accumulate(ranges, lb, ub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                // Return canonical array form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                return canonicalArrayForm (ranges);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Construct a new set-of-integer attribute containing a single integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param  member  Set member.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @exception  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *     (Unchecked exception) Thrown if <CODE>member</CODE> is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *     zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    protected SetOfIntegerSyntax(int member) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (member < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        members = new int[][] {{member, member}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Construct a new set-of-integer attribute containing a single range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * integers. If the lower bound is greater than the upper bound (a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * range), an empty set is constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param  lowerBound  Lower bound of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param  upperBound  Upper bound of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @exception  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *     (Unchecked exception) Thrown if the range is non-null and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *     <CODE>lowerBound</CODE> is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    protected SetOfIntegerSyntax(int lowerBound, int upperBound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (lowerBound <= upperBound && lowerBound < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        members = lowerBound <=upperBound ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            new int[][] {{lowerBound, upperBound}} :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            new int[0][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Obtain this set-of-integer attribute's members in canonical array form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * The returned array is "safe;" the client may alter it without affecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * this set-of-integer attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @return  This set-of-integer attribute's members in canonical array form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public int[][] getMembers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        int n = members.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        int[][] result = new int[n][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        for (int i = 0; i < n; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            result[i] = new int[] {members[i][0], members[i][1]};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Determine if this set-of-integer attribute contains the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param  x  Integer value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @return  True if this set-of-integer attribute contains the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *          <CODE>x</CODE>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public boolean contains(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // Do a linear search to find the range that contains x, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        int n = members.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        for (int i = 0; i < n; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (x < members[i][0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            } else if (x <= members[i][1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Determine if this set-of-integer attribute contains the given integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * attribute's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param  attribute  Integer attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @return  True if this set-of-integer attribute contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *          <CODE>theAttribute</CODE>'s value, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public boolean contains(IntegerSyntax attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return contains (attribute.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Determine the smallest integer in this set-of-integer attribute that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * greater than the given value. If there are no integers in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * set-of-integer attribute greater than the given value, <CODE>-1</CODE> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * returned. (Since a set-of-integer attribute can only contain nonnegative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * values, <CODE>-1</CODE> will never appear in the set.) You can use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * <CODE>next()</CODE> method to iterate through the integer values in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * set-of-integer attribute in ascending order, like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *     SetOfIntegerSyntax attribute = . . .;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *     int i = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *     while ((i = attribute.next (i)) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *         foo (i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param  x  Integer value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @return  The smallest integer in this set-of-integer attribute that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *          greater than <CODE>x</CODE>, or <CODE>-1</CODE> if no integer in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *          this set-of-integer attribute is greater than <CODE>x</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public int next(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // Do a linear search to find the range that contains x, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        int n = members.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        for (int i = 0; i < n; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (x < members[i][0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                return members[i][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            } else if (x < members[i][1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                return x + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Returns whether this set-of-integer attribute is equivalent to the passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * in object. To be equivalent, all of the following conditions must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <OL TYPE=1>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <CODE>object</CODE> is not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * <LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <CODE>object</CODE> is an instance of class SetOfIntegerSyntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * <LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * This set-of-integer attribute's members and <CODE>object</CODE>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * members are the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param  object  Object to compare to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @return  True if <CODE>object</CODE> is equivalent to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *          set-of-integer attribute, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public boolean equals(Object object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (object != null && object instanceof SetOfIntegerSyntax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            int[][] myMembers = this.members;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            int[][] otherMembers = ((SetOfIntegerSyntax) object).members;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            int m = myMembers.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            int n = otherMembers.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            if (m == n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                for (int i = 0; i < m; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    if (myMembers[i][0] != otherMembers[i][0] ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        myMembers[i][1] != otherMembers[i][1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Returns a hash code value for this set-of-integer attribute. The hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * code is the sum of the lower and upper bounds of the ranges in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * canonical array form, or 0 for an empty set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        int n = members.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        for (int i = 0; i < n; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            result += members[i][0] + members[i][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Returns a string value corresponding to this set-of-integer attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * The string value is a zero-length string if this set is empty. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * the string value is a comma-separated list of the ranges in the canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * array form, where each range is represented as <CODE>"<I>i</I>"</CODE> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * the lower bound equals the upper bound or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <CODE>"<I>i</I>-<I>j</I>"</CODE> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 5506
diff changeset
   543
        StringBuilder result = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        int n = members.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                result.append (',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            result.append (members[i][0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            if (members[i][0] != members[i][1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                result.append ('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                result.append (members[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
}