1 15 package org.apache.hivemind.schema.impl; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.definition.Visibility; 25 import org.apache.hivemind.parse.BaseAnnotationHolder; 26 import org.apache.hivemind.schema.AttributeModel; 27 import org.apache.hivemind.schema.ElementModel; 28 import org.apache.hivemind.schema.Schema; 29 import org.apache.hivemind.util.UniqueHashMap; 30 31 36 public class SchemaImpl extends BaseAnnotationHolder implements Schema 37 { 38 private List _elementModels; 39 40 private List _shareableElementModels; 41 42 43 private Visibility _visibility = Visibility.PUBLIC; 44 45 46 private String _moduleId; 47 48 49 private String _id; 50 51 private String _rootElementClassName; 52 53 public SchemaImpl(String moduleId) 54 { 55 _moduleId = moduleId; 56 } 57 58 61 public String getModuleId() 62 { 63 return _moduleId; 64 } 65 66 69 public String getId() 70 { 71 return _id; 72 } 73 74 public String getFullyQualifiedId() 75 { 76 return getModuleId() + "." + _id; 77 } 78 79 82 public Visibility getVisibility() 83 { 84 return _visibility; 85 } 86 87 88 public boolean visibleToModule(String moduleId) 89 { 90 if (_visibility == Visibility.PUBLIC) 91 return true; 92 93 return getModuleId().equals(moduleId); 94 } 95 96 public void addElementModel(ElementModel model) 97 { 98 if (_elementModels == null) 99 _elementModels = new ArrayList (); 100 101 _elementModels.add(model); 102 _shareableElementModels = null; 103 } 104 105 public List getElementModel() 106 { 107 if (_shareableElementModels == null) 108 _shareableElementModels = _elementModels == null ? Collections.EMPTY_LIST : Collections 109 .unmodifiableList(_elementModels); 110 111 return _shareableElementModels; 112 } 113 114 117 public boolean canInstancesBeKeyed() 118 { 119 boolean emptyModel = _elementModels == null || _elementModels.isEmpty(); 120 121 if (emptyModel) 122 return false; 123 124 boolean result = false; 125 for (Iterator i = _elementModels.iterator(); i.hasNext();) 126 { 127 ElementModel model = (ElementModel) i.next(); 128 129 if (model.getKeyAttribute() != null) 130 result = true; 131 132 for (Iterator j = model.getAttributeModels().iterator(); j.hasNext();) 134 { 135 AttributeModel attributeModel = (AttributeModel) j.next(); 136 137 if (attributeModel.isUnique()) 138 result = true; 139 } 140 } 141 142 return result; 143 } 144 145 148 public boolean isKeyUnique() 149 { 150 boolean emptyModel = _elementModels == null || _elementModels.isEmpty(); 151 152 if (emptyModel) 153 return false; 154 155 boolean result = false; 156 for (Iterator i = _elementModels.iterator(); i.hasNext();) 157 { 158 ElementModel model = (ElementModel) i.next(); 159 160 for (Iterator j = model.getAttributeModels().iterator(); j.hasNext();) 162 { 163 AttributeModel attributeModel = (AttributeModel) j.next(); 164 165 if (attributeModel.isUnique()) 166 result = true; 167 } 168 } 169 170 return result; 171 } 172 173 177 public void validateKeyAttributes() 178 { 179 if (_elementModels == null) 180 return; 181 182 for (Iterator i = _elementModels.iterator(); i.hasNext();) 183 { 184 ElementModel em = (ElementModel) i.next(); 185 186 String key = em.getKeyAttribute(); 187 188 if (key == null) 189 continue; 190 191 AttributeModel keyAm = em.getAttributeModel(key); 192 193 if (keyAm == null) 194 throw new ApplicationRuntimeException("Key attribute \'" + key + "\' of element \'" 195 + em.getElementName() + "\' never declared.", em.getLocation(), null); 196 } 197 } 198 199 202 public void setVisibility(Visibility visibility) 203 { 204 _visibility = visibility; 205 } 206 207 210 public void setId(String id) 211 { 212 _id = id; 213 } 214 215 public String getDefiningModuleId() 216 { 217 return _moduleId; 218 } 219 220 public String getRootElementClassName() 221 { 222 if (_rootElementClassName == null) { 223 return getDefaultRootElementClassName(); 224 } 225 return _rootElementClassName; 226 } 227 228 232 private String getDefaultRootElementClassName() 233 { 234 if (canInstancesBeKeyed()) { 237 if (isKeyUnique()) { 238 return UniqueHashMap.class.getName(); 239 } else { 240 return HashMap .class.getName(); 241 } 242 } 243 else return ArrayList .class.getName(); 244 } 245 246 public void setRootElementClassName(String rootElementClassName) 247 { 248 _rootElementClassName = rootElementClassName; 249 } 250 251 } | Popular Tags |