source: src/main/java/genius/core/AgentID.java@ 256

Last change on this file since 256 was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 2.0 KB
Line 
1package genius.core;
2
3import java.io.Serializable;
4
5import javax.xml.bind.annotation.XmlAttribute;
6import javax.xml.bind.annotation.XmlRootElement;
7
8/**
9 * Unique ID for an agent. Immutable. Not guaranteed to be unique.
10 *
11 */
12@XmlRootElement
13public class AgentID implements Serializable {
14
15 /**
16 *
17 */
18 private static final long serialVersionUID = -6858722448196458573L;
19
20 @XmlAttribute
21 String ID;
22
23 private static int serialNr = 0; // for generating unique party ids.
24
25 /**
26 * only for XML deserialization.
27 */
28 @SuppressWarnings("unused")
29 private AgentID() {
30
31 }
32
33 public AgentID(String id) {
34 this.ID = id;
35 }
36
37 /**
38 * factory function
39 *
40 * @param basename
41 * the basename for the agent ID
42 * @return a new unique AgentID based on the basename plus a unique serial
43 * number. These are unique only for a single run of the system, you
44 * can not assume new generated IDs to be different from IDs
45 * generated in earlier runs.
46 */
47 public static AgentID generateID(String basename) {
48 return new AgentID(basename + "@" + serialNr++);
49 }
50
51 @Override
52 public int hashCode() {
53 final int prime = 31;
54 int result = 1;
55 result = prime * result + ((ID == null) ? 0 : ID.hashCode());
56 return result;
57 }
58
59 @Override
60 public boolean equals(Object obj) {
61 if (this == obj)
62 return true;
63 if (obj == null)
64 return false;
65 if (getClass() != obj.getClass())
66 return false;
67 AgentID other = (AgentID) obj;
68 if (ID == null) {
69 if (other.ID != null)
70 return false;
71 } else if (!ID.equals(other.ID))
72 return false;
73 return true;
74 }
75
76 @Override
77 public String toString() {
78 return ID;
79 }
80
81 /**
82 *
83 * @return the agent's name, but with the serial number ("@X" with X a
84 * integer) extension stripped if there is such an extension
85 */
86 public String getName() {
87
88 if (ID.matches(".+@[0-9]+")) {
89 return ID.substring(0, ID.lastIndexOf("@"));
90 }
91 return ID;
92 }
93}
Note: See TracBrowser for help on using the repository browser.