|
20 | 20 |
|
21 | 21 | package com.arangodb.entity;
|
22 | 22 |
|
23 |
| -import com.arangodb.internal.DocumentFields; |
24 | 23 | import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
25 | 24 | import com.fasterxml.jackson.annotation.JsonAnySetter;
|
| 25 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
26 | 26 | import com.fasterxml.jackson.annotation.JsonInclude;
|
27 | 27 |
|
28 |
| -import java.io.Serializable; |
| 28 | +import java.util.Collections; |
29 | 29 | import java.util.HashMap;
|
30 | 30 | import java.util.Map;
|
| 31 | +import java.util.Objects; |
31 | 32 |
|
32 | 33 | /**
|
33 | 34 | * @author Mark Vollmary
|
34 | 35 | */
|
35 |
| -public class BaseDocument implements Serializable { |
| 36 | +public class BaseDocument { |
36 | 37 |
|
37 |
| - private static final long serialVersionUID = -1824742667228719116L; |
38 |
| - |
39 |
| - @Id |
40 |
| - protected String id; |
41 |
| - @Key |
42 |
| - protected String key; |
43 |
| - @Rev |
44 |
| - protected String revision; |
45 |
| - protected Map<String, Object> properties; |
| 38 | + private static final String[] META_PROPS = new String[]{"_id", "_key", "_rev"}; |
| 39 | + private final Map<String, Object> properties; |
46 | 40 |
|
47 | 41 | public BaseDocument() {
|
48 |
| - super(); |
49 | 42 | properties = new HashMap<>();
|
50 | 43 | }
|
51 | 44 |
|
52 | 45 | public BaseDocument(final String key) {
|
53 | 46 | this();
|
54 |
| - this.key = key; |
| 47 | + setKey(key); |
55 | 48 | }
|
56 | 49 |
|
57 | 50 | public BaseDocument(final Map<String, Object> properties) {
|
58 | 51 | this();
|
59 |
| - final Object tmpId = properties.remove(DocumentFields.ID); |
60 |
| - if (tmpId != null) { |
61 |
| - id = tmpId.toString(); |
62 |
| - } |
63 |
| - final Object tmpKey = properties.remove(DocumentFields.KEY); |
64 |
| - if (tmpKey != null) { |
65 |
| - key = tmpKey.toString(); |
66 |
| - } |
67 |
| - final Object tmpRev = properties.remove(DocumentFields.REV); |
68 |
| - if (tmpRev != null) { |
69 |
| - revision = tmpRev.toString(); |
70 |
| - } |
71 |
| - this.properties = properties; |
| 52 | + setProperties(properties); |
72 | 53 | }
|
73 | 54 |
|
| 55 | + @JsonIgnore |
74 | 56 | public String getId() {
|
75 |
| - return id; |
| 57 | + return (String) getAttribute("_id"); |
76 | 58 | }
|
77 | 59 |
|
78 | 60 | public void setId(final String id) {
|
79 |
| - this.id = id; |
| 61 | + addAttribute("_id", id); |
80 | 62 | }
|
81 | 63 |
|
| 64 | + @JsonIgnore |
82 | 65 | public String getKey() {
|
83 |
| - return key; |
| 66 | + return (String) getAttribute("_key"); |
84 | 67 | }
|
85 | 68 |
|
86 | 69 | public void setKey(final String key) {
|
87 |
| - this.key = key; |
| 70 | + addAttribute("_key", key); |
88 | 71 | }
|
89 | 72 |
|
| 73 | + @JsonIgnore |
90 | 74 | public String getRevision() {
|
91 |
| - return revision; |
| 75 | + return (String) getAttribute("_rev"); |
92 | 76 | }
|
93 | 77 |
|
94 |
| - public void setRevision(final String revision) { |
95 |
| - this.revision = revision; |
| 78 | + public void setRevision(final String rev) { |
| 79 | + addAttribute("_rev", rev); |
96 | 80 | }
|
97 | 81 |
|
98 | 82 | @JsonInclude
|
99 | 83 | @JsonAnyGetter
|
100 | 84 | public Map<String, Object> getProperties() {
|
101 |
| - return properties; |
| 85 | + return Collections.unmodifiableMap(properties); |
102 | 86 | }
|
103 | 87 |
|
104 |
| - public void setProperties(final Map<String, Object> properties) { |
105 |
| - this.properties = properties; |
| 88 | + public void setProperties(final Map<String, Object> props) { |
| 89 | + for (String f : getMetaProps()) { |
| 90 | + requireString(f, props.get(f)); |
| 91 | + } |
| 92 | + this.properties.putAll(props); |
| 93 | + } |
| 94 | + |
| 95 | + public Object getAttribute(final String key) { |
| 96 | + return properties.get(key); |
106 | 97 | }
|
107 | 98 |
|
108 | 99 | @JsonInclude
|
109 | 100 | @JsonAnySetter
|
110 | 101 | public void addAttribute(final String key, final Object value) {
|
| 102 | + for (String f : getMetaProps()) { |
| 103 | + if (f.equals(key)) { |
| 104 | + requireString(key, value); |
| 105 | + } |
| 106 | + } |
111 | 107 | properties.put(key, value);
|
112 | 108 | }
|
113 | 109 |
|
114 | 110 | public void updateAttribute(final String key, final Object value) {
|
115 | 111 | if (properties.containsKey(key)) {
|
116 |
| - properties.put(key, value); |
| 112 | + addAttribute(key, value); |
117 | 113 | }
|
118 | 114 | }
|
119 | 115 |
|
120 |
| - public Object getAttribute(final String key) { |
121 |
| - return properties.get(key); |
| 116 | + public void removeAttribute(final String key) { |
| 117 | + properties.remove(key); |
| 118 | + } |
| 119 | + |
| 120 | + protected String[] getMetaProps() { |
| 121 | + return META_PROPS; |
| 122 | + } |
| 123 | + |
| 124 | + private void requireString(final String k, final Object v) { |
| 125 | + if (v != null && !(v instanceof String)) { |
| 126 | + throw new IllegalArgumentException(k + " must be a String"); |
| 127 | + } |
122 | 128 | }
|
123 | 129 |
|
124 | 130 | @Override
|
125 | 131 | public String toString() {
|
126 |
| - return "BaseDocument [documentRevision=" + |
127 |
| - revision + |
128 |
| - ", documentHandle=" + |
129 |
| - id + |
130 |
| - ", documentKey=" + |
131 |
| - key + |
132 |
| - ", properties=" + |
133 |
| - properties + |
134 |
| - "]"; |
| 132 | + return "BaseDocument{" + |
| 133 | + "properties=" + properties + |
| 134 | + '}'; |
135 | 135 | }
|
136 | 136 |
|
137 | 137 | @Override
|
138 |
| - public int hashCode() { |
139 |
| - final int prime = 31; |
140 |
| - int result = 1; |
141 |
| - result = prime * result + ((id == null) ? 0 : id.hashCode()); |
142 |
| - result = prime * result + ((key == null) ? 0 : key.hashCode()); |
143 |
| - result = prime * result + ((properties == null) ? 0 : properties.hashCode()); |
144 |
| - result = prime * result + ((revision == null) ? 0 : revision.hashCode()); |
145 |
| - return result; |
| 138 | + public boolean equals(Object o) { |
| 139 | + if (this == o) return true; |
| 140 | + if (o == null || getClass() != o.getClass()) return false; |
| 141 | + BaseDocument that = (BaseDocument) o; |
| 142 | + return properties.equals(that.properties); |
146 | 143 | }
|
147 | 144 |
|
148 | 145 | @Override
|
149 |
| - public boolean equals(final Object obj) { |
150 |
| - if (this == obj) { |
151 |
| - return true; |
152 |
| - } |
153 |
| - if (obj == null) { |
154 |
| - return false; |
155 |
| - } |
156 |
| - if (getClass() != obj.getClass()) { |
157 |
| - return false; |
158 |
| - } |
159 |
| - final BaseDocument other = (BaseDocument) obj; |
160 |
| - if (id == null) { |
161 |
| - if (other.id != null) { |
162 |
| - return false; |
163 |
| - } |
164 |
| - } else if (!id.equals(other.id)) { |
165 |
| - return false; |
166 |
| - } |
167 |
| - if (key == null) { |
168 |
| - if (other.key != null) { |
169 |
| - return false; |
170 |
| - } |
171 |
| - } else if (!key.equals(other.key)) { |
172 |
| - return false; |
173 |
| - } |
174 |
| - if (properties == null) { |
175 |
| - if (other.properties != null) { |
176 |
| - return false; |
177 |
| - } |
178 |
| - } else if (!properties.equals(other.properties)) { |
179 |
| - return false; |
180 |
| - } |
181 |
| - if (revision == null) { |
182 |
| - return other.revision == null; |
183 |
| - } else return revision.equals(other.revision); |
| 146 | + public int hashCode() { |
| 147 | + return Objects.hash(properties); |
184 | 148 | }
|
185 |
| - |
186 | 149 | }
|
0 commit comments