compactstringarray.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 451 行 · 第 1/2 页

JAVA
451
字号
/*
 * @(#)CompactStringArray.java	1.9 97/10/28
 *
 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
 *
 * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
 *
 *   The original version of this source code and documentation is copyrighted
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
 * materials are provided under terms of a License Agreement between Taligent
 * and Sun. This technology is protected by multiple US and International
 * patents. This notice and attribution to Taligent may not be removed.
 *   Taligent is a registered trademark of Taligent, Inc.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */

package java.text;

/**
 * class CompactATypeArray : use only on primitive data types
 * Provides a compact way to store information that is indexed by Unicode
 * values, such as character properties, types, keyboard values, etc.This
 * is very useful when you have a block of Unicode data that contains
 * significant values while the rest of the Unicode data is unused in the
 * application or when you have a lot of redundance, such as where all 21,000
 * Han ideographs have the same value.  However, lookup is much faster than a
 * hash table.
 * A compact array of any primitive data type serves two purposes:
 * <UL type = round>
 *     <LI>Fast access of the indexed values.
 *     <LI>Smaller memory footprint.
 * </UL>
 * A compact array is composed of a index array and value array.  The index
 * array contains the indicies of Unicode characters to the value array.
 *
 * @see                CompactShortArray
 * @see                CompactByteArray
 * @see                CompactIntArray
 * @see                CompactCharArray
 * @version            1.9 10/28/97
 * @author             Helena Shih
 */
final class CompactStringArray implements Cloneable {

    /**
     * The total number of Unicode characters.
     */
    public static  final int UNICODECOUNT =65536;

    /**
     * Default constructor for CompactStringArray, the default value of the
     * compact array is "".
     */
    public CompactStringArray()
    {
        this("");
    }
    /**
     * Constructor for CompactStringArray.
     * @param defaultValue the default value of the compact array.
     */
    public CompactStringArray(String defaultValue)
    {
        int i;
        values = new char[UNICODECOUNT]; /*type = char*/
        indices = new short[INDEXCOUNT];
        setElementAt((char)0,'\uFFFF',defaultValue);
        for (i = 0; i < INDEXCOUNT; ++i) {
            indices[i] = (short)(i<<BLOCKSHIFT);
        }
        isCompact = false;
    }
    /**
     * Constructor for CompactStringArray.
     * @param indexArray the indicies of the compact array.
     * @param newValues the values of the compact array.
     * @exception IllegalArgumentException If the index is out of range.
     */
    public CompactStringArray(short indexArray[],
                              char[] newValues,
                              String exceptions)
    {
        int i;
        if (indexArray.length != INDEXCOUNT)
            throw new IllegalArgumentException("Index out of bounds.");
        for (i = 0; i < INDEXCOUNT; ++i) {
            short index = indexArray[i];
            if ((index < 0) || (index >= newValues.length+BLOCKCOUNT))
                throw new IllegalArgumentException("Index out of bounds.");
        }
        indices = indexArray;
        values = newValues;
    }
    /**
     * Get the mapped value (String) of a Unicode character.
     * @param index the character to get the mapped value with
     * @param toAppendTo the string buffer to append the values to
     */
    public void elementAt(char index, StringBuffer toAppendTo)
    {
        char result = (values[(indices[index>>BLOCKSHIFT] & 0xFFFF) +
                             (index & BLOCKMASK)]);
        if (result >= '\uE000' && result <= '\uF800') {
            for (int i = (int) result - 0xE000; ; ++i) {
                result = exceptions.charAt(i);
                if (result == '\uFFFF') return;
                toAppendTo.append(result);
            }
        } else {
            toAppendTo.append(result);
        }
    }
    /**
     * Get the mapped value of a Unicode character.
     * @param index the character to get the mapped value with
     * @return the mapped value of the given character
     */
    public String elementAt(char index) {
        StringBuffer result = new StringBuffer();
        elementAt(index,result);
        return result.toString();
    }
    /**
     * Set a new value for a Unicode character.
     * Set automatically expands the array if it is compacted.
     * @param index the character to set the mapped value with
     * @param value the new mapped value
     */
    public void setElementAt(char index, String value)
    {
        if (isCompact)
            expand();
        if (value.length() == 1) {
            char ch = value.charAt(0);
            if (ch < '\uE000' || ch >= '\uF800') {
                values[(int)index] = ch;
                return;
            }
        }
        // search for the string to see if it is already present
        String temp = value + '\uFFFF';
        int position = exceptions.toString().indexOf(temp);
        if (position != -1) {
            values[(int)index] = (char)(0xE000 + position);
            return;
        };
        // if not found, append.
        values[(int)index] = (char) (0xE000 + exceptions.length());
        for (int i = 0; i < value.length(); ++i) {
            exceptions.append(value.charAt(i));
        }
        exceptions.append('\uFFFF');    // termination
    }
    /**
     * Set new values for a range of Unicode character.
     * @param start the starting offset of the range
     * @param end the ending offset of the range
     * @param value the new mapped value
     */
   public void setElementAt(char start, char end, String value)
    {
        if (start >= end) return; // catch degenerate case
        setElementAt(start,value);
        char firstValue = values[(int)start];
        for (int i = start + 1; i <= end; ++i) {
            values[i] = firstValue;
        }
    }
    /**
     * Compact the array.
     */
    public void compact()
    {
        if (isCompact == false) {
            char[]      tempIndex;
            int                     tempIndexCount;
            char[]          tempArray;
            short           iBlock, iIndex;

            // make temp storage, larger than we need
            tempIndex = new char[UNICODECOUNT];
            // set up first block.
            tempIndexCount = BLOCKCOUNT;
            for (iIndex = 0; iIndex < BLOCKCOUNT; ++iIndex) {
                tempIndex[iIndex] = (char)iIndex;
            }; // endfor (iIndex = 0; .....)
            indices[0] = (short)0;

            // for each successive block, find out its first position
            // in the compacted array
            for (iBlock = 1; iBlock < INDEXCOUNT; ++iBlock) {
                int     newCount, firstPosition, block;
                block = iBlock<<BLOCKSHIFT;
                if (DEBUGSMALL) if (block > DEBUGSMALLLIMIT) break;
                firstPosition = FindOverlappingPosition(block, tempIndex,
                                                        tempIndexCount);

                newCount = firstPosition + BLOCKCOUNT;
                if (newCount > tempIndexCount) {
                    for (iIndex = (short)tempIndexCount;
                         iIndex < newCount;
                         ++iIndex) {
                        tempIndex[iIndex]
                            = (char)(iIndex - firstPosition + block);
                    } // endfor (iIndex = tempIndexCount....)
                    tempIndexCount = newCount;
                } // endif (newCount > tempIndexCount)
                indices[iBlock] = (short)firstPosition;
            } // endfor (iBlock = 1.....)

            // now allocate and copy the items into the array
            tempArray = new char[tempIndexCount];

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?