KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sosnoski > util > hashmap > PrimitiveKeyBase


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 package com.sosnoski.util.hashmap;
24
25 import java.lang.reflect.Array JavaDoc;
26 import com.sosnoski.util.PrimitiveHashBase;
27
28 /**
29  * Base class for type-specific hash map classes with primitive keys. This
30  * class builds on the basic structure provided by
31  * <code>PrimitiveHashBase</code>, specializing it for the case of a hash map
32  * where a data value is associated with each key. See the base class
33  * description for details of the implementation.<p>
34  *
35  * Hash sets based on this class are unsynchronized in order to provide the
36  * best possible performance for typical usage scenarios, so explicit
37  * synchronization must be implemented by the subclass or the application in
38  * cases where they are to be modified in a multithreaded environment.<p>
39  *
40  * Subclasses need to implement the abstract methods defined by the base class
41  * for working with the key array, and by this class for working with the value
42  * array and for restructuring, as well as the actual data access methods (at
43  * least the basic <code>add()</code>, <code>containsKey()</code>,
44  * <code>get()</code>, and <code>remove()</code> methods).
45  *
46  * @see com.sosnoski.util.PrimitiveHashBase
47  * @author Dennis M. Sosnoski
48  * @version 1.0
49  */

50
51 public abstract class PrimitiveKeyBase extends PrimitiveHashBase
52 {
53     /**
54      * Constructor with full specification.
55      *
56      * @param count number of values to assume in initial sizing of table
57      * @param fill fraction full allowed for table before growing
58      * @param ktype type of primitives used for keys
59      * @param vtype type of primitives or objects used for values
60      */

61     
62     public PrimitiveKeyBase(int count, double fill, Class JavaDoc ktype, Class JavaDoc vtype) {
63         super(count, fill, ktype);
64         setValueArray(Array.newInstance(vtype, m_flagTable.length));
65     }
66
67     /**
68      * Copy (clone) constructor.
69      *
70      * @param base instance being copied
71      */

72     
73     public PrimitiveKeyBase(PrimitiveKeyBase base) {
74         super(base);
75         int size = base.m_flagTable.length;
76         Class JavaDoc type = base.getValueArray().getClass().getComponentType();
77         Object JavaDoc copy = Array.newInstance(type, size);
78         System.arraycopy(base.getValueArray(), 0, copy, 0, size);
79         setValueArray(copy);
80     }
81
82     /**
83      * Get the backing array of values. This method is used by the type-agnostic
84      * base class code to access the array used for type-specific storage by
85      * the child class.
86      *
87      * @return backing key array object
88      */

89     
90     protected abstract Object JavaDoc getValueArray();
91
92     /**
93      * Set the backing array of values. This method is used by the type-agnostic
94      * base class code to set the array used for type-specific storage by the
95      * child class.
96      *
97      * @param array backing value array object
98      */

99     
100     protected abstract void setValueArray(Object JavaDoc array);
101
102     /**
103      * Restructure the table. This abstract method is used when the table
104      * is increasing or decreasing in size, and works directly with the old
105      * table representation arrays. It should insert pairs from the old arrays
106      * directly into the table without adjusting the count present or checking
107      * the table size.
108      *
109      * @param flags array of flags for array slots used
110      * @param karray array of keys
111      * @param varray array of values
112      */

113     
114     protected abstract void restructure(boolean[] flags, Object JavaDoc karray,
115         Object JavaDoc varray);
116
117     /**
118      * Resize the base arrays after a size change. This implementation of the
119      * abstract base class method allocates the new arrays and then calls
120      * another method for handling the actual transfer of the keys and values
121      * from the old arrays to the new ones.
122      *
123      * @param size new size for base arrays
124      */

125     
126     protected void reallocate(int size) {
127     
128         // allocate the larger arrays
129
boolean[] flags = m_flagTable;
130         m_flagTable = new boolean[size];
131         Object JavaDoc keys = getKeyArray();;
132         Class JavaDoc type = keys.getClass().getComponentType();
133         setKeyArray(Array.newInstance(type, size));
134         Object JavaDoc values = getValueArray();
135         type = values.getClass().getComponentType();
136         setValueArray(Array.newInstance(type, size));
137         
138         // reinsert all entries into new arrays
139
restructure(flags, keys, values);
140     }
141
142     /**
143      * Set the table to the empty state. This override of the base class method
144      * first calls the base class method to clear the entry information, then
145      * checks if the hash map values are objects, and if so clears all
146      * references to these objects.
147      */

148     
149     public void clear() {
150         super.clear();
151         Object JavaDoc values = getValueArray();
152         if (!values.getClass().getComponentType().isPrimitive()) {
153             Object JavaDoc[] objects = (Object JavaDoc[])values;
154             for (int i = 0; i < objects.length; i++) {
155                 objects[i] = null;
156             }
157         }
158     }
159 }
160
Popular Tags