Showing the changes to java/pkg/sun/text/normalizer/IntTrie.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,229 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | /* | ||
| 27 | ******************************************************************************* | ||
| 28 | * (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved * | ||
| 29 | * * | ||
| 30 | * The original version of this source code and documentation is copyrighted * | ||
| 31 | * and owned by IBM, These materials are provided under terms of a License * | ||
| 32 | * Agreement between IBM and Sun. This technology is protected by multiple * | ||
| 33 | * US and International patents. This notice and attribution to IBM may not * | ||
| 34 | * to removed. * | ||
| 35 | ******************************************************************************* | ||
| 36 | */ | ||
| 37 | |||
| 38 | package sun.text.normalizer; | ||
| 39 | |||
| 40 | import java.io.InputStream; | ||
| 41 | import java.io.DataInputStream; | ||
| 42 | import java.io.IOException; | ||
| 43 | import java.util.Arrays; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * Trie implementation which stores data in int, 32 bits. | ||
| 47 | * @author synwee | ||
| 48 | * @see com.ibm.icu.impl.Trie | ||
| 49 | * @since release 2.1, Jan 01 2002 | ||
| 50 | */ | ||
| 51 | public class IntTrie extends Trie | ||
| 52 | { | ||
| 53 | // public constructors --------------------------------------------- | ||
| 54 | |||
| 55 | /** | ||
| 56 | * <p>Creates a new Trie with the settings for the trie data.</p> | ||
| 57 | * <p>Unserialize the 32-bit-aligned input stream and use the data for the | ||
| 58 | * trie.</p> | ||
| 59 | * @param inputStream file input stream to a ICU data file, containing | ||
| 60 | * the trie | ||
| 61 | * @param dataManipulate object which provides methods to parse the char | ||
| 62 | * data | ||
| 63 | * @throws IOException thrown when data reading fails | ||
| 64 | * @draft 2.1 | ||
| 65 | */ | ||
| 66 | public IntTrie(InputStream inputStream, DataManipulate datamanipulate) | ||
| 67 | throws IOException | ||
| 68 | { | ||
| 69 | super(inputStream, datamanipulate); | ||
| 70 | if (!isIntTrie()) { | ||
| 71 | throw new IllegalArgumentException( | ||
| 72 | "Data given does not belong to a int trie."); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | // public methods -------------------------------------------------- | ||
| 77 | |||
| 78 | /** | ||
| 79 | * Gets the value associated with the codepoint. | ||
| 80 | * If no value is associated with the codepoint, a default value will be | ||
| 81 | * returned. | ||
| 82 | * @param ch codepoint | ||
| 83 | * @return offset to data | ||
| 84 | * @draft 2.1 | ||
| 85 | */ | ||
| 86 | public final int getCodePointValue(int ch) | ||
| 87 | { | ||
| 88 | int offset = getCodePointOffset(ch); | ||
| 89 | return (offset >= 0) ? m_data_[offset] : m_initialValue_; | ||
| 90 | } | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Gets the value to the data which this lead surrogate character points | ||
| 94 | * to. | ||
| 95 | * Returned data may contain folding offset information for the next | ||
| 96 | * trailing surrogate character. | ||
| 97 | * This method does not guarantee correct results for trail surrogates. | ||
| 98 | * @param ch lead surrogate character | ||
| 99 | * @return data value | ||
| 100 | * @draft 2.1 | ||
| 101 | */ | ||
| 102 | public final int getLeadValue(char ch) | ||
| 103 | { | ||
| 104 | return m_data_[getLeadOffset(ch)]; | ||
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Get a value from a folding offset (from the value of a lead surrogate) | ||
| 109 | * and a trail surrogate. | ||
| 110 | * @param leadvalue the value of a lead surrogate that contains the | ||
| 111 | * folding offset | ||
| 112 | * @param trail surrogate | ||
| 113 | * @return trie data value associated with the trail character | ||
| 114 | * @draft 2.1 | ||
| 115 | */ | ||
| 116 | public final int getTrailValue(int leadvalue, char trail) | ||
| 117 | { | ||
| 118 | if (m_dataManipulate_ == null) { | ||
| 119 | throw new NullPointerException( | ||
| 120 | "The field DataManipulate in this Trie is null"); | ||
| 121 | } | ||
| 122 | int offset = m_dataManipulate_.getFoldingOffset(leadvalue); | ||
| 123 | if (offset > 0) { | ||
| 124 | return m_data_[getRawOffset(offset, | ||
| 125 | (char)(trail & SURROGATE_MASK_))]; | ||
| 126 | } | ||
| 127 | return m_initialValue_; | ||
| 128 | } | ||
| 129 | |||
| 130 | // protected methods ----------------------------------------------- | ||
| 131 | |||
| 132 | /** | ||
| 133 | * <p>Parses the input stream and stores its trie content into a index and | ||
| 134 | * data array</p> | ||
| 135 | * @param inputStream data input stream containing trie data | ||
| 136 | * @exception IOException thrown when data reading fails | ||
| 137 | */ | ||
| 138 | protected final void unserialize(InputStream inputStream) | ||
| 139 | throws IOException | ||
| 140 | { | ||
| 141 | super.unserialize(inputStream); | ||
| 142 | // one used for initial value | ||
| 143 | m_data_ = new int[m_dataLength_]; | ||
| 144 | DataInputStream input = new DataInputStream(inputStream); | ||
| 145 | for (int i = 0; i < m_dataLength_; i ++) { | ||
| 146 | m_data_[i] = input.readInt(); | ||
| 147 | } | ||
| 148 | m_initialValue_ = m_data_[0]; | ||
| 149 | } | ||
| 150 | |||
| 151 | /** | ||
| 152 | * Gets the offset to the data which the surrogate pair points to. | ||
| 153 | * @param lead lead surrogate | ||
| 154 | * @param trail trailing surrogate | ||
| 155 | * @return offset to data | ||
| 156 | * @draft 2.1 | ||
| 157 | */ | ||
| 158 | protected final int getSurrogateOffset(char lead, char trail) | ||
| 159 | { | ||
| 160 | if (m_dataManipulate_ == null) { | ||
| 161 | throw new NullPointerException( | ||
| 162 | "The field DataManipulate in this Trie is null"); | ||
| 163 | } | ||
| 164 | // get fold position for the next trail surrogate | ||
| 165 | int offset = m_dataManipulate_.getFoldingOffset(getLeadValue(lead)); | ||
| 166 | |||
| 167 | // get the real data from the folded lead/trail units | ||
| 168 | if (offset > 0) { | ||
| 169 | return getRawOffset(offset, (char)(trail & SURROGATE_MASK_)); | ||
| 170 | } | ||
| 171 | |||
| 172 | // return -1 if there is an error, in this case we return the default | ||
| 173 | // value: m_initialValue_ | ||
| 174 | return -1; | ||
| 175 | } | ||
| 176 | |||
| 177 | /** | ||
| 178 | * Gets the value at the argument index. | ||
| 179 | * For use internally in TrieIterator | ||
| 180 | * @param index value at index will be retrieved | ||
| 181 | * @return 32 bit value | ||
| 182 | * @see com.ibm.icu.impl.TrieIterator | ||
| 183 | * @draft 2.1 | ||
| 184 | */ | ||
| 185 | protected final int getValue(int index) | ||
| 186 | { | ||
| 187 | return m_data_[index]; | ||
| 188 | } | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Gets the default initial value | ||
| 192 | * @return 32 bit value | ||
| 193 | * @draft 2.1 | ||
| 194 | */ | ||
| 195 | protected final int getInitialValue() | ||
| 196 | { | ||
| 197 | return m_initialValue_; | ||
| 198 | } | ||
| 199 | |||
| 200 | // package private methods ----------------------------------------- | ||
| 201 | |||
| 202 | /** | ||
| 203 | * Internal constructor for builder use | ||
| 204 | * @param index the index array to be slotted into this trie | ||
| 205 | * @param data the data array to be slotted into this trie | ||
| 206 | * @param initialvalue the initial value for this trie | ||
| 207 | * @param options trie options to use | ||
| 208 | * @param datamanipulate folding implementation | ||
| 209 | */ | ||
| 210 | IntTrie(char index[], int data[], int initialvalue, int options, | ||
| 211 | DataManipulate datamanipulate) | ||
| 212 | { | ||
| 213 | super(index, options, datamanipulate); | ||
| 214 | m_data_ = data; | ||
| 215 | m_dataLength_ = m_data_.length; | ||
| 216 | m_initialValue_ = initialvalue; | ||
| 217 | } | ||
| 218 | |||
| 219 | // private data members -------------------------------------------- | ||
| 220 | |||
| 221 | /** | ||
| 222 | * Default value | ||
| 223 | */ | ||
| 224 | private int m_initialValue_; | ||
| 225 | /** | ||
| 226 | * Array of char data | ||
| 227 | */ | ||
| 228 | private int m_data_[]; | ||
| 229 | } | ||