Skip to content

Commit 2d543ea

Browse files
committed
[DE-296] refactor BaseDocument
1 parent cb327d4 commit 2d543ea

25 files changed

+264
-338
lines changed

src/main/java/com/arangodb/entity/BaseDocument.java

Lines changed: 56 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -20,167 +20,130 @@
2020

2121
package com.arangodb.entity;
2222

23-
import com.arangodb.internal.DocumentFields;
2423
import com.fasterxml.jackson.annotation.JsonAnyGetter;
2524
import com.fasterxml.jackson.annotation.JsonAnySetter;
25+
import com.fasterxml.jackson.annotation.JsonIgnore;
2626
import com.fasterxml.jackson.annotation.JsonInclude;
2727

28-
import java.io.Serializable;
28+
import java.util.Collections;
2929
import java.util.HashMap;
3030
import java.util.Map;
31+
import java.util.Objects;
3132

3233
/**
3334
* @author Mark Vollmary
3435
*/
35-
public class BaseDocument implements Serializable {
36+
public class BaseDocument {
3637

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;
4640

4741
public BaseDocument() {
48-
super();
4942
properties = new HashMap<>();
5043
}
5144

5245
public BaseDocument(final String key) {
5346
this();
54-
this.key = key;
47+
setKey(key);
5548
}
5649

5750
public BaseDocument(final Map<String, Object> properties) {
5851
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);
7253
}
7354

55+
@JsonIgnore
7456
public String getId() {
75-
return id;
57+
return (String) getAttribute("_id");
7658
}
7759

7860
public void setId(final String id) {
79-
this.id = id;
61+
addAttribute("_id", id);
8062
}
8163

64+
@JsonIgnore
8265
public String getKey() {
83-
return key;
66+
return (String) getAttribute("_key");
8467
}
8568

8669
public void setKey(final String key) {
87-
this.key = key;
70+
addAttribute("_key", key);
8871
}
8972

73+
@JsonIgnore
9074
public String getRevision() {
91-
return revision;
75+
return (String) getAttribute("_rev");
9276
}
9377

94-
public void setRevision(final String revision) {
95-
this.revision = revision;
78+
public void setRevision(final String rev) {
79+
addAttribute("_rev", rev);
9680
}
9781

9882
@JsonInclude
9983
@JsonAnyGetter
10084
public Map<String, Object> getProperties() {
101-
return properties;
85+
return Collections.unmodifiableMap(properties);
10286
}
10387

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);
10697
}
10798

10899
@JsonInclude
109100
@JsonAnySetter
110101
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+
}
111107
properties.put(key, value);
112108
}
113109

114110
public void updateAttribute(final String key, final Object value) {
115111
if (properties.containsKey(key)) {
116-
properties.put(key, value);
112+
addAttribute(key, value);
117113
}
118114
}
119115

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+
}
122128
}
123129

124130
@Override
125131
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+
'}';
135135
}
136136

137137
@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);
146143
}
147144

148145
@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);
184148
}
185-
186149
}

src/main/java/com/arangodb/entity/BaseEdgeDocument.java

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
package com.arangodb.entity;
2222

23-
import com.arangodb.internal.DocumentFields;
23+
import com.fasterxml.jackson.annotation.JsonIgnore;
2424

2525
import java.util.Map;
2626

@@ -29,105 +29,65 @@
2929
*/
3030
public final class BaseEdgeDocument extends BaseDocument {
3131

32-
private static final long serialVersionUID = 6904923804449368783L;
33-
34-
@From
35-
private String from;
36-
@To
37-
private String to;
32+
private static final String[] META_PROPS = new String[]{"_id", "_key", "_rev", "_from", "_to"};
3833

3934
public BaseEdgeDocument() {
4035
super();
4136
}
4237

4338
public BaseEdgeDocument(final String from, final String to) {
4439
super();
45-
this.from = from;
46-
this.to = to;
40+
setFrom(from);
41+
setTo(to);
4742
}
4843

4944
public BaseEdgeDocument(final String key, final String from, final String to) {
5045
super(key);
51-
this.from = from;
52-
this.to = to;
46+
setFrom(from);
47+
setTo(to);
5348
}
5449

5550
public BaseEdgeDocument(final Map<String, Object> properties) {
5651
super(properties);
57-
final Object tmpFrom = properties.remove(DocumentFields.FROM);
58-
if (tmpFrom != null) {
59-
from = tmpFrom.toString();
60-
}
61-
final Object tmpTo = properties.remove(DocumentFields.TO);
62-
if (tmpTo != null) {
63-
to = tmpTo.toString();
64-
}
6552
}
6653

54+
@JsonIgnore
6755
public String getFrom() {
68-
return from;
56+
return (String) getAttribute("_from");
6957
}
7058

7159
public void setFrom(final String from) {
72-
this.from = from;
60+
addAttribute("_from", from);
7361
}
7462

63+
@JsonIgnore
7564
public String getTo() {
76-
return to;
65+
return (String) getAttribute("_to");
7766
}
7867

7968
public void setTo(final String to) {
80-
this.to = to;
69+
addAttribute("_to", to);
8170
}
8271

8372
@Override
84-
public String toString() {
85-
return "BaseDocument [documentRevision=" +
86-
revision +
87-
", documentHandle=" +
88-
id +
89-
", documentKey=" +
90-
key +
91-
", from=" +
92-
from +
93-
", to=" +
94-
to +
95-
", properties=" +
96-
properties +
97-
"]";
73+
protected String[] getMetaProps() {
74+
return META_PROPS;
9875
}
9976

10077
@Override
101-
public int hashCode() {
102-
final int prime = 31;
103-
int result = super.hashCode();
104-
result = prime * result + ((from == null) ? 0 : from.hashCode());
105-
result = prime * result + ((to == null) ? 0 : to.hashCode());
106-
return result;
78+
public String toString() {
79+
return "BaseEdgeDocument{properties=" + getProperties() + '}';
10780
}
10881

10982
@Override
110-
public boolean equals(final Object obj) {
111-
if (this == obj) {
112-
return true;
113-
}
114-
if (!super.equals(obj)) {
115-
return false;
116-
}
117-
if (getClass() != obj.getClass()) {
118-
return false;
119-
}
120-
final BaseEdgeDocument other = (BaseEdgeDocument) obj;
121-
if (from == null) {
122-
if (other.from != null) {
123-
return false;
124-
}
125-
} else if (!from.equals(other.from)) {
126-
return false;
127-
}
128-
if (to == null) {
129-
return other.to == null;
130-
} else return to.equals(other.to);
83+
public boolean equals(Object o) {
84+
if (this == o) return true;
85+
if (o == null || getClass() != o.getClass()) return false;
86+
return super.equals(o);
13187
}
13288

89+
@Override
90+
public int hashCode() {
91+
return super.hashCode();
92+
}
13393
}

0 commit comments

Comments
 (0)