KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > classfile > MemberTable


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.classfile.MemberTable
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.classfile;
23
24 import java.io.IOException JavaDoc;
25
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28
29
30
31 class MemberTable {
32     protected Vector JavaDoc entries;
33     private Hashtable JavaDoc hashtable;
34     private MemberTableHash mutableMTH = null;
35
36     public MemberTable(int count) {
37         entries = new Vector JavaDoc(count);
38         hashtable = new Hashtable JavaDoc((count > 50) ? count : 50);
39         mutableMTH = new MemberTableHash(null, null);
40     }
41
42     void addEntry(ClassMember item) {
43         MemberTableHash mth= new MemberTableHash(
44                                     item.getName(),
45                                     item.getDescriptor(),
46                                     entries.size());
47         /* Add to the Vector */
48         entries.addElement(item);
49
50         /* Add to the Hashtable */
51         hashtable.put(mth, mth);
52     }
53
54     ClassMember find(String JavaDoc name, String JavaDoc descriptor) {
55
56         /* Set up the mutable MTH for the search */
57         mutableMTH.name = name;
58         mutableMTH.descriptor = descriptor;
59         mutableMTH.setHashCode();
60
61         /* search the hash table */
62         MemberTableHash mth = (MemberTableHash) hashtable.get(mutableMTH);
63         if (mth == null)
64         {
65             return null;
66         }
67
68         return (ClassMember) entries.elementAt(mth.index);
69     }
70
71     void put(ClassFormatOutput out) throws IOException JavaDoc {
72
73         Vector JavaDoc lentries = entries;
74         int count = lentries.size();
75         for (int i = 0; i < count; i++) {
76             ((ClassMember) lentries.elementAt(i)).put(out);
77         }
78     }
79
80     int size() {
81         return entries.size();
82     }
83
84     int classFileSize() {
85         int size = 0;
86
87         Vector JavaDoc lentries = entries;
88         int count = lentries.size();
89         for (int i = 0; i < count; i++) {
90             size += ((ClassMember) lentries.elementAt(i)).classFileSize();
91         }
92
93         return size;
94     }
95 }
96
97 class MemberTableHash
98 {
99     String JavaDoc name;
100     String JavaDoc descriptor;
101     int index;
102     int hashCode;
103     
104     MemberTableHash(String JavaDoc name, String JavaDoc descriptor, int index)
105     {
106         this.name = name;
107         this.descriptor = descriptor;
108         this.index = index;
109         /* Only set hashCode if both name and descriptor are non-null */
110         if (name != null && descriptor != null)
111         {
112             setHashCode();
113         }
114     }
115
116     MemberTableHash(String JavaDoc name, String JavaDoc descriptor)
117     {
118         this(name, descriptor, -1);
119     }
120
121     void setHashCode()
122     {
123         hashCode = name.hashCode() + descriptor.hashCode();
124     }
125
126     public boolean equals(Object JavaDoc other)
127     {
128         MemberTableHash mth = (MemberTableHash) other;
129
130         if (other == null)
131         {
132             return false;
133         }
134
135         if (name.equals(mth.name) && descriptor.equals(mth.descriptor))
136         {
137             return true;
138         }
139         else
140         {
141             return false;
142         }
143     }
144
145     public int hashCode()
146     {
147         return hashCode;
148     }
149 }
150
151
152
153
154
155
156
Popular Tags