1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.classfile.MemberTable |
4 | |
5 | Copyright 2000, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.classfile; |
22 | |
23 | import java.io.IOException; |
24 | |
25 | import java.util.Hashtable; |
26 | import java.util.Vector; |
27 | |
28 | |
29 | |
30 | class MemberTable { |
31 | protected Vector entries; |
32 | private Hashtable hashtable; |
33 | private MemberTableHash mutableMTH = null; |
34 | |
35 | public MemberTable(int count) { |
36 | entries = new Vector(count); |
37 | hashtable = new Hashtable((count > 50) ? count : 50); |
38 | mutableMTH = new MemberTableHash(null, null); |
39 | } |
40 | |
41 | void addEntry(ClassMember item) { |
42 | MemberTableHash mth= new MemberTableHash( |
43 | item.getName(), |
44 | item.getDescriptor(), |
45 | entries.size()); |
46 | /* Add to the Vector */ |
47 | entries.addElement(item); |
48 | |
49 | /* Add to the Hashtable */ |
50 | hashtable.put(mth, mth); |
51 | } |
52 | |
53 | ClassMember find(String name, String descriptor) { |
54 | |
55 | /* Set up the mutable MTH for the search */ |
56 | mutableMTH.name = name; |
57 | mutableMTH.descriptor = descriptor; |
58 | mutableMTH.setHashCode(); |
59 | |
60 | /* search the hash table */ |
61 | MemberTableHash mth = (MemberTableHash) hashtable.get(mutableMTH); |
62 | if (mth == null) |
63 | { |
64 | return null; |
65 | } |
66 | |
67 | return (ClassMember) entries.elementAt(mth.index); |
68 | } |
69 | |
70 | void put(ClassFormatOutput out) throws IOException { |
71 | |
72 | Vector lentries = entries; |
73 | int count = lentries.size(); |
74 | for (int i = 0; i < count; i++) { |
75 | ((ClassMember) lentries.elementAt(i)).put(out); |
76 | } |
77 | } |
78 | |
79 | int size() { |
80 | return entries.size(); |
81 | } |
82 | |
83 | int classFileSize() { |
84 | int size = 0; |
85 | |
86 | Vector lentries = entries; |
87 | int count = lentries.size(); |
88 | for (int i = 0; i < count; i++) { |
89 | size += ((ClassMember) lentries.elementAt(i)).classFileSize(); |
90 | } |
91 | |
92 | return size; |
93 | } |
94 | } |
95 | |
96 | class MemberTableHash |
97 | { |
98 | String name; |
99 | String descriptor; |
100 | int index; |
101 | int hashCode; |
102 | |
103 | MemberTableHash(String name, String descriptor, int index) |
104 | { |
105 | this.name = name; |
106 | this.descriptor = descriptor; |
107 | this.index = index; |
108 | /* Only set hashCode if both name and descriptor are non-null */ |
109 | if (name != null && descriptor != null) |
110 | { |
111 | setHashCode(); |
112 | } |
113 | } |
114 | |
115 | MemberTableHash(String name, String descriptor) |
116 | { |
117 | this(name, descriptor, -1); |
118 | } |
119 | |
120 | void setHashCode() |
121 | { |
122 | hashCode = name.hashCode() + descriptor.hashCode(); |
123 | } |
124 | |
125 | public boolean equals(Object other) |
126 | { |
127 | MemberTableHash mth = (MemberTableHash) other; |
128 | |
129 | if (other == null) |
130 | { |
131 | return false; |
132 | } |
133 | |
134 | if (name.equals(mth.name) && descriptor.equals(mth.descriptor)) |
135 | { |
136 | return true; |
137 | } |
138 | else |
139 | { |
140 | return false; |
141 | } |
142 | } |
143 | |
144 | public int hashCode() |
145 | { |
146 | return hashCode; |
147 | } |
148 | } |
149 | |
150 | |
151 | |
152 | |
153 | |
154 | |