src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/util/CharArrayIntMap.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2004, 2011, 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  * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    26  */
    28 package com.sun.xml.internal.fastinfoset.util;
    30 import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
    32 public class CharArrayIntMap extends KeyIntMap {
    34     private CharArrayIntMap _readOnlyMap;
    36     // Total character count of Map
    37     protected int _totalCharacterCount;
    39     static class Entry extends BaseEntry {
    40         final char[] _ch;
    41         final int _start;
    42         final int _length;
    43         Entry _next;
    45         public Entry(char[] ch, int start, int length, int hash, int value, Entry next) {
    46             super(hash, value);
    47             _ch = ch;
    48             _start = start;
    49             _length = length;
    50             _next = next;
    51         }
    53         public final boolean equalsCharArray(char[] ch, int start, int length) {
    54             if (_length == length) {
    55                 int n = _length;
    56                 int i = _start;
    57                 int j = start;
    58                 while (n-- != 0) {
    59                     if (_ch[i++] != ch[j++])
    60                         return false;
    61                 }
    62                 return true;
    63             }
    65             return false;
    66         }
    68     }
    70     private Entry[] _table;
    72     public CharArrayIntMap(int initialCapacity, float loadFactor) {
    73         super(initialCapacity, loadFactor);
    75         _table = new Entry[_capacity];
    76     }
    78     public CharArrayIntMap(int initialCapacity) {
    79         this(initialCapacity, DEFAULT_LOAD_FACTOR);
    80     }
    82     public CharArrayIntMap() {
    83         this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
    84     }
    86     public final void clear() {
    87         for (int i = 0; i < _table.length; i++) {
    88             _table[i] = null;
    89         }
    90         _size = 0;
    91         _totalCharacterCount = 0;
    92     }
    94     public final void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) {
    95         if (!(readOnlyMap instanceof CharArrayIntMap)) {
    96             throw new IllegalArgumentException(CommonResourceBundle.getInstance().
    97                     getString("message.illegalClass", new Object[]{readOnlyMap}));
    98         }
   100         setReadOnlyMap((CharArrayIntMap)readOnlyMap, clear);
   101     }
   103     public final void setReadOnlyMap(CharArrayIntMap readOnlyMap, boolean clear) {
   104         _readOnlyMap = readOnlyMap;
   105         if (_readOnlyMap != null) {
   106             _readOnlyMapSize = _readOnlyMap.size();
   108             if (clear) {
   109                 clear();
   110             }
   111         }  else {
   112             _readOnlyMapSize = 0;
   113         }
   114     }
   116     /**
   117      * Method returns an index of the passed character buffer in
   118      * <code>CharArrayIntMap</code>.
   119      *
   120      * @return index of character buffer in <code>CharArrayIntMap</code>,
   121      * otherwise NOT_PRESENT.
   122      */
   123     public final int get(char[] ch, int start, int length) {
   124         final int hash = hashHash(CharArray.hashCode(ch, start, length));
   125         return get(ch, start, length, hash);
   126     }
   128     /**
   129      * Method returns an index of the passed character buffer in
   130      * <code>CharArrayIntMap</code>. If character buffer is not in
   131      * <code>CharArrayIntMap</code> - it will be added.
   132      *
   133      * @return index of character buffer in <code>CharArrayIntMap</code>, or
   134      * NOT_PRESENT if character buffer was just added.
   135      */
   136     public final int obtainIndex(char[] ch, int start, int length, boolean clone) {
   137         final int hash = hashHash(CharArray.hashCode(ch, start, length));
   139         if (_readOnlyMap != null) {
   140             final int index = _readOnlyMap.get(ch, start, length, hash);
   141             if (index != -1) {
   142                 return index;
   143             }
   144         }
   146         final int tableIndex = indexFor(hash, _table.length);
   147         for (Entry e = _table[tableIndex]; e != null; e = e._next) {
   148             if (e._hash == hash && e.equalsCharArray(ch, start, length)) {
   149                 return e._value;
   150             }
   151         }
   153         if (clone) {
   154             char[] chClone = new char[length];
   155             System.arraycopy(ch, start, chClone, 0, length);
   157             ch = chClone;
   158             start = 0;
   159         }
   161         addEntry(ch, start, length, hash, _size + _readOnlyMapSize, tableIndex);
   162         return NOT_PRESENT;
   163     }
   165     public final int getTotalCharacterCount() {
   166         return _totalCharacterCount;
   167     }
   169     private final int get(char[] ch, int start, int length, int hash) {
   170         if (_readOnlyMap != null) {
   171             final int i = _readOnlyMap.get(ch, start, length, hash);
   172             if (i != -1) {
   173                 return i;
   174             }
   175         }
   177         final int tableIndex = indexFor(hash, _table.length);
   178         for (Entry e = _table[tableIndex]; e != null; e = e._next) {
   179             if (e._hash == hash && e.equalsCharArray(ch, start, length)) {
   180                 return e._value;
   181             }
   182         }
   184         return NOT_PRESENT;
   185     }
   187     private final void addEntry(char[] ch, int start, int length, int hash, int value, int bucketIndex) {
   188         Entry e = _table[bucketIndex];
   189         _table[bucketIndex] = new Entry(ch, start, length, hash, value, e);
   190         _totalCharacterCount += length;
   191                 if (_size++ >= _threshold) {
   192             resize(2 * _table.length);
   193         }
   194     }
   196     private final void resize(int newCapacity) {
   197         _capacity = newCapacity;
   198         Entry[] oldTable = _table;
   199         int oldCapacity = oldTable.length;
   200         if (oldCapacity == MAXIMUM_CAPACITY) {
   201             _threshold = Integer.MAX_VALUE;
   202             return;
   203         }
   205         Entry[] newTable = new Entry[_capacity];
   206         transfer(newTable);
   207         _table = newTable;
   208         _threshold = (int)(_capacity * _loadFactor);
   209     }
   211     private final void transfer(Entry[] newTable) {
   212         Entry[] src = _table;
   213         int newCapacity = newTable.length;
   214         for (int j = 0; j < src.length; j++) {
   215             Entry e = src[j];
   216             if (e != null) {
   217                 src[j] = null;
   218                 do {
   219                     Entry next = e._next;
   220                     int i = indexFor(e._hash, newCapacity);
   221                     e._next = newTable[i];
   222                     newTable[i] = e;
   223                     e = next;
   224                 } while (e != null);
   225             }
   226         }
   227     }
   228 }

mercurial