Skip to content

Instantly share code, notes, and snippets.

@mox601
Created January 27, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mox601/77955c9440e267a8af73e63fb7dcf1d8 to your computer and use it in GitHub Desktop.
Save mox601/77955c9440e267a8af73e63fb7dcf1d8 to your computer and use it in GitHub Desktop.
public class ATest {
@Test
public void test() {
JSONObject root = new JSONObject("{}");
List<TestC> list = new ArrayList<>();
TestC testC = new TestC("type", "bucket", "code", null, DateTime.now(),
null);
TestC testC1 = new TestC("type", "bucket", "code", null, DateTime.now(),
null);
root.put("1", testC);
list.add(testC);
list.add(testC1);
root.put("2", list);
}
private static class TestC {
private final String id;
private final String bucket;
private final String type;
private final String code;
private Map<String, String> fields;
private List<String> tags;
private DateTime time;
private Long version;
public TestC(String type, String bucket, String code, Map<String, String> fields, DateTime time,
List<String> tags) {
if (bucket == null || bucket.isEmpty() || bucket.contains("-")) {
throw new IllegalArgumentException("bucket cannot be null, empty and cannot contain the '-' character.");
}
if (type == null || type.isEmpty() || type.contains("-")) {
throw new IllegalArgumentException("type cannot be null, empty and cannot contain the '-' character.");
}
if (code == null || code.isEmpty()) {
throw new IllegalArgumentException("code cannot be null, empty.");
}
if (fields == null) {
fields = new HashMap<>();
}
if (time == null) {
time = DateTime.now().withZone(DateTimeZone.UTC);
}
if (tags == null) {
tags = new ArrayList<>();
}
this.id = createId(bucket, type, code);
this.type = type;
this.bucket = bucket;
this.code = code;
this.fields = fields;
this.time = time;
this.tags = tags;
}
public static String createId(String bucket, String type, String code) {
return bucket + "-" + type + "-" + code;
}
public String getId() {
return id;
}
public String getBucket() {
return bucket;
}
public String getType() {
return type;
}
public String getCode() {
return code;
}
public Map<String, String> getFields() {
return fields;
}
public void setFields(Map<String, String> fields) {
this.fields = fields;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public DateTime getTime() {
return time;
}
public void setTime(DateTime time) {
this.time = time;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TestC testC = (TestC) o;
return Objects.equals(getId(), testC.getId()) &&
Objects.equals(getBucket(), testC.getBucket()) &&
Objects.equals(getType(), testC.getType()) &&
Objects.equals(getCode(), testC.getCode()) &&
Objects.equals(getFields(), testC.getFields()) &&
Objects.equals(getTags(), testC.getTags()) &&
Objects.equals(getTime(), testC.getTime()) &&
Objects.equals(getVersion(), testC.getVersion());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getBucket(), getType(), getCode(), getFields(), getTags(), getTime(), getVersion());
}
@Override
public String toString() {
return "TestC{" +
"id='" + id + '\'' +
", bucket='" + bucket + '\'' +
", type='" + type + '\'' +
", code='" + code + '\'' +
", fields=" + fields +
", tags=" + tags +
", time=" + time +
", version=" + version +
'}';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment