1 4 package com.tc.object.config; 5 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 import java.util.Set ; 9 10 14 public class Type { 15 16 public static final int STATE_OPEN = 1; 17 public static final int STATE_COMMITTED = 2; 18 19 private String typeName; 20 private final HashSet transients = new HashSet (); 21 private int state = STATE_OPEN; 22 23 public Type() { 24 } 26 27 public Type(String typeName, String [] transientNames) { 28 this.typeName = typeName; 29 addTransients(transientNames); 30 } 31 32 public String getName() { 33 return this.typeName; 34 } 35 36 public void setName(String name) { 37 writeTest(); 38 validateName(name); 39 this.typeName = name; 40 } 41 42 private void validateName(String name) { 43 if (name == null) { 44 throw new IllegalArgumentException ("Class name cannot be null"); 45 } else if ("".equals(name.trim())) { throw new IllegalArgumentException ("Invalid class name: " + name); } 46 } 47 48 public void addTransients(String [] transientNames) { 49 writeTest(); 50 if (transientNames != null) { 51 if (transientNames != null) { 52 for (int i = 0; i < transientNames.length; i++) { 53 addTransient(transientNames[i]); 54 } 55 } 56 } 57 } 58 59 public void addTransient(String transientName) { 60 writeTest(); 61 validateTransient(transientName); 62 this.transients.add(transientName); 63 } 64 65 private void validateTransient(String transientName) { 66 if (transientName == null) { 67 throw new IllegalArgumentException ("transient may not be null."); 68 } else if ("".equals(transientName.trim())) { throw new IllegalArgumentException ("invalid transient: " 69 + transientName); } 70 } 71 72 public Set getTransients() { 73 return (Set ) this.transients.clone(); 74 } 75 76 81 public void validate() throws IllegalArgumentException { 82 validateName(getName()); 83 for (Iterator i = this.transients.iterator(); i.hasNext();) { 84 validateTransient((String ) i.next()); 85 } 86 } 87 88 public void commit() { 89 this.state = STATE_COMMITTED; 90 } 91 92 private void writeTest() { 93 if (this.state != STATE_OPEN) throw new IllegalStateException ("Attempt to write to a committed Type object."); 94 } 95 96 public boolean containsTransient(String field) { 97 if (field != null) { return this.transients.contains(field); } 98 return false; 99 } 100 101 public String toString() { 102 return getClass().getName() + "[typeName: " + typeName + ", transients: " + transients + "]"; 103 } 104 } | Popular Tags |