[505] | 1 | package testcode.actions;
|
---|
| 2 |
|
---|
[1116] | 3 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 4 |
|
---|
[505] | 5 | import com.fasterxml.jackson.annotation.JsonValue;
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * Unique ID of a party in a negotiation. The name should start with a short
|
---|
| 9 | * string indicating the party (eg, the party name plus some machine
|
---|
| 10 | * identifier), optionally followed by an "_", and more characters to make the
|
---|
| 11 | * partyid unique. We require the name and "-" so that the string up to the
|
---|
| 12 | * first "-" can be used to determine between sessions which opponents are
|
---|
| 13 | * instances of the same class.
|
---|
| 14 | * <h2>Note</h2> Normally, negotiation parties should not create new Party IDs
|
---|
| 15 | * as all needed IDs should be provided by the protocol.
|
---|
| 16 | */
|
---|
| 17 | public final class PartyId {
|
---|
[1116] | 18 | private final @NonNull String name;
|
---|
[505] | 19 |
|
---|
| 20 | /**
|
---|
| 21 | *
|
---|
| 22 | * @param name a simple name, starting with letter, followed by zero or more
|
---|
| 23 | * letters, digits or _.
|
---|
| 24 | */
|
---|
[1116] | 25 | public PartyId(@NonNull String name) {
|
---|
[505] | 26 | if (name == null || !name.matches("[a-zA-Z]\\w*")) {
|
---|
| 27 | throw new IllegalArgumentException("name '" + name
|
---|
| 28 | + "' is not a letter followed by zero or more word characters (letter, digit or _)");
|
---|
| 29 | }
|
---|
| 30 | this.name = name;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | @JsonValue
|
---|
[1116] | 34 | public @NonNull String getName() {
|
---|
[505] | 35 | return name;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | @Override
|
---|
[1116] | 39 | public @NonNull String toString() {
|
---|
[505] | 40 | return name;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @Override
|
---|
| 44 | public int hashCode() {
|
---|
| 45 | final int prime = 31;
|
---|
| 46 | int result = 1;
|
---|
| 47 | result = prime * result + ((name == null) ? 0 : name.hashCode());
|
---|
| 48 | return result;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | public boolean equals(Object obj) {
|
---|
| 53 | if (this == obj)
|
---|
| 54 | return true;
|
---|
| 55 | if (obj == null)
|
---|
| 56 | return false;
|
---|
| 57 | if (getClass() != obj.getClass())
|
---|
| 58 | return false;
|
---|
| 59 | PartyId other = (PartyId) obj;
|
---|
| 60 | if (name == null) {
|
---|
| 61 | if (other.name != null)
|
---|
| 62 | return false;
|
---|
| 63 | } else if (!name.equals(other.name))
|
---|
| 64 | return false;
|
---|
| 65 | return true;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | }
|
---|