View Javadoc
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   */
27  
28  package com.sun.xml.internal.fastinfoset.util;
29  
30  import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
31  
32  public class FixedEntryStringIntMap extends StringIntMap {
33  
34      private Entry _fixedEntry;
35  
36      public FixedEntryStringIntMap(String fixedEntry, int initialCapacity, float loadFactor) {
37          super(initialCapacity, loadFactor);
38  
39          // Add the fixed entry
40          final int hash = hashHash(fixedEntry.hashCode());
41          final int tableIndex = indexFor(hash, _table.length);
42          _table[tableIndex] = _fixedEntry = new Entry(fixedEntry, hash, _index++, null);
43          if (_size++ >= _threshold) {
44              resize(2 * _table.length);
45          }
46      }
47  
48      public FixedEntryStringIntMap(String fixedEntry, int initialCapacity) {
49          this(fixedEntry, initialCapacity, DEFAULT_LOAD_FACTOR);
50      }
51  
52      public FixedEntryStringIntMap(String fixedEntry) {
53          this(fixedEntry, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
54      }
55  
56      public final void clear() {
57          for (int i = 0; i < _table.length; i++) {
58              _table[i] = null;
59          }
60          _lastEntry = NULL_ENTRY;
61  
62          if (_fixedEntry != null) {
63              final int tableIndex = indexFor(_fixedEntry._hash, _table.length);
64              _table[tableIndex] = _fixedEntry;
65              _fixedEntry._next = null;
66              _size = 1;
67              _index = _readOnlyMapSize + 1;
68          } else {
69              _size = 0;
70              _index = _readOnlyMapSize;
71          }
72      }
73  
74      public final void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) {
75          if (!(readOnlyMap instanceof FixedEntryStringIntMap)) {
76              throw new IllegalArgumentException(CommonResourceBundle.getInstance().
77                      getString("message.illegalClass", new Object[]{readOnlyMap}));
78          }
79  
80          setReadOnlyMap((FixedEntryStringIntMap)readOnlyMap, clear);
81      }
82  
83      public final void setReadOnlyMap(FixedEntryStringIntMap readOnlyMap, boolean clear) {
84          _readOnlyMap = readOnlyMap;
85          if (_readOnlyMap != null) {
86              readOnlyMap.removeFixedEntry();
87              _readOnlyMapSize = readOnlyMap.size();
88              _index = _readOnlyMapSize + _size;
89              if (clear) {
90                  clear();
91              }
92          }  else {
93              _readOnlyMapSize = 0;
94          }
95      }
96  
97      private final void removeFixedEntry() {
98          if (_fixedEntry != null) {
99              final int tableIndex = indexFor(_fixedEntry._hash, _table.length);
100             final Entry firstEntry = _table[tableIndex];
101             if (firstEntry == _fixedEntry) {
102                 _table[tableIndex] = _fixedEntry._next;
103             } else {
104                 Entry previousEntry = firstEntry;
105                 while (previousEntry._next != _fixedEntry) {
106                     previousEntry = previousEntry._next;
107                 }
108                 previousEntry._next = _fixedEntry._next;
109             }
110 
111             _fixedEntry = null;
112             _size--;
113         }
114     }
115 }