1 36 package org.columba.ristretto.message; 37 38 import java.io.IOException ; 39 import java.io.ObjectInputStream ; 40 import java.io.ObjectOutputStream ; 41 import java.util.Hashtable ; 42 import java.util.Iterator ; 43 import java.util.Set ; 44 45 53 public class Attributes implements Saveable, Cloneable { 54 55 private Hashtable attributes; 56 57 61 public Attributes() { 62 attributes = new Hashtable (); 63 } 64 65 71 public Attributes(ObjectInputStream in) throws IOException { 72 load(in); 73 } 74 75 80 public void put(String key, Object value) { 81 attributes.put(key, value); 82 } 83 84 90 public Object get(String key) { 91 return attributes.get(key); 92 } 93 94 95 96 public final void load(ObjectInputStream in) throws IOException { 97 int size = in.readInt(); 98 attributes = new Hashtable (size); 99 for (int i = 0; i < size; i++) { 100 try { 101 attributes.put(in.readUTF(), in.readObject()); 102 } catch (ClassNotFoundException e) { 103 e.printStackTrace(); 104 } 105 } 106 } 107 108 109 public final void save(ObjectOutputStream out) throws IOException { 110 out.writeInt(attributes.size()); 111 Iterator keys = attributes.keySet().iterator(); 112 113 while (keys.hasNext()) { 114 String key = (String ) keys.next(); 115 Object value = attributes.get(key); 116 out.writeUTF(key); 117 out.writeObject(value); 118 } 119 } 120 121 125 public int count() { 126 return attributes.size(); 127 } 128 129 130 public Object clone() { 131 Attributes clone; 132 try { 133 clone = (Attributes) super.clone(); 134 clone.attributes = (Hashtable ) attributes.clone(); 135 return clone; 136 } catch (CloneNotSupportedException e) { 137 throw new RuntimeException (e); 138 } 139 } 140 141 142 public boolean equals(Object obj) { 143 boolean isEqual = false; 144 if ((obj != null) && (obj instanceof Attributes)) { 145 Attributes other = (Attributes) obj; 146 isEqual = attributes.equals(other.attributes); 147 } 148 return isEqual; 149 } 150 151 152 public int hashCode() { 153 return attributes.hashCode(); 154 } 155 156 157 public String toString() { 158 StringBuffer buffer = new StringBuffer (); 159 buffer.append("Attributes["); 160 Set set = attributes.keySet(); 161 for (Iterator iterator = set.iterator(); iterator.hasNext();) { 162 String key = (String ) iterator.next(); 163 buffer.append(key); 164 buffer.append("="); 165 buffer.append(attributes.get(key)); 166 if (iterator.hasNext()) { 167 buffer.append(", "); 168 } 169 } 170 buffer.append("]"); 171 return buffer.toString(); 172 } 173 } 174 | Popular Tags |