1 8 9 package com.sleepycat.persist.raw; 10 11 import java.util.Arrays ; 12 import java.util.Map ; 13 import java.util.TreeSet ; 14 15 import com.sleepycat.persist.evolve.Conversion; 16 import com.sleepycat.persist.model.EntityModel; 17 18 31 public class RawObject { 32 33 private static final String INDENT = " "; 34 35 private RawType type; 36 private Map <String ,Object > values; 37 private Object [] elements; 38 private String enumConstant; 39 private RawObject superObject; 40 41 58 public RawObject(RawType type, 59 Map <String ,Object > values, 60 RawObject superObject) { 61 if (type == null || values == null) { 62 throw new NullPointerException (); 63 } 64 this.type = type; 65 this.values = values; 66 this.superObject = superObject; 67 } 68 69 82 public RawObject(RawType type, Object [] elements) { 83 if (type == null || elements == null) { 84 throw new NullPointerException (); 85 } 86 this.type = type; 87 this.elements = elements; 88 } 89 90 101 public RawObject(RawType type, String enumConstant) { 102 if (type == null || enumConstant == null) { 103 throw new NullPointerException (); 104 } 105 this.type = type; 106 this.enumConstant = enumConstant; 107 } 108 109 118 public RawType getType() { 119 return type; 120 } 121 122 134 public Map <String ,Object > getValues() { 135 return values; 136 } 137 138 144 public Object [] getElements() { 145 return elements; 146 } 147 148 153 public String getEnum() { 154 return enumConstant; 155 } 156 157 161 public RawObject getSuper() { 162 return superObject; 163 } 164 165 @Override 166 public boolean equals(Object other) { 167 if (other == this) { 168 return true; 169 } 170 if (!(other instanceof RawObject)) { 171 return false; 172 } 173 RawObject o = (RawObject) other; 174 if (type != o.type) { 175 return false; 176 } 177 if (!Arrays.deepEquals(elements, o.elements)) { 178 return false; 179 } 180 if (values != null) { 181 if (!values.equals(o.values)) { 182 return false; 183 } 184 } else { 185 if (o.values != null) { 186 return false; 187 } 188 } 189 if (superObject != null) { 190 if (!superObject.equals(o.superObject)) { 191 return false; 192 } 193 } else { 194 if (o.superObject != null) { 195 return false; 196 } 197 } 198 return true; 199 } 200 201 @Override 202 public int hashCode() { 203 return System.identityHashCode(type) + 204 Arrays.deepHashCode(elements) + 205 (values != null ? values.hashCode() : 0) + 206 (superObject != null ? superObject.hashCode() : 0); 207 } 208 209 @Override 210 public String toString() { 211 StringBuffer buf = new StringBuffer (500); 212 formatRawObject(buf, "", null, false); 213 return buf.toString(); 214 } 215 216 private void formatRawObject(StringBuffer buf, 217 String indent, 218 String id, 219 boolean isSuper) { 220 String indent2 = indent + INDENT; 221 String endTag; 222 buf.append(indent); 223 if (type.isArray()) { 224 buf.append("<Array"); 225 endTag = "</Array>"; 226 } else if (type.isEnum()) { 227 buf.append("<Enum"); 228 endTag = "</Enum>"; 229 } else if (isSuper) { 230 buf.append("<Super"); 231 endTag = "</Super>"; 232 } else { 233 buf.append("<Object"); 234 endTag = "</Object>"; 235 } 236 if (id != null) { 237 formatId(buf, id); 238 } 239 if (type.isArray()) { 240 buf.append(" length=\""); 241 buf.append(elements.length); 242 buf.append('"'); 243 } 244 buf.append(" class=\""); 245 buf.append(type.getClassName()); 246 buf.append("\">\n"); 247 248 if (superObject != null) { 249 superObject.formatRawObject(buf, indent2, null, true); 250 } 251 if (type.isArray()) { 252 for (int i = 0; i < elements.length; i += 1) { 253 formatValue(buf, indent2, String.valueOf(i), elements[i]); 254 } 255 } else if (type.isEnum()) { 256 buf.append(enumConstant); 257 } else { 258 TreeSet <String > keys = new TreeSet <String >(values.keySet()); 259 for (String name : keys) { 260 formatValue(buf, indent2, name, values.get(name)); 261 } 262 } 263 buf.append(indent); 264 buf.append(endTag); 265 buf.append("\n"); 266 } 267 268 private static void formatValue(StringBuffer buf, 269 String indent, 270 String id, 271 Object val) { 272 if (val == null) { 273 buf.append(indent); 274 buf.append("<Null"); 275 formatId(buf, id); 276 buf.append("/>\n"); 277 } else if (val instanceof RawObject) { 278 ((RawObject) val).formatRawObject(buf, indent, id, false); 279 } else { 280 buf.append(indent); 281 buf.append("<Value"); 282 formatId(buf, id); 283 buf.append(" class=\""); 284 buf.append(val.getClass().getName()); 285 buf.append("\">"); 286 buf.append(val.toString()); 287 buf.append("</Value>\n"); 288 } 289 } 290 291 private static void formatId(StringBuffer buf, String id) { 292 if (Character.isDigit(id.charAt(0))) { 293 buf.append(" index=\""); 294 } else { 295 buf.append(" field=\""); 296 } 297 buf.append(id); 298 buf.append('"'); 299 } 300 } 301 | Popular Tags |