KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > util > IVMap


1 /*
2  * $Id: IVMap.java,v 1.2 2002/02/15 23:44:28 skavish Exp $
3  *
4  * ===========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.util;
52
53 import java.io.*;
54 import java.util.*;
55
56 import org.openlaszlo.iv.flash.api.*;
57
58 /**
59  * Simple unsynchronized hashtable with integer keys.
60  *
61  * @author Dmitry Skavish
62  */

63 public final class IVMap {
64
65     private static final int DEFAULT_SIZE = 257;
66     private static final float DEFAULT_RATIO = 0.75f;
67
68     private MyNode[] buckets;
69     private int size;
70     private int limit;
71     private int length;
72
73     /**
74      * Create empty hashtable.
75      */

76     public IVMap() {
77         clear();
78     }
79
80     /**
81      * Add flash definition to the hashtable by it's ID.
82      *
83      * @param def flash definition to add
84      */

85     public void add( FlashDef def ) {
86         put( def.getID(), def );
87     }
88
89     /**
90      * Add flash definition to the hashtable by integer key.
91      *
92      * @param key object's key
93      * @param def flash definition to add
94      */

95     public void put( int key, FlashDef def ) {
96         int probe = key % length;
97         MyNode newNode = new MyNode( key, def );
98         newNode.next = buckets[probe];
99         buckets[probe] = newNode;
100         if( size++ > limit ) expand();
101     }
102
103     /**
104      * Retrieve flash definition by integer key.
105      *
106      * @param key defintion's key
107      * @return found flash defintion or null
108      */

109     public FlashDef get( int key ) {
110         for( MyNode node = buckets[key%length]; node != null; node = node.next ) {
111             if( key == node.key ) return node.value;
112         }
113         return null;
114     }
115
116     /**
117      * Number of objects in the hashtable.
118      *
119      * @return number of objects
120      */

121     public int size() {
122         return size;
123     }
124
125     /**
126      * Enumeration of all the values (FlashDef's) of the hashtable.
127      *
128      * @return values of the hashtable
129      */

130     public Enumeration values() {
131         return new Enumeration() {
132             int cur = 0;
133             MyNode node = null;
134             public boolean hasMoreElements() {
135                 while( node == null ) {
136                     if( cur >= length ) return false;
137                     node = buckets[cur++];
138                  }
139                  return true;
140              }
141              public Object JavaDoc nextElement() {
142                  if( !hasMoreElements() ) return null;
143                  Object JavaDoc value = node.value;
144                  node = node.next;
145                  return value;
146              }
147         };
148     }
149
150     /**
151      * Clear hashtable.
152      */

153     public void clear() {
154         length = DEFAULT_SIZE;
155         buckets = new MyNode[length];
156         size = 0;
157         limit = (int) (length*DEFAULT_RATIO);
158     }
159
160     protected void expand() {
161         length = length*2+1;
162         MyNode[] new_bucket = new MyNode[length];
163         for( int i=buckets.length; --i>=0; ) {
164             MyNode node = buckets[i];
165             while( node != null ) {
166                 MyNode cnode = node;
167                 node = node.next;
168                 int probe = cnode.key % length;
169                 cnode.next = new_bucket[probe];
170                 new_bucket[probe] = cnode;
171             }
172         }
173         limit = (int) (length*DEFAULT_RATIO);
174         buckets = new_bucket;
175     }
176
177     static class MyNode {
178         public int key;
179         public FlashDef value;
180         public MyNode next;
181
182         public MyNode( int key, FlashDef value ) {
183             this.key = key;;
184             this.value = value;
185         }
186     }
187
188 }
189
Popular Tags