~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to javax/print/attribute/SetOfIntegerSyntax.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* SetOfIntegerSyntax.java -- 
2
 
   Copyright (C) 2003, 2004  Free Software Foundation, Inc.
 
2
   Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
3
3
 
4
4
This file is part of GNU Classpath.
5
5
 
45
45
import java.util.Comparator;
46
46
 
47
47
/**
48
 
 * @author Michael Koch
 
48
 * <code>SetOfIntegerSyntax</code> is the abstract base class of all attribute 
 
49
 * classes which provide a set of non-negative integers as value (e.g. the
 
50
 * page ranges to print) represented as single values or ranges of values.
 
51
 * <p>
 
52
 * A <code>SetOfIntegerSyntax</code> instance consists of an integer array of
 
53
 * ranges. Ranges may have the same lower and upper bound representing a single
 
54
 * integer value. Ranges with a lower bound greater than the upper bound are 
 
55
 * null ranges and discarded. Ranges may overlap in their values. In no case 
 
56
 * negative integers are allowed.
 
57
 * </p>
 
58
 * <p>
 
59
 * There are several constructors available:
 
60
 * <ul>
 
61
 * <li><code>SetOfIntegerSyntax(int member)</code><br>
 
62
 * Constructor for an instance with only one integer value.
 
63
 * </li><br>
 
64
 * <li><code>SetOfIntegerSyntax(int lowerBound, int upperBound)</code><br>
 
65
 * Constructor for an instance with one range of integer values.
 
66
 * </li><br>
 
67
 * <li><code>SetOfIntegerSyntax(int[][] members)</code><br>
 
68
 * Flexible constructor for an instance with several single integer values 
 
69
 * and/or several ranges of integer values. The allowed array form is an 
 
70
 * array of integer arrays of length one or two. Examples are: 
 
71
 * <code>int[0][]</code> for empty set of integers, <code>int[][] {{1}}</code>
 
72
 * , <code>int[][] {{1,5}}</code>, <code>int[][] {{1,5},{7,9}}</code>,
 
73
 * <code>int[][] {{3,7},{19}}</code>.
 
74
 * </li><br>
 
75
 * <li><code>SetOfIntegerSyntax(String s)</code><br>
 
76
 * Flexible constructor for an instance with several single integer values 
 
77
 * and/or several ranges of integer values. The allowed String instance have
 
78
 * to be a String with comma separated ranges of integer values or single 
 
79
 * values. Ranges are represented by two integer with a hypen (-) or colon (:)
 
80
 * between the lower and upper bound value. Whitespace characters are ignored.
 
81
 * Examples are: <code>""</code> for an empty set of integers, 
 
82
 * <code>"1"</code>, <code>"1-5"</code>, <code>"1-5,7-9"</code>, 
 
83
 * <code>"3-7,19"</code> and <code>"1:2,4"</code>.
 
84
 * </li>
 
85
 * </ul>
 
86
 * </p>
 
87
 * <p>
 
88
 * <b>Internal storage:</b><br>
 
89
 * The set of integers are stored internally in a normalized array form.
 
90
 * In the normalized array form the set of integer ranges are represented
 
91
 * in as few ranges as possible and overlapping ranges are merged. The ranges 
 
92
 * are always represented as an integer array of length two with ranges 
 
93
 * stored in {lower bound, upper bound} form. The ranges are stored in 
 
94
 * ascending order, without any null ranges.
 
95
 * </p>
 
96
 * @author Michael Koch (konqueror@gmx.de)
49
97
 */
50
98
public abstract class SetOfIntegerSyntax
51
99
  implements Cloneable, Serializable
96
144
   *
97
145
   * @param member the member value
98
146
   *
99
 
   * @exception IllegalArgumentException if member is < 0
 
147
   * @exception IllegalArgumentException if member is &lt; 0
100
148
   */
101
149
  protected SetOfIntegerSyntax(int member)
102
150
  {
109
157
  /**
110
158
   * Creates a <code>SetOfIntegerSyntax</code> object.
111
159
   *
112
 
   * @param members the members to use in this set
 
160
   * @param members the members to use in this set. If
 
161
   * <code>null</code> an empty set is created.
113
162
   *
114
163
   * @exception IllegalArgumentException if any element is invalid
115
164
   * @exception NullPointerException if any element of members is null
176
225
    return readAny;
177
226
  }
178
227
 
 
228
  /**
 
229
   * Creates a <code>SetOfIntegerSyntax</code> object.
 
230
   *
 
231
   * @param s the members to use in this set in string form. If
 
232
   * <code>null</code> an empty set is created.
 
233
   *
 
234
   * @exception IllegalArgumentException if any element is invalid
 
235
   */
179
236
  protected SetOfIntegerSyntax(String s)
180
237
  {
181
 
    ArrayList vals = new ArrayList();
182
 
 
183
 
    StringCharacterIterator it = new StringCharacterIterator(s);
184
 
 
185
 
    while (true)
186
 
      {
187
 
        // Skip whitespace.
188
 
        if (skipWhitespace(it))
189
 
          break;
190
 
 
191
 
        // Parse integer.
192
 
        int index = it.getIndex();
193
 
        if (! skipNumber(it))
194
 
          throw new IllegalArgumentException();
195
 
        int[] item = new int[2];
196
 
        item[0] = Integer.parseInt(s.substring(index, it.getIndex()));
197
 
 
198
 
        if (! skipWhitespace(it))
 
238
    if (s == null)
 
239
      this.members = normalize(new int[0][], 0);
 
240
    else
 
241
      {      
 
242
        ArrayList vals = new ArrayList();
 
243
        
 
244
        StringCharacterIterator it = new StringCharacterIterator(s);
 
245
        
 
246
        while (true)
199
247
          {
200
 
            char c = it.current();
201
 
            if (c == ':' || c == '-')
 
248
            // Skip whitespace.
 
249
            if (skipWhitespace(it))
 
250
              break;
 
251
            
 
252
            // Parse integer.
 
253
            int index = it.getIndex();
 
254
            if (! skipNumber(it))
 
255
              throw new IllegalArgumentException();
 
256
            int[] item = new int[2];
 
257
            item[0] = Integer.parseInt(s.substring(index, it.getIndex()));
 
258
            
 
259
            if (! skipWhitespace(it))
202
260
              {
203
 
                it.next();
204
 
                if (skipWhitespace(it))
205
 
                  throw new IllegalArgumentException();
206
 
                index = it.getIndex();
207
 
                if (! skipNumber(it))
208
 
                  throw new IllegalArgumentException();
209
 
                item[1] = Integer.parseInt(s.substring(index, it.getIndex()));
 
261
                char c = it.current();
 
262
                if (c == ':' || c == '-')
 
263
                  {
 
264
                  it.next();
 
265
                  if (skipWhitespace(it))
 
266
                    throw new IllegalArgumentException();
 
267
                  index = it.getIndex();
 
268
                  if (! skipNumber(it))
 
269
                    throw new IllegalArgumentException();
 
270
                  item[1] = Integer.parseInt(s.substring(index, it.getIndex()));
 
271
                  }
 
272
                else
 
273
                  item[1] = item[0];
210
274
              }
211
275
            else
212
276
              item[1] = item[0];
 
277
            
 
278
            if (item[0] <= item[1]) 
 
279
              vals.add(item);
 
280
            
 
281
            if (skipWhitespace(it))
 
282
              break;
 
283
            if (it.current() != ',')
 
284
              throw new IllegalArgumentException();
 
285
            it.next();
213
286
          }
214
 
        else
215
 
          item[1] = item[0];
216
 
 
217
 
        if (item[0] <= item[1]) 
218
 
          vals.add(item);
219
287
        
220
 
        if (skipWhitespace(it))
221
 
          break;
222
 
        if (it.current() != ',')
223
 
          throw new IllegalArgumentException();
224
 
        it.next();
 
288
        members = normalize((int[][]) vals.toArray(new int[0][]), vals.size());
225
289
      }
226
 
 
227
 
    members = normalize((int[][]) vals.toArray(new int[0][]), vals.size());
228
290
  }
229
291
 
230
292
  /**
248
310
  }
249
311
 
250
312
  /**
251
 
   * Checks if this set contains value.
 
313
   * Checks if this set contains the given value.
252
314
   *
253
315
   * @param value the value to test for
254
316
   *
269
331
  }
270
332
 
271
333
  /**
272
 
   * Checks if this set contains value.
 
334
   * Checks if this set contains the given value.
273
335
   *
274
336
   * @param value the value to test for
275
337
   *
281
343
  }
282
344
 
283
345
  /**
284
 
   * Tests of obj is equal to this object.
 
346
   * Tests if the given object is equal to this object.
285
347
   *
286
348
   * @param obj the object to test
287
349
   *
306
368
  /**
307
369
   * Returns an array describing the members included in this set.
308
370
   *
309
 
   * @return the array with the members
 
371
   * @return The members in normalized array form.
310
372
   */
311
373
  public int[][] getMembers()
312
374
  {
316
378
  /**
317
379
   * Returns the hashcode for this object.
318
380
   *
319
 
   * @return the hashcode
 
381
   * @return The hashcode.
320
382
   */
321
383
  public int hashCode()
322
384
  {
331
393
   *
332
394
   * @param x an integer value
333
395
   *
334
 
   * @return the next value
 
396
   * @return The next smallest integer value, or <code>-1</code> if there 
 
397
   * is no greater integer in the set.
335
398
   */
336
399
  public int next(int x)
337
400
  {
349
412
 
350
413
  /**
351
414
   * Returns the string representation for this object.
 
415
   * The value is a zero length string for an empty set, or a comma seperated
 
416
   * list of ranges and single values in the form <code>"1-2,5-7,10"</code>.
352
417
   *
353
 
   * @return the string representation
 
418
   * @return The string representation.
354
419
   */
355
420
  public String toString()
356
421
  {