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 27 package javax.print.attribute; 28 29 import java.io.Serializable; 30 import java.util.Vector; 31 32 /** 33 * Class SetOfIntegerSyntax is an abstract base class providing the common 34 * implementation of all attributes whose value is a set of nonnegative 35 * integers. This includes attributes whose value is a single range of integers 36 * and attributes whose value is a set of ranges of integers. 37 * <P> 38 * You can construct an instance of SetOfIntegerSyntax by giving it in "string 39 * form." The string consists of zero or more comma-separated integer groups. 40 * Each integer group consists of either one integer, two integers separated by 41 * a hyphen (<CODE>-</CODE>), or two integers separated by a colon 42 * (<CODE>:</CODE>). Each integer consists of one or more decimal digits 43 * (<CODE>0</CODE> through <CODE>9</CODE>). Whitespace characters cannot 44 * appear within an integer but are otherwise ignored. For example: 45 * <CODE>""</CODE>, <CODE>"1"</CODE>, <CODE>"5-10"</CODE>, <CODE>"1:2, 46 * 4"</CODE>. 47 * <P> 48 * You can also construct an instance of SetOfIntegerSyntax by giving it in 49 * "array form." Array form consists of an array of zero or more integer groups 50 * where each integer group is a length-1 or length-2 array of 51 * <CODE>int</CODE>s; for example, <CODE>int[0][]</CODE>, 52 * <CODE>int[][]{{1}}</CODE>, <CODE>int[][]{{5,10}}</CODE>, 53 * <CODE>int[][]{{1,2},{4}}</CODE>. 54 * <P> 55 * In both string form and array form, each successive integer group gives a 56 * range of integers to be included in the set. The first integer in each group 57 * gives the lower bound of the range; the second integer in each group gives 58 * the upper bound of the range; if there is only one integer in the group, the 59 * upper bound is the same as the lower bound. If the upper bound is less than 60 * the lower bound, it denotes a null range (no values). If the upper bound is 61 * equal to the lower bound, it denotes a range consisting of a single value. If 62 * the upper bound is greater than the lower bound, it denotes a range 63 * consisting of more than one value. The ranges may appear in any order and are 64 * allowed to overlap. The union of all the ranges gives the set's contents. 65 * Once a SetOfIntegerSyntax instance is constructed, its value is immutable. 66 * <P> 67 * The SetOfIntegerSyntax object's value is actually stored in "<I>canonical</I> 68 * array form." This is the same as array form, except there are no null ranges; 69 * the members of the set are represented in as few ranges as possible (i.e., 70 * overlapping ranges are coalesced); the ranges appear in ascending order; and 71 * each range is always represented as a length-two array of <CODE>int</CODE>s 72 * in the form {lower bound, upper bound}. An empty set is represented as a 73 * zero-length array. 74 * <P> 75 * Class SetOfIntegerSyntax has operations to return the set's members in 76 * canonical array form, to test whether a given integer is a member of the 77 * set, and to iterate through the members of the set. 78 * 79 * @author David Mendenhall 80 * @author Alan Kaminsky 81 */ 82 public abstract class SetOfIntegerSyntax implements Serializable, Cloneable { 83 84 private static final long serialVersionUID = 3666874174847632203L; 85 86 /** 87 * This set's members in canonical array form. 88 * @serial 89 */ 90 private int[][] members; 91 92 93 /** 94 * Construct a new set-of-integer attribute with the given members in 95 * string form. 96 * 97 * @param members Set members in string form. If null, an empty set is 98 * constructed. 99 * 100 * @exception IllegalArgumentException 101 * (Unchecked exception) Thrown if <CODE>members</CODE> does not 102 * obey the proper syntax. 103 */ 104 protected SetOfIntegerSyntax(String members) { 105 this.members = parse (members); 106 } 107 108 /** 109 * Parse the given string, returning canonical array form. 110 */ 111 private static int[][] parse(String members) { 112 // Create vector to hold int[] elements, each element being one range 113 // parsed out of members. 114 Vector<int[]> theRanges = new Vector<>(); 115 116 // Run state machine over members. 117 int n = (members == null ? 0 : members.length()); 118 int i = 0; 119 int state = 0; 120 int lb = 0; 121 int ub = 0; 288 } 289 } 290 } 291 292 /** 293 * Convert the given vector of int[] objects to canonical array form. 294 */ 295 private static int[][] canonicalArrayForm(Vector<int[]> ranges) { 296 return ranges.toArray (new int[ranges.size()][]); 297 } 298 299 /** 300 * Construct a new set-of-integer attribute with the given members in 301 * array form. 302 * 303 * @param members Set members in array form. If null, an empty set is 304 * constructed. 305 * 306 * @exception NullPointerException 307 * (Unchecked exception) Thrown if any element of 308 * <CODE>members</CODE> is null. 309 * @exception IllegalArgumentException 310 * (Unchecked exception) Thrown if any element of 311 * <CODE>members</CODE> is not a length-one or length-two array or if 312 * any non-null range in <CODE>members</CODE> has a lower bound less 313 * than zero. 314 */ 315 protected SetOfIntegerSyntax(int[][] members) { 316 this.members = parse (members); 317 } 318 319 /** 320 * Parse the given array form, returning canonical array form. 321 */ 322 private static int[][] parse(int[][] members) { 323 // Create vector to hold int[] elements, each element being one range 324 // parsed out of members. 325 Vector<int[]> ranges = new Vector<>(); 326 327 // Process all integer groups in members. 328 int n = (members == null ? 0 : members.length); 329 for (int i = 0; i < n; ++ i) { 330 // Get lower and upper bounds of the range. 331 int lb, ub; 332 if (members[i].length == 1) { 340 341 // Verify valid bounds. 342 if (lb <= ub && lb < 0) { 343 throw new IllegalArgumentException(); 344 } 345 346 // Accumulate the range. 347 accumulate(ranges, lb, ub); 348 } 349 350 // Return canonical array form. 351 return canonicalArrayForm (ranges); 352 } 353 354 /** 355 * Construct a new set-of-integer attribute containing a single integer. 356 * 357 * @param member Set member. 358 * 359 * @exception IllegalArgumentException 360 * (Unchecked exception) Thrown if <CODE>member</CODE> is less than 361 * zero. 362 */ 363 protected SetOfIntegerSyntax(int member) { 364 if (member < 0) { 365 throw new IllegalArgumentException(); 366 } 367 members = new int[][] {{member, member}}; 368 } 369 370 /** 371 * Construct a new set-of-integer attribute containing a single range of 372 * integers. If the lower bound is greater than the upper bound (a null 373 * range), an empty set is constructed. 374 * 375 * @param lowerBound Lower bound of the range. 376 * @param upperBound Upper bound of the range. 377 * 378 * @exception IllegalArgumentException 379 * (Unchecked exception) Thrown if the range is non-null and 380 * <CODE>lowerBound</CODE> is less than zero. 381 */ 382 protected SetOfIntegerSyntax(int lowerBound, int upperBound) { 383 if (lowerBound <= upperBound && lowerBound < 0) { 384 throw new IllegalArgumentException(); 385 } 386 members = lowerBound <=upperBound ? 387 new int[][] {{lowerBound, upperBound}} : 388 new int[0][]; 389 } 390 391 392 /** 393 * Obtain this set-of-integer attribute's members in canonical array form. 394 * The returned array is "safe;" the client may alter it without affecting 395 * this set-of-integer attribute. 396 * 397 * @return This set-of-integer attribute's members in canonical array form. 398 */ 399 public int[][] getMembers() { 400 int n = members.length; 401 int[][] result = new int[n][]; 402 for (int i = 0; i < n; ++ i) { 403 result[i] = new int[] {members[i][0], members[i][1]}; 404 } 405 return result; 406 } 407 408 /** 409 * Determine if this set-of-integer attribute contains the given value. 410 * 411 * @param x Integer value. 412 * 413 * @return True if this set-of-integer attribute contains the value 414 * <CODE>x</CODE>, false otherwise. 415 */ 416 public boolean contains(int x) { 417 // Do a linear search to find the range that contains x, if any. 418 int n = members.length; 419 for (int i = 0; i < n; ++ i) { 420 if (x < members[i][0]) { 421 return false; 422 } else if (x <= members[i][1]) { 423 return true; 424 } 425 } 426 return false; 427 } 428 429 /** 430 * Determine if this set-of-integer attribute contains the given integer 431 * attribute's value. 432 * 433 * @param attribute Integer attribute. 434 * 435 * @return True if this set-of-integer attribute contains 436 * <CODE>theAttribute</CODE>'s value, false otherwise. 437 */ 438 public boolean contains(IntegerSyntax attribute) { 439 return contains (attribute.getValue()); 440 } 441 442 /** 443 * Determine the smallest integer in this set-of-integer attribute that is 444 * greater than the given value. If there are no integers in this 445 * set-of-integer attribute greater than the given value, <CODE>-1</CODE> is 446 * returned. (Since a set-of-integer attribute can only contain nonnegative 447 * values, <CODE>-1</CODE> will never appear in the set.) You can use the 448 * <CODE>next()</CODE> method to iterate through the integer values in a 449 * set-of-integer attribute in ascending order, like this: 450 * <PRE> 451 * SetOfIntegerSyntax attribute = . . .; 452 * int i = -1; 453 * while ((i = attribute.next (i)) != -1) 454 * { 455 * foo (i); 456 * } 457 * </PRE> 458 * 459 * @param x Integer value. 460 * 461 * @return The smallest integer in this set-of-integer attribute that is 462 * greater than <CODE>x</CODE>, or <CODE>-1</CODE> if no integer in 463 * this set-of-integer attribute is greater than <CODE>x</CODE>. 464 */ 465 public int next(int x) { 466 // Do a linear search to find the range that contains x, if any. 467 int n = members.length; 468 for (int i = 0; i < n; ++ i) { 469 if (x < members[i][0]) { 470 return members[i][0]; 471 } else if (x < members[i][1]) { 472 return x + 1; 473 } 474 } 475 return -1; 476 } 477 478 /** 479 * Returns whether this set-of-integer attribute is equivalent to the passed 480 * in object. To be equivalent, all of the following conditions must be 481 * true: 482 * <OL TYPE=1> 483 * <LI> 484 * <CODE>object</CODE> is not null. 485 * <LI> 486 * <CODE>object</CODE> is an instance of class SetOfIntegerSyntax. 487 * <LI> 488 * This set-of-integer attribute's members and <CODE>object</CODE>'s 489 * members are the same. 490 * </OL> 491 * 492 * @param object Object to compare to. 493 * 494 * @return True if <CODE>object</CODE> is equivalent to this 495 * set-of-integer attribute, false otherwise. 496 */ 497 public boolean equals(Object object) { 498 if (object != null && object instanceof SetOfIntegerSyntax) { 499 int[][] myMembers = this.members; 500 int[][] otherMembers = ((SetOfIntegerSyntax) object).members; 501 int m = myMembers.length; 502 int n = otherMembers.length; 503 if (m == n) { 504 for (int i = 0; i < m; ++ i) { 505 if (myMembers[i][0] != otherMembers[i][0] || 506 myMembers[i][1] != otherMembers[i][1]) { 507 return false; 508 } 509 } 510 return true; 511 } else { 512 return false; 513 } 514 } else { 517 } 518 519 /** 520 * Returns a hash code value for this set-of-integer attribute. The hash 521 * code is the sum of the lower and upper bounds of the ranges in the 522 * canonical array form, or 0 for an empty set. 523 */ 524 public int hashCode() { 525 int result = 0; 526 int n = members.length; 527 for (int i = 0; i < n; ++ i) { 528 result += members[i][0] + members[i][1]; 529 } 530 return result; 531 } 532 533 /** 534 * Returns a string value corresponding to this set-of-integer attribute. 535 * The string value is a zero-length string if this set is empty. Otherwise, 536 * the string value is a comma-separated list of the ranges in the canonical 537 * array form, where each range is represented as <CODE>"<I>i</I>"</CODE> if 538 * the lower bound equals the upper bound or 539 * <CODE>"<I>i</I>-<I>j</I>"</CODE> otherwise. 540 */ 541 public String toString() { 542 StringBuilder result = new StringBuilder(); 543 int n = members.length; 544 for (int i = 0; i < n; i++) { 545 if (i > 0) { 546 result.append (','); 547 } 548 result.append (members[i][0]); 549 if (members[i][0] != members[i][1]) { 550 result.append ('-'); 551 result.append (members[i][1]); 552 } 553 } 554 return result.toString(); 555 } 556 557 } | 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 27 package javax.print.attribute; 28 29 import java.io.Serializable; 30 import java.util.Vector; 31 32 /** 33 * Class SetOfIntegerSyntax is an abstract base class providing the common 34 * implementation of all attributes whose value is a set of nonnegative 35 * integers. This includes attributes whose value is a single range of integers 36 * and attributes whose value is a set of ranges of integers. 37 * <P> 38 * You can construct an instance of SetOfIntegerSyntax by giving it in "string 39 * form." The string consists of zero or more comma-separated integer groups. 40 * Each integer group consists of either one integer, two integers separated by 41 * a hyphen ({@code -}), or two integers separated by a colon 42 * ({@code :}). Each integer consists of one or more decimal digits 43 * ({@code 0} through {@code 9}). Whitespace characters cannot 44 * appear within an integer but are otherwise ignored. For example: 45 * {@code ""}, {@code "1"}, {@code "5-10"}, {@code "1:2, 4"}. 46 * <P> 47 * You can also construct an instance of SetOfIntegerSyntax by giving it in 48 * "array form." Array form consists of an array of zero or more integer groups 49 * where each integer group is a length-1 or length-2 array of 50 * {@code int}s; for example, {@code int[0][]}, 51 * {@code int[][]{{1}}}, {@code int[][]{{5,10}}}, 52 * {@code int[][]{{1,2},{4}}}. 53 * <P> 54 * In both string form and array form, each successive integer group gives a 55 * range of integers to be included in the set. The first integer in each group 56 * gives the lower bound of the range; the second integer in each group gives 57 * the upper bound of the range; if there is only one integer in the group, the 58 * upper bound is the same as the lower bound. If the upper bound is less than 59 * the lower bound, it denotes a null range (no values). If the upper bound is 60 * equal to the lower bound, it denotes a range consisting of a single value. If 61 * the upper bound is greater than the lower bound, it denotes a range 62 * consisting of more than one value. The ranges may appear in any order and are 63 * allowed to overlap. The union of all the ranges gives the set's contents. 64 * Once a SetOfIntegerSyntax instance is constructed, its value is immutable. 65 * <P> 66 * The SetOfIntegerSyntax object's value is actually stored in "<I>canonical</I> 67 * array form." This is the same as array form, except there are no null ranges; 68 * the members of the set are represented in as few ranges as possible (i.e., 69 * overlapping ranges are coalesced); the ranges appear in ascending order; and 70 * each range is always represented as a length-two array of {@code int}s 71 * in the form {lower bound, upper bound}. An empty set is represented as a 72 * zero-length array. 73 * <P> 74 * Class SetOfIntegerSyntax has operations to return the set's members in 75 * canonical array form, to test whether a given integer is a member of the 76 * set, and to iterate through the members of the set. 77 * 78 * @author David Mendenhall 79 * @author Alan Kaminsky 80 */ 81 public abstract class SetOfIntegerSyntax implements Serializable, Cloneable { 82 83 private static final long serialVersionUID = 3666874174847632203L; 84 85 /** 86 * This set's members in canonical array form. 87 * @serial 88 */ 89 private int[][] members; 90 91 92 /** 93 * Construct a new set-of-integer attribute with the given members in 94 * string form. 95 * 96 * @param members Set members in string form. If null, an empty set is 97 * constructed. 98 * 99 * @exception IllegalArgumentException 100 * (Unchecked exception) Thrown if {@code members} does not 101 * obey the proper syntax. 102 */ 103 protected SetOfIntegerSyntax(String members) { 104 this.members = parse (members); 105 } 106 107 /** 108 * Parse the given string, returning canonical array form. 109 */ 110 private static int[][] parse(String members) { 111 // Create vector to hold int[] elements, each element being one range 112 // parsed out of members. 113 Vector<int[]> theRanges = new Vector<>(); 114 115 // Run state machine over members. 116 int n = (members == null ? 0 : members.length()); 117 int i = 0; 118 int state = 0; 119 int lb = 0; 120 int ub = 0; 287 } 288 } 289 } 290 291 /** 292 * Convert the given vector of int[] objects to canonical array form. 293 */ 294 private static int[][] canonicalArrayForm(Vector<int[]> ranges) { 295 return ranges.toArray (new int[ranges.size()][]); 296 } 297 298 /** 299 * Construct a new set-of-integer attribute with the given members in 300 * array form. 301 * 302 * @param members Set members in array form. If null, an empty set is 303 * constructed. 304 * 305 * @exception NullPointerException 306 * (Unchecked exception) Thrown if any element of 307 * {@code members} is null. 308 * @exception IllegalArgumentException 309 * (Unchecked exception) Thrown if any element of 310 * {@code members} is not a length-one or length-two array or if 311 * any non-null range in {@code members} has a lower bound less 312 * than zero. 313 */ 314 protected SetOfIntegerSyntax(int[][] members) { 315 this.members = parse (members); 316 } 317 318 /** 319 * Parse the given array form, returning canonical array form. 320 */ 321 private static int[][] parse(int[][] members) { 322 // Create vector to hold int[] elements, each element being one range 323 // parsed out of members. 324 Vector<int[]> ranges = new Vector<>(); 325 326 // Process all integer groups in members. 327 int n = (members == null ? 0 : members.length); 328 for (int i = 0; i < n; ++ i) { 329 // Get lower and upper bounds of the range. 330 int lb, ub; 331 if (members[i].length == 1) { 339 340 // Verify valid bounds. 341 if (lb <= ub && lb < 0) { 342 throw new IllegalArgumentException(); 343 } 344 345 // Accumulate the range. 346 accumulate(ranges, lb, ub); 347 } 348 349 // Return canonical array form. 350 return canonicalArrayForm (ranges); 351 } 352 353 /** 354 * Construct a new set-of-integer attribute containing a single integer. 355 * 356 * @param member Set member. 357 * 358 * @exception IllegalArgumentException 359 * (Unchecked exception) Thrown if {@code member} is less than 360 * zero. 361 */ 362 protected SetOfIntegerSyntax(int member) { 363 if (member < 0) { 364 throw new IllegalArgumentException(); 365 } 366 members = new int[][] {{member, member}}; 367 } 368 369 /** 370 * Construct a new set-of-integer attribute containing a single range of 371 * integers. If the lower bound is greater than the upper bound (a null 372 * range), an empty set is constructed. 373 * 374 * @param lowerBound Lower bound of the range. 375 * @param upperBound Upper bound of the range. 376 * 377 * @exception IllegalArgumentException 378 * (Unchecked exception) Thrown if the range is non-null and 379 * {@code lowerBound} is less than zero. 380 */ 381 protected SetOfIntegerSyntax(int lowerBound, int upperBound) { 382 if (lowerBound <= upperBound && lowerBound < 0) { 383 throw new IllegalArgumentException(); 384 } 385 members = lowerBound <=upperBound ? 386 new int[][] {{lowerBound, upperBound}} : 387 new int[0][]; 388 } 389 390 391 /** 392 * Obtain this set-of-integer attribute's members in canonical array form. 393 * The returned array is "safe;" the client may alter it without affecting 394 * this set-of-integer attribute. 395 * 396 * @return This set-of-integer attribute's members in canonical array form. 397 */ 398 public int[][] getMembers() { 399 int n = members.length; 400 int[][] result = new int[n][]; 401 for (int i = 0; i < n; ++ i) { 402 result[i] = new int[] {members[i][0], members[i][1]}; 403 } 404 return result; 405 } 406 407 /** 408 * Determine if this set-of-integer attribute contains the given value. 409 * 410 * @param x Integer value. 411 * 412 * @return True if this set-of-integer attribute contains the value 413 * {@code x}, false otherwise. 414 */ 415 public boolean contains(int x) { 416 // Do a linear search to find the range that contains x, if any. 417 int n = members.length; 418 for (int i = 0; i < n; ++ i) { 419 if (x < members[i][0]) { 420 return false; 421 } else if (x <= members[i][1]) { 422 return true; 423 } 424 } 425 return false; 426 } 427 428 /** 429 * Determine if this set-of-integer attribute contains the given integer 430 * attribute's value. 431 * 432 * @param attribute Integer attribute. 433 * 434 * @return True if this set-of-integer attribute contains 435 * {@code theAttribute}'s value, false otherwise. 436 */ 437 public boolean contains(IntegerSyntax attribute) { 438 return contains (attribute.getValue()); 439 } 440 441 /** 442 * Determine the smallest integer in this set-of-integer attribute that is 443 * greater than the given value. If there are no integers in this 444 * set-of-integer attribute greater than the given value, {@code -1} is 445 * returned. (Since a set-of-integer attribute can only contain nonnegative 446 * values, {@code -1} will never appear in the set.) You can use the 447 * {@code next()} method to iterate through the integer values in a 448 * set-of-integer attribute in ascending order, like this: 449 * <PRE> 450 * SetOfIntegerSyntax attribute = . . .; 451 * int i = -1; 452 * while ((i = attribute.next (i)) != -1) 453 * { 454 * foo (i); 455 * } 456 * </PRE> 457 * 458 * @param x Integer value. 459 * 460 * @return The smallest integer in this set-of-integer attribute that is 461 * greater than {@code x}, or {@code -1} if no integer in 462 * this set-of-integer attribute is greater than {@code x}. 463 */ 464 public int next(int x) { 465 // Do a linear search to find the range that contains x, if any. 466 int n = members.length; 467 for (int i = 0; i < n; ++ i) { 468 if (x < members[i][0]) { 469 return members[i][0]; 470 } else if (x < members[i][1]) { 471 return x + 1; 472 } 473 } 474 return -1; 475 } 476 477 /** 478 * Returns whether this set-of-integer attribute is equivalent to the passed 479 * in object. To be equivalent, all of the following conditions must be 480 * true: 481 * <OL TYPE=1> 482 * <LI> 483 * {@code object} is not null. 484 * <LI> 485 * {@code object} is an instance of class SetOfIntegerSyntax. 486 * <LI> 487 * This set-of-integer attribute's members and {@code object}'s 488 * members are the same. 489 * </OL> 490 * 491 * @param object Object to compare to. 492 * 493 * @return True if {@code object} is equivalent to this 494 * set-of-integer attribute, false otherwise. 495 */ 496 public boolean equals(Object object) { 497 if (object != null && object instanceof SetOfIntegerSyntax) { 498 int[][] myMembers = this.members; 499 int[][] otherMembers = ((SetOfIntegerSyntax) object).members; 500 int m = myMembers.length; 501 int n = otherMembers.length; 502 if (m == n) { 503 for (int i = 0; i < m; ++ i) { 504 if (myMembers[i][0] != otherMembers[i][0] || 505 myMembers[i][1] != otherMembers[i][1]) { 506 return false; 507 } 508 } 509 return true; 510 } else { 511 return false; 512 } 513 } else { 516 } 517 518 /** 519 * Returns a hash code value for this set-of-integer attribute. The hash 520 * code is the sum of the lower and upper bounds of the ranges in the 521 * canonical array form, or 0 for an empty set. 522 */ 523 public int hashCode() { 524 int result = 0; 525 int n = members.length; 526 for (int i = 0; i < n; ++ i) { 527 result += members[i][0] + members[i][1]; 528 } 529 return result; 530 } 531 532 /** 533 * Returns a string value corresponding to this set-of-integer attribute. 534 * The string value is a zero-length string if this set is empty. Otherwise, 535 * the string value is a comma-separated list of the ranges in the canonical 536 * array form, where each range is represented as <code>"<I>i</I>"</code> if 537 * the lower bound equals the upper bound or 538 * <code>"<I>i</I>-<I>j</I>"</code> otherwise. 539 */ 540 public String toString() { 541 StringBuilder result = new StringBuilder(); 542 int n = members.length; 543 for (int i = 0; i < n; i++) { 544 if (i > 0) { 545 result.append (','); 546 } 547 result.append (members[i][0]); 548 if (members[i][0] != members[i][1]) { 549 result.append ('-'); 550 result.append (members[i][1]); 551 } 552 } 553 return result.toString(); 554 } 555 556 } |