001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.classfile.MemberTable
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.services.classfile;
023:
024: import java.io.IOException;
025:
026: import java.util.Hashtable;
027: import java.util.Vector;
028:
029: class MemberTable {
030: protected Vector entries;
031: private Hashtable hashtable;
032: private MemberTableHash mutableMTH = null;
033:
034: public MemberTable(int count) {
035: entries = new Vector(count);
036: hashtable = new Hashtable((count > 50) ? count : 50);
037: mutableMTH = new MemberTableHash(null, null);
038: }
039:
040: void addEntry(ClassMember item) {
041: MemberTableHash mth = new MemberTableHash(item.getName(), item
042: .getDescriptor(), entries.size());
043: /* Add to the Vector */
044: entries.addElement(item);
045:
046: /* Add to the Hashtable */
047: hashtable.put(mth, mth);
048: }
049:
050: ClassMember find(String name, String descriptor) {
051:
052: /* Set up the mutable MTH for the search */
053: mutableMTH.name = name;
054: mutableMTH.descriptor = descriptor;
055: mutableMTH.setHashCode();
056:
057: /* search the hash table */
058: MemberTableHash mth = (MemberTableHash) hashtable
059: .get(mutableMTH);
060: if (mth == null) {
061: return null;
062: }
063:
064: return (ClassMember) entries.elementAt(mth.index);
065: }
066:
067: void put(ClassFormatOutput out) throws IOException {
068:
069: Vector lentries = entries;
070: int count = lentries.size();
071: for (int i = 0; i < count; i++) {
072: ((ClassMember) lentries.elementAt(i)).put(out);
073: }
074: }
075:
076: int size() {
077: return entries.size();
078: }
079:
080: int classFileSize() {
081: int size = 0;
082:
083: Vector lentries = entries;
084: int count = lentries.size();
085: for (int i = 0; i < count; i++) {
086: size += ((ClassMember) lentries.elementAt(i))
087: .classFileSize();
088: }
089:
090: return size;
091: }
092: }
093:
094: class MemberTableHash {
095: String name;
096: String descriptor;
097: int index;
098: int hashCode;
099:
100: MemberTableHash(String name, String descriptor, int index) {
101: this .name = name;
102: this .descriptor = descriptor;
103: this .index = index;
104: /* Only set hashCode if both name and descriptor are non-null */
105: if (name != null && descriptor != null) {
106: setHashCode();
107: }
108: }
109:
110: MemberTableHash(String name, String descriptor) {
111: this (name, descriptor, -1);
112: }
113:
114: void setHashCode() {
115: hashCode = name.hashCode() + descriptor.hashCode();
116: }
117:
118: public boolean equals(Object other) {
119: MemberTableHash mth = (MemberTableHash) other;
120:
121: if (other == null) {
122: return false;
123: }
124:
125: if (name.equals(mth.name) && descriptor.equals(mth.descriptor)) {
126: return true;
127: } else {
128: return false;
129: }
130: }
131:
132: public int hashCode() {
133: return hashCode;
134: }
135: }
|