KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > au > id > jericho > lib > html > IntStringHashMap


1 // Jericho HTML Parser - Java based library for analysing and manipulating HTML
2
// Version 2.2
3
// Copyright (C) 2006 Martin Jericho
4
// http://sourceforge.net/projects/jerichohtml/
5
//
6
// This library is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU Lesser General Public
8
// License as published by the Free Software Foundation; either
9
// version 2.1 of the License, or (at your option) any later version.
10
// http://www.gnu.org/copyleft/lesser.html
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20

21 package au.id.jericho.lib.html;
22
23 import java.util.*;
24
25 /**
26  * This is an internal class used to efficiently map integers to strings, which is used in the CharacterEntityReference class.
27  */

28 final class IntStringHashMap {
29     private static final int DEFAULT_INITIAL_CAPACITY=15;
30     private static final float DEFAULT_LOAD_FACTOR=0.75f;
31     private transient Entry[] entries; // length must always be a power of 2.
32
private transient int size;
33     private int threshold;
34     private float loadFactor;
35     private int bitmask; // always entries.length-1
36

37     public IntStringHashMap(int initialCapacity, final float loadFactor) {
38         this.loadFactor=loadFactor;
39         int capacity=1;
40         while (capacity<initialCapacity) capacity<<=1;
41         threshold=(int)(capacity*loadFactor);
42         entries=new Entry[capacity];
43         bitmask=capacity-1;
44     }
45
46     public IntStringHashMap(final int initialCapacity) {
47         this(initialCapacity,DEFAULT_LOAD_FACTOR);
48     }
49
50     public IntStringHashMap() {
51         this(DEFAULT_INITIAL_CAPACITY,DEFAULT_LOAD_FACTOR);
52     }
53
54     public int size() {
55         return size;
56     }
57
58     public boolean isEmpty() {
59         return size==0;
60     }
61
62     private int getIndex(final int key) {
63         return key&bitmask; // equivalent to (key%entries.length) but more efficient.
64
}
65
66     public String JavaDoc get(final int key) {
67         Entry entry=entries[getIndex(key)];
68         while (entry!=null) {
69             if (key==entry.key) return entry.value;
70             entry=entry.next;
71         }
72         return null;
73     }
74
75     private Entry getEntry(final int key) {
76         Entry entry=entries[getIndex(key)];
77         while (entry!=null && key!=entry.key) entry=entry.next;
78         return entry;
79     }
80
81     public boolean containsKey(final int key) {
82         return getEntry(key)!=null;
83     }
84
85     public String JavaDoc put(final int key, final String JavaDoc value) {
86         final int index=getIndex(key);
87         for (Entry entry=entries[index]; entry!= null; entry=entry.next) {
88             if (key==entry.key) {
89                 final String JavaDoc oldValue=entry.value;
90                 entry.value=value;
91                 return oldValue;
92             }
93         }
94         entries[index]=new Entry(key,value,entries[index]);
95         if (size++>=threshold) increaseCapacity();
96         return null;
97     }
98
99     private void increaseCapacity() {
100         final int oldCapacity=entries.length;
101         final Entry[] oldEntries=entries;
102         entries=new Entry[oldCapacity<<1];
103         bitmask=entries.length-1;
104         for (int i=0; i<oldCapacity; i++) {
105             Entry entry=oldEntries[i];
106             while (entry!=null) {
107                 final Entry next=entry.next;
108                 final int index=getIndex(entry.key);
109                 entry.next=entries[index];
110                 entries[index]=entry;
111                 entry=next;
112             }
113         }
114         threshold=(int)(entries.length*loadFactor);
115     }
116
117     public String JavaDoc remove(final int key) {
118         final int index=getIndex(key);
119         Entry previous=null;
120         for (Entry entry=entries[index]; entry!=null; entry=(previous=entry).next) {
121             if (key==entry.key) {
122                 if (previous==null)
123                     entries[index]=entry.next;
124                 else
125                     previous.next=entry.next;
126                 size--;
127                 return entry.value;
128             }
129         }
130         return null;
131     }
132
133     public void clear() {
134         for (int i=bitmask; i>=0; i--) entries[i]=null;
135         size=0;
136     }
137
138     public boolean containsValue(final String JavaDoc value) {
139         if (value==null) {
140             for (int i=bitmask; i>=0; i--)
141                 for (Entry entry=entries[i]; entry!=null; entry=entry.next)
142                     if (entry.value==null) return true;
143         } else {
144             for (int i=bitmask; i>=0; i--)
145                 for (Entry entry=entries[i]; entry!=null; entry=entry.next)
146                     if (value.equals(entry.value)) return true;
147         }
148         return false;
149     }
150
151     private static final class Entry {
152         final int key;
153         String JavaDoc value;
154         Entry next;
155
156         public Entry(final int key, final String JavaDoc value, final Entry next) {
157             this.key=key;
158             this.value=value;
159             this.next=next;
160         }
161     }
162 }
163
Popular Tags