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

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/util/StringIntMap.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,216 @@
     1.4 +/*
     1.5 + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + *
    1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    1.29 + */
    1.30 +
    1.31 +package com.sun.xml.internal.fastinfoset.util;
    1.32 +
    1.33 +import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
    1.34 +
    1.35 +public class StringIntMap extends KeyIntMap {
    1.36 +    protected static final Entry NULL_ENTRY = new Entry(null, 0, -1, null);
    1.37 +
    1.38 +    protected StringIntMap _readOnlyMap;
    1.39 +
    1.40 +    protected static class Entry extends BaseEntry {
    1.41 +        final String _key;
    1.42 +        Entry _next;
    1.43 +
    1.44 +        public Entry(String key, int hash, int value, Entry next) {
    1.45 +            super(hash, value);
    1.46 +            _key = key;
    1.47 +            _next = next;
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51 +    protected Entry _lastEntry = NULL_ENTRY;
    1.52 +
    1.53 +    protected Entry[] _table;
    1.54 +
    1.55 +    protected int _index;
    1.56 +
    1.57 +    // Total character count of Map
    1.58 +    protected int _totalCharacterCount;
    1.59 +
    1.60 +    public StringIntMap(int initialCapacity, float loadFactor) {
    1.61 +        super(initialCapacity, loadFactor);
    1.62 +
    1.63 +        _table = new Entry[_capacity];
    1.64 +    }
    1.65 +
    1.66 +    public StringIntMap(int initialCapacity) {
    1.67 +        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    1.68 +    }
    1.69 +
    1.70 +    public StringIntMap() {
    1.71 +        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
    1.72 +    }
    1.73 +
    1.74 +    public void clear() {
    1.75 +        for (int i = 0; i < _table.length; i++) {
    1.76 +            _table[i] = null;
    1.77 +        }
    1.78 +        _lastEntry = NULL_ENTRY;
    1.79 +        _size = 0;
    1.80 +        _index = _readOnlyMapSize;
    1.81 +        _totalCharacterCount = 0;
    1.82 +    }
    1.83 +
    1.84 +    public void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) {
    1.85 +        if (!(readOnlyMap instanceof StringIntMap)) {
    1.86 +            throw new IllegalArgumentException(CommonResourceBundle.getInstance().
    1.87 +                    getString("message.illegalClass", new Object[]{readOnlyMap}));
    1.88 +        }
    1.89 +
    1.90 +        setReadOnlyMap((StringIntMap)readOnlyMap, clear);
    1.91 +    }
    1.92 +
    1.93 +    public final void setReadOnlyMap(StringIntMap readOnlyMap, boolean clear) {
    1.94 +        _readOnlyMap = readOnlyMap;
    1.95 +        if (_readOnlyMap != null) {
    1.96 +            _readOnlyMapSize = _readOnlyMap.size();
    1.97 +            _index = _size + _readOnlyMapSize;
    1.98 +
    1.99 +            if (clear) {
   1.100 +                clear();
   1.101 +            }
   1.102 +        }  else {
   1.103 +            _readOnlyMapSize = 0;
   1.104 +            _index = _size;
   1.105 +        }
   1.106 +    }
   1.107 +
   1.108 +    public final int getNextIndex() {
   1.109 +        return _index++;
   1.110 +    }
   1.111 +
   1.112 +    public final int getIndex() {
   1.113 +        return _index;
   1.114 +    }
   1.115 +
   1.116 +    public final int obtainIndex(String key) {
   1.117 +        final int hash = hashHash(key.hashCode());
   1.118 +
   1.119 +        if (_readOnlyMap != null) {
   1.120 +            final int index = _readOnlyMap.get(key, hash);
   1.121 +            if (index != -1) {
   1.122 +                return index;
   1.123 +            }
   1.124 +        }
   1.125 +
   1.126 +        final int tableIndex = indexFor(hash, _table.length);
   1.127 +        for (Entry e = _table[tableIndex]; e != null; e = e._next) {
   1.128 +            if (e._hash == hash && eq(key, e._key)) {
   1.129 +                return e._value;
   1.130 +            }
   1.131 +        }
   1.132 +
   1.133 +        addEntry(key, hash, tableIndex);
   1.134 +        return NOT_PRESENT;
   1.135 +    }
   1.136 +
   1.137 +    public final void add(String key) {
   1.138 +        final int hash = hashHash(key.hashCode());
   1.139 +        final int tableIndex = indexFor(hash, _table.length);
   1.140 +        addEntry(key, hash, tableIndex);
   1.141 +    }
   1.142 +
   1.143 +    public final int get(String key) {
   1.144 +        if (key == _lastEntry._key)
   1.145 +            return _lastEntry._value;
   1.146 +
   1.147 +        return get(key, hashHash(key.hashCode()));
   1.148 +    }
   1.149 +
   1.150 +    public final int getTotalCharacterCount() {
   1.151 +        return _totalCharacterCount;
   1.152 +    }
   1.153 +
   1.154 +    private final int get(String key, int hash) {
   1.155 +        if (_readOnlyMap != null) {
   1.156 +            final int i = _readOnlyMap.get(key, hash);
   1.157 +            if (i != -1) {
   1.158 +                return i;
   1.159 +            }
   1.160 +        }
   1.161 +
   1.162 +        final int tableIndex = indexFor(hash, _table.length);
   1.163 +        for (Entry e = _table[tableIndex]; e != null; e = e._next) {
   1.164 +            if (e._hash == hash && eq(key, e._key)) {
   1.165 +                _lastEntry = e;
   1.166 +                return e._value;
   1.167 +            }
   1.168 +        }
   1.169 +
   1.170 +        return NOT_PRESENT;
   1.171 +    }
   1.172 +
   1.173 +
   1.174 +    private final void addEntry(String key, int hash, int bucketIndex) {
   1.175 +        Entry e = _table[bucketIndex];
   1.176 +        _table[bucketIndex] = new Entry(key, hash, _index++, e);
   1.177 +        _totalCharacterCount += key.length();
   1.178 +        if (_size++ >= _threshold) {
   1.179 +            resize(2 * _table.length);
   1.180 +        }
   1.181 +    }
   1.182 +
   1.183 +    protected final void resize(int newCapacity) {
   1.184 +        _capacity = newCapacity;
   1.185 +        Entry[] oldTable = _table;
   1.186 +        int oldCapacity = oldTable.length;
   1.187 +        if (oldCapacity == MAXIMUM_CAPACITY) {
   1.188 +            _threshold = Integer.MAX_VALUE;
   1.189 +            return;
   1.190 +        }
   1.191 +
   1.192 +        Entry[] newTable = new Entry[_capacity];
   1.193 +        transfer(newTable);
   1.194 +        _table = newTable;
   1.195 +        _threshold = (int)(_capacity * _loadFactor);
   1.196 +    }
   1.197 +
   1.198 +    private final void transfer(Entry[] newTable) {
   1.199 +        Entry[] src = _table;
   1.200 +        int newCapacity = newTable.length;
   1.201 +        for (int j = 0; j < src.length; j++) {
   1.202 +            Entry e = src[j];
   1.203 +            if (e != null) {
   1.204 +                src[j] = null;
   1.205 +                do {
   1.206 +                    Entry next = e._next;
   1.207 +                    int i = indexFor(e._hash, newCapacity);
   1.208 +                    e._next = newTable[i];
   1.209 +                    newTable[i] = e;
   1.210 +                    e = next;
   1.211 +                } while (e != null);
   1.212 +            }
   1.213 +        }
   1.214 +    }
   1.215 +
   1.216 +    private final boolean eq(String x, String y) {
   1.217 +        return x == y || x.equals(y);
   1.218 +    }
   1.219 +}

mercurial