home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / src / COM / objectspace / jgl / EqualCollationKey.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  2.3 KB  |  70 lines

  1. // Copyright(c) 1997 ObjectSpace, Inc.
  2.  
  3. package COM.objectspace.jgl;
  4.  
  5. import java.text.Collator;
  6. import java.text.CollationKey;
  7.  
  8. /**
  9.  * EqualCollationKey is a binary predicate that returns true
  10.  * if the first operand as a string is equal to the
  11.  * second operand as a string when compared using the given Collator object.
  12.  * Instead of using the compare() call, getCollationKey() is used and the
  13.  * results of that are compared.
  14.  * If either operand is itself a CollationKey object, then that operand is
  15.  * used without getting a new key from the collator.
  16.  * <p>
  17.  * If an explicit Collator object is not given, the default is used.
  18.  * <p>
  19.  * @see java.text.Collator
  20.  * @see java.text.CollationKey
  21.  * @see COM.objectspace.jgl.examples.CollateExamples
  22.  * @version 2.0.2
  23.  * @author ObjectSpace, Inc.
  24.  */
  25.  
  26. public final class EqualCollationKey implements BinaryPredicate
  27.   {
  28.   Collator collator;
  29.  
  30.   /**
  31.    * Construct a EqualCollationKey function object that uses the collator
  32.    * object for the current default locale to compare operands that are not
  33.    * instances of CollationKey.
  34.    */
  35.   public EqualCollationKey()
  36.     {
  37.     collator = Collator.getInstance();
  38.     }
  39.  
  40.   /**
  41.    * Construct a EqualCollationKey function object that uses the given collator
  42.    * object to compare operands that are not instances of CollationKey.
  43.    * @param collator The Collator object that is to be used for comparisons.
  44.    */
  45.   public EqualCollationKey( Collator collator )
  46.     {
  47.     this.collator = collator;
  48.     }
  49.  
  50.   /**
  51.    * Return true if the first operand is equal to the second operand.
  52.    * <STRONG>Note:</STRONG> CollationKeys created by different Collators can 
  53.    * not be compared.
  54.    * @see java.text.CollationKey#compareTo
  55.    * @param first The first operand.
  56.    * @param second The second operand.
  57.    * @return true if the sort key of the first operand is equal to that of the second.
  58.    */
  59.   public boolean execute( Object first, Object second )
  60.     {
  61.     CollationKey firstKey = first instanceof CollationKey
  62.       ? (CollationKey)first
  63.       : collator.getCollationKey( first.toString() );
  64.     CollationKey secondKey = second instanceof CollationKey
  65.       ? (CollationKey)second
  66.       : collator.getCollationKey( second.toString() );
  67.     return firstKey.compareTo( secondKey ) == 0;
  68.     }
  69.   }
  70.