1 package org.eclipse.emf.examples.jet.article2.model; 2 3 4 import java.util.Iterator ; 5 import java.util.Properties ; 6 7 8 16 public class Instance 17 { 18 19 private TypesafeEnum mType = null; 20 21 private String mName = ""; 22 23 private Properties mValues = new Properties (); 24 25 28 public Instance() 29 { 30 } 31 32 38 public Instance(String name) 39 { 40 mName = name; 41 } 42 43 53 public Instance(String name, Properties values) 54 { 55 super(); 56 mName = name; 57 mValues = values; 58 } 59 60 70 public String keyDescription() 71 { 72 assertTypeNotNull(); 73 74 StringBuffer result = new StringBuffer (); 75 for (Iterator i = mType.keyAttributes(); i.hasNext();) 76 { 77 Attribute attribute = (Attribute)i.next(); 78 result.append(attribute.getCappedName()); 79 result.append(" = "); 80 result.append(getAttributeValue(attribute)); 81 if (i.hasNext()) 82 { 83 result.append(", "); 84 } 85 } 86 return result.toString(); 87 } 88 89 98 public String constructorValues() 99 { 100 assertTypeNotNull(); 102 StringBuffer result = new StringBuffer (); 103 for (Iterator i = getType().attributes(); i.hasNext();) 104 { 105 Attribute attribute = (Attribute)i.next(); 106 result.append(getAttributeValue(attribute)); 107 if (i.hasNext()) 108 { 109 result.append(", "); 110 } 111 } 112 return result.toString(); 113 } 114 115 122 public String getCappedName() 123 { 124 return NameUtil.constantToJavaClassName(getName()); 125 } 126 127 132 public String getName() 133 { 134 return mName; 135 } 136 137 144 public Properties getValues() 145 { 146 return mValues; 147 } 148 149 156 public String getAttributeValue(Attribute attribute) 157 { 158 return getAttributeValue(attribute.getName()); 159 } 160 161 168 public String getAttributeValue(String attributeName) 169 { 170 return mValues.getProperty(attributeName); 171 } 172 173 179 public void setName(String name) 180 { 181 mName = name; 182 } 183 184 192 public void setValues(Properties values) 193 { 194 mValues = values; 195 } 196 197 202 public TypesafeEnum getType() 203 { 204 return mType; 205 } 206 207 213 void setType(TypesafeEnum type) 214 { 215 mType = type; 216 } 217 218 227 public boolean isDefault() 228 { 229 assertTypeNotNull(); 230 return getType().getDefaultInstance() == this; 231 } 232 233 243 public void setDefault() 244 { 245 assertTypeNotNull(); 246 getType().setDefaultInstance(this); 247 } 248 249 private void assertTypeNotNull() 250 { 251 if (getType() == null) 252 { 253 throw new IllegalStateException ("Type is null. This instance has not been added to a type yet."); 254 } 255 } 256 } | Popular Tags |