0N/A/*
3261N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *******************************************************************************
0N/A * (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved *
1173N/A * *
0N/A * The original version of this source code and documentation is copyrighted *
0N/A * and owned by IBM, These materials are provided under terms of a License *
0N/A * Agreement between IBM and Sun. This technology is protected by multiple *
0N/A * US and International patents. This notice and attribution to IBM may not *
0N/A * to removed. *
0N/A *******************************************************************************
0N/A */
0N/A
1173N/Apackage sun.text.normalizer;
1173N/A
1173N/Aimport java.io.InputStream;
1173N/Aimport java.io.DataInputStream;
1173N/Aimport java.io.IOException;
0N/Aimport java.util.Arrays;
1999N/A
1999N/A/**
0N/A * Trie implementation which stores data in int, 32 bits.
0N/A * @author synwee
1999N/A * @see com.ibm.icu.impl.Trie
0N/A * @since release 2.1, Jan 01 2002
1999N/A */
1999N/Apublic class IntTrie extends Trie
0N/A{
1173N/A // public constructors ---------------------------------------------
1173N/A
0N/A /**
1999N/A * <p>Creates a new Trie with the settings for the trie data.</p>
1999N/A * <p>Unserialize the 32-bit-aligned input stream and use the data for the
1999N/A * trie.</p>
1999N/A * @param inputStream file input stream to a ICU data file, containing
1999N/A * the trie
1999N/A * @param dataManipulate object which provides methods to parse the char
0N/A * data
0N/A * @throws IOException thrown when data reading fails
0N/A * @draft 2.1
0N/A */
1999N/A public IntTrie(InputStream inputStream, DataManipulate datamanipulate)
2146N/A throws IOException
2146N/A {
2146N/A super(inputStream, datamanipulate);
2146N/A if (!isIntTrie()) {
2146N/A throw new IllegalArgumentException(
2146N/A "Data given does not belong to a int trie.");
2146N/A }
2146N/A }
2146N/A
2146N/A // public methods --------------------------------------------------
1999N/A
1173N/A /**
0N/A * Gets the value associated with the codepoint.
0N/A * If no value is associated with the codepoint, a default value will be
0N/A * returned.
0N/A * @param ch codepoint
0N/A * @return offset to data
0N/A * @draft 2.1
0N/A */
0N/A public final int getCodePointValue(int ch)
0N/A {
0N/A int offset = getCodePointOffset(ch);
1999N/A return (offset >= 0) ? m_data_[offset] : m_initialValue_;
2146N/A }
2146N/A
2146N/A /**
2146N/A * Gets the value to the data which this lead surrogate character points
2146N/A * to.
2146N/A * Returned data may contain folding offset information for the next
2146N/A * trailing surrogate character.
1999N/A * This method does not guarantee correct results for trail surrogates.
1173N/A * @param ch lead surrogate character
0N/A * @return data value
0N/A * @draft 2.1
0N/A */
0N/A public final int getLeadValue(char ch)
0N/A {
0N/A return m_data_[getLeadOffset(ch)];
0N/A }
1999N/A
1999N/A /**
1999N/A * Get a value from a folding offset (from the value of a lead surrogate)
1999N/A * and a trail surrogate.
2146N/A * @param leadvalue the value of a lead surrogate that contains the
2146N/A * folding offset
1999N/A * @param trail surrogate
0N/A * @return trie data value associated with the trail character
0N/A * @draft 2.1
0N/A */
0N/A public final int getTrailValue(int leadvalue, char trail)
0N/A {
0N/A if (m_dataManipulate_ == null) {
0N/A throw new NullPointerException(
0N/A "The field DataManipulate in this Trie is null");
1999N/A }
1999N/A int offset = m_dataManipulate_.getFoldingOffset(leadvalue);
1999N/A if (offset > 0) {
1999N/A return m_data_[getRawOffset(offset,
0N/A (char)(trail & SURROGATE_MASK_))];
0N/A }
0N/A return m_initialValue_;
0N/A }
0N/A
1999N/A // protected methods -----------------------------------------------
1999N/A
1999N/A /**
1173N/A * <p>Parses the input stream and stores its trie content into a index and
0N/A * data array</p>
0N/A * @param inputStream data input stream containing trie data
0N/A * @exception IOException thrown when data reading fails
0N/A */
0N/A protected final void unserialize(InputStream inputStream)
0N/A throws IOException
0N/A {
0N/A super.unserialize(inputStream);
0N/A // one used for initial value
0N/A m_data_ = new int[m_dataLength_];
0N/A DataInputStream input = new DataInputStream(inputStream);
0N/A for (int i = 0; i < m_dataLength_; i ++) {
0N/A m_data_[i] = input.readInt();
0N/A }
0N/A m_initialValue_ = m_data_[0];
0N/A }
0N/A
0N/A /**
0N/A * Gets the offset to the data which the surrogate pair points to.
0N/A * @param lead lead surrogate
0N/A * @param trail trailing surrogate
0N/A * @return offset to data
1999N/A * @draft 2.1
1999N/A */
1999N/A protected final int getSurrogateOffset(char lead, char trail)
1173N/A {
0N/A if (m_dataManipulate_ == null) {
0N/A throw new NullPointerException(
0N/A "The field DataManipulate in this Trie is null");
1173N/A }
1173N/A // get fold position for the next trail surrogate
1173N/A int offset = m_dataManipulate_.getFoldingOffset(getLeadValue(lead));
1173N/A
1173N/A // get the real data from the folded lead/trail units
1173N/A if (offset > 0) {
1173N/A return getRawOffset(offset, (char)(trail & SURROGATE_MASK_));
1173N/A }
0N/A
0N/A // return -1 if there is an error, in this case we return the default
1999N/A // value: m_initialValue_
1999N/A return -1;
1999N/A }
1173N/A
0N/A /**
0N/A * Gets the value at the argument index.
0N/A * For use internally in TrieIterator
0N/A * @param index value at index will be retrieved
0N/A * @return 32 bit value
0N/A * @see com.ibm.icu.impl.TrieIterator
0N/A * @draft 2.1
0N/A */
0N/A protected final int getValue(int index)
0N/A {
0N/A return m_data_[index];
1999N/A }
1999N/A
1999N/A /**
1173N/A * Gets the default initial value
0N/A * @return 32 bit value
0N/A * @draft 2.1
0N/A */
1173N/A protected final int getInitialValue()
1173N/A {
1173N/A return m_initialValue_;
1173N/A }
1173N/A
1173N/A // package private methods -----------------------------------------
1173N/A
1173N/A /**
0N/A * Internal constructor for builder use
0N/A * @param index the index array to be slotted into this trie
1999N/A * @param data the data array to be slotted into this trie
1999N/A * @param initialvalue the initial value for this trie
1999N/A * @param options trie options to use
1999N/A * @param datamanipulate folding implementation
0N/A */
0N/A IntTrie(char index[], int data[], int initialvalue, int options,
0N/A DataManipulate datamanipulate)
0N/A {
0N/A super(index, options, datamanipulate);
0N/A m_data_ = data;
0N/A m_dataLength_ = m_data_.length;
0N/A m_initialValue_ = initialvalue;
0N/A }
0N/A
1173N/A // private data members --------------------------------------------
1173N/A
1173N/A /**
1173N/A * Default value
1173N/A */
0N/A private int m_initialValue_;
0N/A /**
0N/A * Array of char data
0N/A */
0N/A private int m_data_[];
0N/A}
0N/A