1 21 package oracle.toplink.essentials.internal.expressions; 23 24 import java.io.*; 25 import oracle.toplink.essentials.internal.helper.*; 26 27 34 public class TableAliasLookup implements Serializable { protected DatabaseTable[] keys; 36 protected DatabaseTable[] values; 37 protected int lastUsed; 38 39 40 protected boolean haveBeenAddedToStatement; 41 42 45 public TableAliasLookup() { 46 super(); 47 keys = new DatabaseTable[5]; 48 values = new DatabaseTable[5]; 49 lastUsed = 0; 50 } 51 52 55 public TableAliasLookup(int initialSize) { 56 super(); 57 keys = new DatabaseTable[initialSize]; 58 values = new DatabaseTable[initialSize]; 59 lastUsed = 0; 60 } 61 62 public void addToHashtable(java.util.Hashtable aHashTable) { 64 for (int i = 0; i < lastUsed; i++) { 65 aHashTable.put(keys[i], values[i]); 66 } 67 } 68 69 public DatabaseTable get(DatabaseTable key) { 70 int index = lookupIndexOf(key); 71 if (index == -1) { 72 return null; 73 } 74 return values[index]; 75 } 76 77 private void grow() { 78 DatabaseTable[] newKeys = new DatabaseTable[(lastUsed * 2)]; 79 DatabaseTable[] newValues = new DatabaseTable[(lastUsed * 2)]; 80 81 for (int i = 0; i < lastUsed; i++) { 82 newKeys[i] = keys[i]; 83 newValues[i] = values[i]; 84 } 85 keys = newKeys; 86 values = newValues; 87 } 88 89 96 public boolean haveBeenAddedToStatement() { 97 return haveBeenAddedToStatement; 98 } 99 100 103 public boolean isEmpty() { 104 return keys[0] == null; 105 } 106 107 public DatabaseTable keyAtValue(DatabaseTable value) { 108 int index = lookupValueIndexOf(value); 109 if (index == -1) { 110 return null; 111 } 112 return keys[index]; 113 } 114 115 public DatabaseTable[] keys() { 116 return keys; 117 } 118 119 private int lookupIndexOf(DatabaseTable table) { 120 for (int i = 0; i < lastUsed; i++) { 121 if (keys[i].equals(table)) { 122 return i; 123 } 124 } 125 return -1; 126 } 127 128 private int lookupValueIndexOf(DatabaseTable table) { 129 for (int i = 0; i < lastUsed; i++) { 130 if (values[i].equals(table)) { 131 return i; 132 } 133 } 134 return -1; 135 } 136 137 140 public DatabaseTable put(DatabaseTable key, DatabaseTable value) { 141 int index = lookupIndexOf(key); 142 if (index == -1) { 143 keys[lastUsed] = key; 144 values[lastUsed++] = value; 145 if (lastUsed >= keys.length) { 146 grow(); 147 } 148 } else { 149 values[index] = value; 150 } 151 return value; 152 } 153 154 161 public void setHaveBeenAddedToStatement(boolean value) { 162 haveBeenAddedToStatement = value; 163 } 164 165 168 public int size() { 169 return lastUsed; 170 } 171 172 public String toString() { 173 int max = size() - 1; 174 StringBuffer buf = new StringBuffer (); 175 buf.append("{"); 176 177 for (int i = 0; i <= max; i++) { 178 String s1 = keys[i].toString(); 179 String s2 = values[i].toString(); 180 buf.append(s1 + "=" + s2); 181 if (i < max) { 182 buf.append(", "); 183 } 184 } 185 buf.append("}"); 186 return buf.toString(); 187 } 188 189 public DatabaseTable[] values() { 190 return values; 191 } 192 } 193 | Popular Tags |