/revolsys-core/src/main/java/com/revolsys/record/io/format/esri/gdb/xml/model/Domain.java

https://github.com/revolsys/com.revolsys.open · Java · 358 lines · 291 code · 67 blank · 0 comment · 41 complexity · e1f538063f168ced480c9c7458ee1cb8 MD5 · raw file

  1. package com.revolsys.record.io.format.esri.gdb.xml.model;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import javax.swing.JComponent;
  9. import org.jeometry.common.compare.CompareUtil;
  10. import org.jeometry.common.data.identifier.Identifier;
  11. import com.revolsys.record.code.AbstractCodeTable;
  12. import com.revolsys.record.io.format.esri.gdb.xml.model.enums.FieldType;
  13. import com.revolsys.record.io.format.esri.gdb.xml.model.enums.MergePolicyType;
  14. import com.revolsys.record.io.format.esri.gdb.xml.model.enums.SplitPolicyType;
  15. import com.revolsys.record.io.format.json.JsonObject;
  16. public class Domain extends AbstractCodeTable implements Cloneable {
  17. private List<CodedValue> codedValues = new ArrayList<>();
  18. private Map<Identifier, List<Object>> idValueMap = new HashMap<>();
  19. private int maxId = 0;
  20. private Map<String, Identifier> stringIdMap = new HashMap<>();
  21. private JComponent swingEditor;
  22. private Map<String, Identifier> valueIdMap = new HashMap<>();
  23. private String description;
  24. private String domainName;
  25. private FieldType fieldType = FieldType.esriFieldTypeSmallInteger;
  26. private MergePolicyType mergePolicy = MergePolicyType.esriMPTAreaWeighted;
  27. private String owner;
  28. private SplitPolicyType splitPolicy = SplitPolicyType.esriSPTDuplicate;
  29. private String minValue;
  30. private String maxValue;
  31. public Domain() {
  32. }
  33. public Domain(final String domainName, final FieldType fieldType, final String description) {
  34. this.domainName = domainName;
  35. this.fieldType = fieldType;
  36. this.description = description;
  37. }
  38. public synchronized Domain addCodedValue(final Object code, final String name) {
  39. final Identifier identifier = Identifier.newIdentifier(code);
  40. final CodedValue value = new CodedValue(code, name);
  41. this.codedValues.add(value);
  42. final List<Object> values = Collections.<Object> singletonList(name);
  43. this.idValueMap.put(identifier, values);
  44. this.stringIdMap.put(code.toString(), identifier);
  45. this.valueIdMap.put(name.toLowerCase(), identifier);
  46. if (code instanceof Number) {
  47. final int id = ((Number)code).intValue();
  48. if (this.maxId < id) {
  49. this.maxId = id;
  50. }
  51. }
  52. return this;
  53. }
  54. public synchronized Domain addCodedValue(final String name) {
  55. newCodedValue(name);
  56. return this;
  57. }
  58. @Override
  59. protected int calculateValueFieldLength() {
  60. int length = 0;
  61. for (final String value : this.valueIdMap.keySet()) {
  62. final int valueLength = value.length();
  63. if (valueLength > length) {
  64. length = valueLength;
  65. }
  66. }
  67. return length;
  68. }
  69. @Override
  70. public Domain clone() {
  71. final Domain clone = (Domain)super.clone();
  72. clone.idValueMap = new HashMap<>();
  73. clone.stringIdMap = new HashMap<>();
  74. clone.valueIdMap = new HashMap<>();
  75. clone.codedValues = new ArrayList<>();
  76. for (final CodedValue codedValue : this.codedValues) {
  77. clone.addCodedValue(codedValue.getCode(), codedValue.getName());
  78. }
  79. return clone;
  80. }
  81. @Override
  82. public int compare(final Object value1, final Object value2) {
  83. if (value1 == null) {
  84. if (value2 == null) {
  85. return 0;
  86. } else {
  87. return 1;
  88. }
  89. } else if (value2 == null) {
  90. return -1;
  91. } else {
  92. final Object codeValue1 = getValue(Identifier.newIdentifier(value1));
  93. final Object codeValue2 = getValue(Identifier.newIdentifier(value2));
  94. return CompareUtil.compare(codeValue1, codeValue2);
  95. }
  96. }
  97. public List<CodedValue> getCodedValues() {
  98. return this.codedValues;
  99. }
  100. public String getDescription() {
  101. return this.description;
  102. }
  103. public String getDomainName() {
  104. return this.domainName;
  105. }
  106. @Override
  107. public List<String> getFieldNameAliases() {
  108. return Collections.emptyList();
  109. }
  110. public FieldType getFieldType() {
  111. return this.fieldType;
  112. }
  113. @Override
  114. public Identifier getIdentifier(final List<Object> values) {
  115. if (this.codedValues.isEmpty()) {
  116. return null;
  117. } else if (values.size() == 1) {
  118. final Object value = values.get(0);
  119. if (value == null) {
  120. return null;
  121. } else if (this.idValueMap.containsKey(value)) {
  122. return Identifier.newIdentifier(value);
  123. } else if (this.stringIdMap.containsKey(value.toString())) {
  124. return this.stringIdMap.get(value.toString());
  125. } else {
  126. final String lowerValue = ((String)value).toLowerCase();
  127. final Identifier id = this.valueIdMap.get(lowerValue);
  128. return id;
  129. }
  130. } else {
  131. throw new IllegalArgumentException("Expecting only a single value " + values);
  132. }
  133. }
  134. @Override
  135. public Identifier getIdentifier(final Map<String, ? extends Object> values) {
  136. if (this.codedValues.isEmpty()) {
  137. return null;
  138. } else {
  139. final Object name = getName(values);
  140. return getIdentifier(name);
  141. }
  142. }
  143. @Override
  144. public List<Identifier> getIdentifiers() {
  145. return new ArrayList<>(this.idValueMap.keySet());
  146. }
  147. @Override
  148. public String getIdFieldName() {
  149. return getDomainName() + "_ID";
  150. }
  151. @Override
  152. public JsonObject getMap(final Identifier id) {
  153. final Object value = getValue(id);
  154. return JsonObject.hash("NAME", value);
  155. }
  156. public String getMaxValue() {
  157. return this.maxValue;
  158. }
  159. public MergePolicyType getMergePolicy() {
  160. return this.mergePolicy;
  161. }
  162. public String getMinValue() {
  163. return this.minValue;
  164. }
  165. @Override
  166. public String getName() {
  167. return getDomainName();
  168. }
  169. public String getName(final Map<String, ? extends Object> values) {
  170. return (String)values.get("NAME");
  171. }
  172. public String getOwner() {
  173. return this.owner;
  174. }
  175. public SplitPolicyType getSplitPolicy() {
  176. return this.splitPolicy;
  177. }
  178. @Override
  179. public JComponent getSwingEditor() {
  180. return this.swingEditor;
  181. }
  182. @Override
  183. @SuppressWarnings("unchecked")
  184. public <V> V getValue(final Identifier id) {
  185. final List<Object> values = getValues(id);
  186. if (values == null) {
  187. return null;
  188. } else {
  189. final Object value = values.get(0);
  190. return (V)value;
  191. }
  192. }
  193. @Override
  194. public <V> V getValue(final Object id) {
  195. return getValue(Identifier.newIdentifier(id));
  196. }
  197. @Override
  198. public List<String> getValueFieldNames() {
  199. return Arrays.asList("NAME");
  200. }
  201. @Override
  202. public List<Object> getValues(final Identifier id) {
  203. if (id == null) {
  204. return null;
  205. } else {
  206. List<Object> values = this.idValueMap.get(id);
  207. if (values == null) {
  208. final Identifier objectId = this.stringIdMap.get(id.toString());
  209. if (objectId == null) {
  210. return null;
  211. } else {
  212. values = this.idValueMap.get(objectId);
  213. }
  214. }
  215. return Collections.unmodifiableList(values);
  216. }
  217. }
  218. public boolean hasCodedValues() {
  219. return !this.idValueMap.isEmpty();
  220. }
  221. @Override
  222. public boolean isEmpty() {
  223. return this.idValueMap.isEmpty();
  224. }
  225. @Override
  226. public boolean isLoaded() {
  227. return true;
  228. }
  229. @Override
  230. public boolean isLoading() {
  231. return false;
  232. }
  233. public synchronized Identifier newCodedValue(final String name) {
  234. Object id;
  235. switch (getFieldType()) {
  236. case esriFieldTypeInteger:
  237. id = (int)++this.maxId;
  238. break;
  239. case esriFieldTypeSmallInteger:
  240. id = (short)++this.maxId;
  241. break;
  242. default:
  243. throw new RuntimeException("Cannot generate code for field type " + getFieldType());
  244. }
  245. addCodedValue(id, name);
  246. return Identifier.newIdentifier(id);
  247. }
  248. @Override
  249. public void refresh() {
  250. }
  251. public synchronized void setCodedValues(final List<CodedValue> codedValues) {
  252. this.codedValues = new ArrayList<>();
  253. for (final CodedValue codedValue : codedValues) {
  254. final Object code = codedValue.getCode();
  255. final String name = codedValue.getName();
  256. addCodedValue(code, name);
  257. }
  258. }
  259. public void setDescription(final String description) {
  260. this.description = description;
  261. }
  262. public void setDomainName(final String domainName) {
  263. this.domainName = domainName;
  264. }
  265. public void setFieldType(final FieldType fieldType) {
  266. this.fieldType = fieldType;
  267. }
  268. public void setMaxValue(final String maxValue) {
  269. this.maxValue = maxValue;
  270. }
  271. public void setMergePolicy(final MergePolicyType mergePolicy) {
  272. this.mergePolicy = mergePolicy;
  273. }
  274. public void setMinValue(final String minValue) {
  275. this.minValue = minValue;
  276. }
  277. public void setOwner(final String owner) {
  278. this.owner = owner;
  279. }
  280. public void setSplitPolicy(final SplitPolicyType splitPolicy) {
  281. this.splitPolicy = splitPolicy;
  282. }
  283. @Override
  284. public void setSwingEditor(final JComponent swingEditor) {
  285. this.swingEditor = swingEditor;
  286. }
  287. @Override
  288. public String toString() {
  289. return this.domainName;
  290. }
  291. }