Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/text/normalizer/IntTrie.java
38830 views
/*1* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*******************************************************************************27* (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved *28* *29* The original version of this source code and documentation is copyrighted *30* and owned by IBM, These materials are provided under terms of a License *31* Agreement between IBM and Sun. This technology is protected by multiple *32* US and International patents. This notice and attribution to IBM may not *33* to removed. *34*******************************************************************************35*/3637package sun.text.normalizer;3839import java.io.InputStream;40import java.io.DataInputStream;41import java.io.IOException;42import java.util.Arrays;4344/**45* Trie implementation which stores data in int, 32 bits.46* @author synwee47* @see com.ibm.icu.impl.Trie48* @since release 2.1, Jan 01 200249*/50public class IntTrie extends Trie51{52// public constructors ---------------------------------------------5354/**55* <p>Creates a new Trie with the settings for the trie data.</p>56* <p>Unserialize the 32-bit-aligned input stream and use the data for the57* trie.</p>58* @param inputStream file input stream to a ICU data file, containing59* the trie60* @param dataManipulate object which provides methods to parse the char61* data62* @throws IOException thrown when data reading fails63* @draft 2.164*/65public IntTrie(InputStream inputStream, DataManipulate datamanipulate)66throws IOException67{68super(inputStream, datamanipulate);69if (!isIntTrie()) {70throw new IllegalArgumentException(71"Data given does not belong to a int trie.");72}73}7475// public methods --------------------------------------------------7677/**78* Gets the value associated with the codepoint.79* If no value is associated with the codepoint, a default value will be80* returned.81* @param ch codepoint82* @return offset to data83* @draft 2.184*/85public final int getCodePointValue(int ch)86{87int offset = getCodePointOffset(ch);88return (offset >= 0) ? m_data_[offset] : m_initialValue_;89}9091/**92* Gets the value to the data which this lead surrogate character points93* to.94* Returned data may contain folding offset information for the next95* trailing surrogate character.96* This method does not guarantee correct results for trail surrogates.97* @param ch lead surrogate character98* @return data value99* @draft 2.1100*/101public final int getLeadValue(char ch)102{103return m_data_[getLeadOffset(ch)];104}105106/**107* Get a value from a folding offset (from the value of a lead surrogate)108* and a trail surrogate.109* @param leadvalue the value of a lead surrogate that contains the110* folding offset111* @param trail surrogate112* @return trie data value associated with the trail character113* @draft 2.1114*/115public final int getTrailValue(int leadvalue, char trail)116{117if (m_dataManipulate_ == null) {118throw new NullPointerException(119"The field DataManipulate in this Trie is null");120}121int offset = m_dataManipulate_.getFoldingOffset(leadvalue);122if (offset > 0) {123return m_data_[getRawOffset(offset,124(char)(trail & SURROGATE_MASK_))];125}126return m_initialValue_;127}128129// protected methods -----------------------------------------------130131/**132* <p>Parses the input stream and stores its trie content into a index and133* data array</p>134* @param inputStream data input stream containing trie data135* @exception IOException thrown when data reading fails136*/137protected final void unserialize(InputStream inputStream)138throws IOException139{140super.unserialize(inputStream);141// one used for initial value142m_data_ = new int[m_dataLength_];143DataInputStream input = new DataInputStream(inputStream);144for (int i = 0; i < m_dataLength_; i ++) {145m_data_[i] = input.readInt();146}147m_initialValue_ = m_data_[0];148}149150/**151* Gets the offset to the data which the surrogate pair points to.152* @param lead lead surrogate153* @param trail trailing surrogate154* @return offset to data155* @draft 2.1156*/157protected final int getSurrogateOffset(char lead, char trail)158{159if (m_dataManipulate_ == null) {160throw new NullPointerException(161"The field DataManipulate in this Trie is null");162}163// get fold position for the next trail surrogate164int offset = m_dataManipulate_.getFoldingOffset(getLeadValue(lead));165166// get the real data from the folded lead/trail units167if (offset > 0) {168return getRawOffset(offset, (char)(trail & SURROGATE_MASK_));169}170171// return -1 if there is an error, in this case we return the default172// value: m_initialValue_173return -1;174}175176/**177* Gets the value at the argument index.178* For use internally in TrieIterator179* @param index value at index will be retrieved180* @return 32 bit value181* @see com.ibm.icu.impl.TrieIterator182* @draft 2.1183*/184protected final int getValue(int index)185{186return m_data_[index];187}188189/**190* Gets the default initial value191* @return 32 bit value192* @draft 2.1193*/194protected final int getInitialValue()195{196return m_initialValue_;197}198199// package private methods -----------------------------------------200201/**202* Internal constructor for builder use203* @param index the index array to be slotted into this trie204* @param data the data array to be slotted into this trie205* @param initialvalue the initial value for this trie206* @param options trie options to use207* @param datamanipulate folding implementation208*/209IntTrie(char index[], int data[], int initialvalue, int options,210DataManipulate datamanipulate)211{212super(index, options, datamanipulate);213m_data_ = data;214m_dataLength_ = m_data_.length;215m_initialValue_ = initialvalue;216}217218// private data members --------------------------------------------219220/**221* Default value222*/223private int m_initialValue_;224/**225* Array of char data226*/227private int m_data_[];228}229230231