📄 hashtablealist.java
字号:
package st.ata.util;import java.io.IOException;import java.io.InputStream;import java.io.Serializable;import java.util.Arrays;import java.util.Date;import java.util.Hashtable;import java.util.Iterator;import java.util.NoSuchElementException;// Tested by TestHashtableAList/** Implementation of {@link AList} using simple hashtable. */@SuppressWarnings({"unchecked"})public class HashtableAList implements MutableAList, Serializable { private static final long serialVersionUID = 3670660167336648644L; private final Hashtable mTable = new Hashtable(); private static class DateArray { public Date[] values; public DateArray(Date[] v) { values = v; } public boolean equals(Object obj) { if (! (obj instanceof DateArray)) return false; return Arrays.equals(values, ((DateArray)obj).values); } } /** Remove all key-value mappings. */ public void clear() { close(); mTable.clear(); } public boolean containsKey(String key) { return mTable.containsKey(key); } /** * Deep Clone. * * Limited implementation * @return The cloned object. */ public Object clone() { HashtableAList copy = new HashtableAList(); String[] keys = getKeyArray(); for (int i=0; i<keys.length; i++) { Object me=getObject(keys[i]); if (me instanceof AList) copy.putObject(keys[i], ((AList)me).clone()); else if (me instanceof AList[]) { AList[] from = (AList[])me; int count=from.length; for (int j=0; j<from.length; j++) { if (from[j]==null) { count--; } } AList[] copyAList = new AList[count]; for (int j=0; j<count; j++) { if (from[j]==null) continue; copyAList[j]=(AList)from[j].clone(); } copy.putObject(keys[i], copyAList); } else if (me instanceof String[]) { String[] from = (String[])me; String[] copyA = new String[from.length]; for (int j=0; j<from.length; j++) copyA[j]=from[j]; copy.putObject(keys[i], copyA); } else if (me instanceof Long) { copy.putObject(keys[i], new Long(((Long)me).longValue())); } else if (me instanceof String) { copy.putObject(keys[i], me); } else X.noimpl(); } return copy; } /** * Shallow copy of fields of <code>other</code> into <code>this</code>. * @param other AList to copy from. */ public void copyFrom(AList other) { Iterator keys = other.getKeys(); while (keys.hasNext()) { String key = (String)keys.next(); switch (other.getType(key)) { case T_ALIST: putAList(key, other.getAList(key)); break; case T_DATE: putDate(key, other.getDate(key)); break; case T_INT: putInt(key, other.getInt(key)); break; case T_LONG: putLong(key, other.getLong(key)); break; case T_STRING: putString(key, other.getString(key)); break; case T_INPUTSTREAM: putInputStream(key, other.getInputStream(key)); break; case F_ARRAY | T_ALIST: putAListArray(key, other.getAListArray(key)); break; case F_ARRAY | T_DATE: putDateArray(key, other.getDateArray(key)); break; case F_ARRAY | T_INT: putIntArray(key, other.getIntArray(key)); break; case F_ARRAY | T_LONG: putLongArray(key, other.getLongArray(key)); break; case F_ARRAY | T_STRING: putStringArray(key, other.getStringArray(key)); break; case F_ARRAY_ARRAY | T_STRING: putStringArrayArray(key, other.getStringArrayArray(key)); break; case F_ARRAY | T_INPUTSTREAM: putInputStreamArray(key, other.getInputStreamArray(key)); break; default: X.fail("Unexpected case"); } } } public void copyKeysFrom(Iterator keys, AList other) { for (; keys.hasNext();) { String key = (String)keys.next(); Object value = other.getObject(key); // TODO: consider shallow or deep copy in some cases? // perhaps controlled by a third parameter? if(value!=null) { putObject(key,value); } } } public Object getObject(String key) { return mTable.get(key); } public void putObject(String key, Object val) { mTable.put(key, val); } public void remove(String key) { mTable.remove(key); } public Iterator getKeys() { return mTable.keySet().iterator(); } public String[] getKeyArray() { int i = 0; String keys[] = new String[mTable.size()]; for(Iterator it = getKeys(); it.hasNext(); ++i) keys[i] = (String)it.next(); return keys; } public int getInt(String key) { Integer v = (Integer)mTable.get(key); if (v == null) throw new NoSuchElementException(key); return v.intValue(); } public long getLong(String key) { Long v = (Long)mTable.get(key); if (v == null) throw new NoSuchElementException(key); return v.longValue(); } public String getString(String key) { String v = (String)mTable.get(key); if (v == null) throw new NoSuchElementException(key); return v; } public AList getAList(String key) { AList a = (AList) mTable.get(key); if (a == null) throw new NoSuchElementException(key); return a; } public Date getDate(String key) { Date v = (Date)mTable.get(key); if (v == null) throw new NoSuchElementException(key); return v; } public InputStream getInputStream(String key) { InputStream v = (InputStream)mTable.get(key); if (v == null) throw new NoSuchElementException(key); return v; } public int[] getIntArray(String key) { int[] a = (int[]) mTable.get(key); if (a == null) throw new NoSuchElementException(key); return a; } public long[] getLongArray(String key) { long[] a = (long[]) mTable.get(key); if (a == null) throw new NoSuchElementException(key); return a; } public String[] getStringArray(String key) { String[] a = (String[]) mTable.get(key); if (a == null) throw new NoSuchElementException(key); return a; } public AList[] getAListArray(String key) { AList[] a = (AList[]) mTable.get(key); if (a == null) throw new NoSuchElementException(key); return a; } public Date[] getDateArray(String key) { DateArray a = (DateArray) mTable.get(key); if (a == null) throw new NoSuchElementException(key); return a.values; } public InputStream[] getInputStreamArray(String key) { InputStream v[] = (InputStream [])mTable.get(key); if (v == null) throw new NoSuchElementException(key); return v; } public String[][] getStringArrayArray(String key) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -