home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / UnicodeClassMapping.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  101 lines

  1. /*
  2.  * @(#)UnicodeClassMapping.java    1.5 97/03/07
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996-1997 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996-1997 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32.  
  33. import java.lang.Character;
  34. /**
  35.  * This class maps categories to state change inputs for the
  36.  * WordBreakTable.  An entire category is mapped to the same
  37.  * value unless the character in question appears in the exception list.
  38.  */
  39. final class UnicodeClassMapping
  40. {
  41.     private int mappedValue[];
  42.     private SpecialMapping exceptionChars[];
  43.  
  44.     /**
  45.      * Create a mapping given a mapping from categories and a list
  46.      * of exceptions.  Both the mapping list and exceptionChars list must
  47.      * be sorted in ascending order.
  48.      */
  49.     public UnicodeClassMapping(int mappedValue[],
  50.                                SpecialMapping exceptionChars[])
  51.     {
  52.         this.mappedValue = mappedValue;
  53.         this.exceptionChars = exceptionChars;
  54.     }
  55.  
  56.     /**
  57.      * Map a character to a stage change input for WordBreakTable
  58.      * @param ch The character to map.
  59.      * @return The mapped value.
  60.      */
  61.     public int mappedChar(char ch)
  62.     {
  63.         //This is a special case optimization for ASCII text.  If the
  64.         //exception lists change to include items in this range, this
  65.         //should be removed.
  66.         if ((exceptionChars.length == 0) || ('\u003F' < ch && ch < '\u00A0')) {
  67.             return mappedValue[Character.getType(ch)];
  68.         }
  69.  
  70.         //do binary search of exceptionChars table
  71.         int min = 0;
  72.         int max = exceptionChars.length;
  73.     int pos;
  74.     while ((max - (pos = ((max - min) >> 1) + min)) > 1) {
  75.             if (ch > exceptionChars[pos].endChar) {
  76.                 min = pos+1;
  77.             }
  78.             else {
  79.                 max = pos;
  80.             }
  81.         }
  82.         SpecialMapping sm = exceptionChars[min];
  83.         if (sm.startChar <= ch && ch <= sm.endChar) {
  84.             return sm.newValue;
  85.         }
  86.         else if (max < exceptionChars.length) {
  87.             sm = exceptionChars[max];
  88.             if (sm.startChar <= ch && ch <= sm.endChar) {
  89.                 return sm.newValue;
  90.             }
  91.             else {
  92.                 return mappedValue[Character.getType(ch)];
  93.             }
  94.         }
  95.         else {
  96.             return mappedValue[Character.getType(ch)];
  97.         }
  98.     }
  99. }
  100.  
  101.