-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathPerson.java
99 lines (79 loc) · 2.93 KB
/
Person.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.simpleyaml.examples;
import org.simpleyaml.configuration.serialization.ConfigurationSerializable;
import org.simpleyaml.configuration.serialization.SerializableAs;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
/**
* Class that represents a Person that is used only for the test examples.
* <br>
* Note that has methods to serialize and deserialize Person objects to save them later.
*/
@SerializableAs("Person") // this is an alias to org.simpleyaml.examples.Person
public class Person implements ConfigurationSerializable {
private final String id;
private final String name;
private final int birthYear;
private boolean isAlive;
public Person(final String id, final String name, final int birthYear, final boolean isAlive) {
this.id = id;
this.name = name;
this.birthYear = birthYear;
this.isAlive = isAlive;
}
public Person(final String id, final String name, final int birthYear) {
this(id, name, birthYear, true);
}
/*
* The following methods allows you to serialize and deserialize
* your object to save them correctly to a YAML file.
*
* If you want you can create a constructor that accepts a single Map<String, Object>
* to deserialize instead using the method deserialize of below.
*/
public static Person deserialize(final Map<String, Object> mappedObject) { // note that is static
return new Person((String) mappedObject.get("id"),
(String) mappedObject.get("name"),
(int) mappedObject.get("birthYear"),
(boolean) mappedObject.get("isAlive"));
}
@Override
public Map<String, Object> serialize() {
final Map<String, Object> mappedObject = new LinkedHashMap<>();
mappedObject.put("id", this.id);
mappedObject.put("name", this.name);
mappedObject.put("birthYear", this.birthYear);
mappedObject.put("isAlive", this.isAlive);
return mappedObject;
}
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
public int getBirthYear() {
return this.birthYear;
}
public boolean isAlive() {
return this.isAlive;
}
public void kill() {
this.isAlive = false;
}
@Override
public String toString() {
return "Person [id= " + this.id + ", name= " + this.name + ", birthYear= " + this.birthYear + ", isAlive= " + this.isAlive + "]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return birthYear == person.birthYear && isAlive == person.isAlive && Objects.equals(id, person.id) && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name, birthYear, isAlive);
}
}