1 package prefuse.data; 2 3 import java.util.HashMap ; 4 5 import prefuse.util.PrefuseLib; 6 7 22 public class Schema implements Cloneable { 23 24 private String [] m_names; 25 private Class [] m_types; 26 private Object [] m_dflts; 27 private HashMap m_lookup; 28 private int m_size; 29 private boolean m_locked; 30 31 34 37 public Schema() { 38 this(10); 39 } 40 41 46 public Schema(int ncols) { 47 m_names = new String [ncols]; 48 m_types = new Class [ncols]; 49 m_dflts = new Object [ncols]; 50 m_size = 0; 51 m_locked = false; 52 } 53 54 59 public Schema(String [] names, Class [] types) { 60 this(names.length); 61 62 if ( names.length != types.length ) { 64 throw new IllegalArgumentException ( 65 "Input arrays should be the same length"); 66 } 67 for ( int i=0; i<names.length; ++i ) { 68 addColumn(names[i], types[i], null); 69 } 70 } 71 72 79 public Schema(String [] names, Class [] types, Object [] defaults) { 80 this(names.length); 81 82 if ( names.length != types.length || 84 types.length != defaults.length ) 85 { 86 throw new IllegalArgumentException ( 87 "Input arrays should be the same length"); 88 } 89 for ( int i=0; i<names.length; ++i ) { 90 addColumn(names[i], types[i], defaults[i]); 91 } 92 } 93 94 100 public Object clone() { 101 Schema s = new Schema(m_size); 102 for ( int i=0; i<m_size; ++i ) { 103 s.addColumn(m_names[i], m_types[i], m_dflts[i]); 104 } 105 return s; 106 } 107 108 112 protected void initLookup() { 113 m_lookup = new HashMap (); 114 for ( int i=0; i<m_names.length; ++i ) { 115 m_lookup.put(m_names[i], new Integer (i)); 116 } 117 } 118 119 122 128 public Schema lockSchema() { 129 m_locked = true; 130 return this; 131 } 132 133 137 public boolean isLocked() { 138 return m_locked; 139 } 140 141 148 public void addColumn(String name, Class type) { 149 addColumn(name, type, null); 150 } 151 152 159 public void addColumn(String name, Class type, Object defaultValue) { 160 if ( m_locked ) { 162 throw new IllegalStateException ( 163 "Can not add column to a locked Schema."); 164 } 165 if ( name == null ) { 167 throw new IllegalArgumentException ( 168 "Null column names are not allowed."); 169 } 170 if ( type == null ) { 171 throw new IllegalArgumentException ( 172 "Null column types are not allowed."); 173 } 174 for ( int i=0; i<m_size; ++i ) { 175 if ( m_names[i].equals(name) ) { 176 throw new IllegalArgumentException ( 177 "Duplicate column names are not allowed: "+m_names[i]); 178 } 179 } 180 181 if ( m_names.length == m_size ) { 184 int capacity = (3*m_names.length)/2 + 1; 185 String [] names = new String [capacity]; 186 Class [] types = new Class [capacity]; 187 Object [] dflts = new Object [capacity]; 188 System.arraycopy(m_names, 0, names, 0, m_size); 189 System.arraycopy(m_types, 0, types, 0, m_size); 190 System.arraycopy(m_dflts, 0, dflts, 0, m_size); 191 m_names = names; 192 m_types = types; 193 m_dflts = dflts; 194 } 195 196 m_names[m_size] = name; 197 m_types[m_size] = type; 198 m_dflts[m_size] = defaultValue; 199 200 if ( m_lookup != null ) 201 m_lookup.put(name, new Integer (m_size)); 202 203 ++m_size; 204 } 205 206 224 public void addInterpolatedColumn(String name, Class type, Object dflt) { 225 addColumn(name, type, dflt); 226 addColumn(PrefuseLib.getStartField(name), type, dflt); 227 addColumn(PrefuseLib.getEndField(name), type, dflt); 228 } 229 230 236 public void addInterpolatedColumn(String name, Class type) { 237 addInterpolatedColumn(name, type, null); 238 } 239 240 244 public int getColumnCount() { 245 return m_size; 246 } 247 248 253 public String getColumnName(int col) { 254 return m_names[col]; 255 } 256 257 262 public int getColumnIndex(String field) { 263 if ( m_lookup == null ) 264 initLookup(); 265 266 Integer idx = (Integer )m_lookup.get(field); 267 return ( idx==null ? -1 : idx.intValue() ); 268 } 269 270 275 public Class getColumnType(int col) { 276 return m_types[col]; 277 } 278 279 284 public Class getColumnType(String field) { 285 int idx = getColumnIndex(field); 286 return ( idx<0 ? null : m_types[idx] ); 287 } 288 289 294 public Object getDefault(int col) { 295 return m_dflts[col]; 296 } 297 298 303 public Object getDefault(String field) { 304 int idx = getColumnIndex(field); 305 return ( idx<0 ? null : m_dflts[idx] ); 306 } 307 308 313 public void setDefault(int col, Object val) { 314 if ( m_locked ) { 316 throw new IllegalStateException ( 317 "Can not update default values of a locked Schema."); 318 } 319 m_dflts[col] = val; 320 } 321 322 327 public void setDefault(String field, Object val) { 328 if ( m_locked ) { 330 throw new IllegalStateException ( 331 "Can not update default values of a locked Schema."); 332 } 333 int idx = getColumnIndex(field); 334 m_dflts[idx] = val; 335 } 336 337 342 public void setDefault(String field, int val) { 343 setDefault(field, new Integer (val)); 344 } 345 346 351 public void setDefault(String field, long val) { 352 setDefault(field, new Long (val)); 353 } 354 355 360 public void setDefault(String field, float val) { 361 setDefault(field, new Float (val)); 362 } 363 364 369 public void setDefault(String field, double val) { 370 setDefault(field, new Double (val)); 371 } 372 373 378 public void setDefault(String field, boolean val) { 379 setDefault(field, val ? Boolean.TRUE : Boolean.FALSE); 380 } 381 382 388 public void setInterpolatedDefault(String field, Object val) { 389 setDefault(field, val); 390 setDefault(PrefuseLib.getStartField(field), val); 391 setDefault(PrefuseLib.getEndField(field), val); 392 } 393 394 400 public void setInterpolatedDefault(String field, int val) { 401 setInterpolatedDefault(field, new Integer (val)); 402 } 403 404 410 public void setInterpolatedDefault(String field, long val) { 411 setInterpolatedDefault(field, new Long (val)); 412 } 413 414 420 public void setInterpolatedDefault(String field, float val) { 421 setInterpolatedDefault(field, new Float (val)); 422 } 423 424 430 public void setInterpolatedDefault(String field, double val) { 431 setInterpolatedDefault(field, new Double (val)); 432 } 433 434 440 public void setInterpolatedDefault(String field, boolean val) { 441 setInterpolatedDefault(field, val ? Boolean.TRUE : Boolean.FALSE); 442 } 443 444 445 448 451 public boolean equals(Object o) { 452 if ( !(o instanceof Schema) ) 453 return false; 454 455 Schema s = (Schema)o; 456 if ( m_size != s.getColumnCount() ) 457 return false; 458 459 for ( int i=0; i<m_size; ++i ) { 460 if ( !(m_names[i].equals(s.getColumnName(i)) && 461 m_types[i].equals(s.getColumnType(i)) && 462 m_dflts[i].equals(s.getDefault(i))) ) 463 { 464 return false; 465 } 466 } 467 return true; 468 } 469 470 483 public boolean isAssignableFrom(Schema s) { 484 int ssize = s.getColumnCount(); 485 486 if ( ssize > m_size ) 487 return false; 488 489 for ( int i=0; i<ssize; ++i ) { 490 int idx = getColumnIndex(s.getColumnName(i)); 491 if ( idx < 0 ) 492 return false; 493 494 if ( !m_types[idx].equals(s.getColumnType(i)) ) 495 return false; 496 } 497 return true; 498 } 499 500 503 public int hashCode() { 504 int hashcode = 0; 505 for ( int i=0; i<m_size; ++i ) { 506 int idx = i+1; 507 int code = idx*m_names[i].hashCode(); 508 code ^= idx*m_types[i].hashCode(); 509 if ( m_dflts[i] != null ) 510 code ^= m_dflts[i].hashCode(); 511 hashcode ^= code; 512 } 513 return hashcode; 514 } 515 516 519 public String toString() { 520 StringBuffer sbuf = new StringBuffer (); 521 sbuf.append("Schema["); 522 for ( int i=0; i<m_size; ++i ) { 523 if ( i > 0 ) sbuf.append(' '); 524 sbuf.append('(').append(m_names[i]).append(", "); 525 sbuf.append(m_types[i].getName()).append(", "); 526 sbuf.append(m_dflts[i]).append(')'); 527 } 528 sbuf.append(']'); 529 return sbuf.toString(); 530 } 531 532 535 539 public Table instantiate() { 540 return instantiate(0); 541 } 542 543 548 public Table instantiate(int nrows) { 549 Table t = new Table(nrows, m_size); 550 for ( int i=0; i<m_size; ++i ) { 551 t.addColumn(m_names[i], m_types[i], m_dflts[i]); 552 } 553 return t; 554 } 555 556 } | Popular Tags |