KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > config > Type


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.config;
5
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Set JavaDoc;
9
10 /**
11  * Represents configuration goo that hangs off of a particular class. Note: This class has no synchronization. If you
12  * want to use it in a multi-threaded context, you should add it.
13  */

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 JavaDoc typeName;
20   private final HashSet JavaDoc transients = new HashSet JavaDoc();
21   private int state = STATE_OPEN;
22
23   public Type() {
24     //
25
}
26
27   public Type(String JavaDoc typeName, String JavaDoc[] transientNames) {
28     this.typeName = typeName;
29     addTransients(transientNames);
30   }
31
32   public String JavaDoc getName() {
33     return this.typeName;
34   }
35
36   public void setName(String JavaDoc name) {
37     writeTest();
38     validateName(name);
39     this.typeName = name;
40   }
41
42   private void validateName(String JavaDoc name) {
43     if (name == null) {
44       throw new IllegalArgumentException JavaDoc("Class name cannot be null");
45     } else if ("".equals(name.trim())) { throw new IllegalArgumentException JavaDoc("Invalid class name: " + name); }
46   }
47
48   public void addTransients(String JavaDoc[] 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 JavaDoc transientName) {
60     writeTest();
61     validateTransient(transientName);
62     this.transients.add(transientName);
63   }
64
65   private void validateTransient(String JavaDoc transientName) {
66     if (transientName == null) {
67       throw new IllegalArgumentException JavaDoc("transient may not be null.");
68     } else if ("".equals(transientName.trim())) { throw new IllegalArgumentException JavaDoc("invalid transient: "
69                                                                                      + transientName); }
70   }
71
72   public Set JavaDoc getTransients() {
73     return (Set JavaDoc) this.transients.clone();
74   }
75
76   /**
77    * XXX: It's a bit goofy to throw an IllegalArgumentException. It's done this way because the validation is defined in
78    * terms of the field validator methods which throw IllegalArgumentExceptions. I didn't feel like creating a new
79    * exception type or hunting around for a more appropriate one.
80    */

81   public void validate() throws IllegalArgumentException JavaDoc {
82     validateName(getName());
83     for (Iterator JavaDoc i = this.transients.iterator(); i.hasNext();) {
84       validateTransient((String JavaDoc) 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 JavaDoc("Attempt to write to a committed Type object.");
94   }
95
96   public boolean containsTransient(String JavaDoc field) {
97     if (field != null) { return this.transients.contains(field); }
98     return false;
99   }
100   
101   public String JavaDoc toString() {
102     return getClass().getName() + "[typeName: " + typeName + ", transients: " + transients + "]";
103   }
104 }
Popular Tags