1 | package geniusweb.partiesserver.repository;
|
---|
2 |
|
---|
3 | import geniusweb.actions.PartyId;
|
---|
4 | import geniusweb.party.Capabilities;
|
---|
5 | import geniusweb.party.Party;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * An approved and ready to run party.
|
---|
9 | */
|
---|
10 | public class AvailableJavaParty implements AvailableParty {
|
---|
11 |
|
---|
12 | private final Class<? extends Party> partyclass;
|
---|
13 | private final String name;
|
---|
14 | private final transient Capabilities capabilities;
|
---|
15 | private final transient String description;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | *
|
---|
19 | * @param name the filename. This is not a {@link PartyId}.
|
---|
20 | * @param partyclass the class of the party.
|
---|
21 | * @throws ClassNotFoundException if unknown partyclass
|
---|
22 | */
|
---|
23 | public AvailableJavaParty(String name, Class<? extends Party> partyclass)
|
---|
24 | throws ClassNotFoundException {
|
---|
25 | this.name = name;
|
---|
26 | this.partyclass = partyclass;
|
---|
27 |
|
---|
28 | Party party;
|
---|
29 | try {
|
---|
30 | party = partyclass.newInstance();
|
---|
31 | } catch (Throwable e) {
|
---|
32 | throw new ClassNotFoundException(
|
---|
33 | "Could not instantiate " + partyclass, e);
|
---|
34 | }
|
---|
35 | capabilities = party.getCapabilities();
|
---|
36 | description = party.getDescription();
|
---|
37 |
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public String getID() {
|
---|
42 | return name;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public String getName() {
|
---|
47 | return name;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | *
|
---|
52 | * @return cached copy of what was received from a call to
|
---|
53 | * {@link Party#getDescription()}.
|
---|
54 | */
|
---|
55 | @Override
|
---|
56 | public String getDescription() {
|
---|
57 | return description;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | *
|
---|
62 | * @return cached copy of what was received from a call to
|
---|
63 | * {@link AvailableParty#getCapabilities()}
|
---|
64 | */
|
---|
65 | @Override
|
---|
66 | public Capabilities getCapabilities() {
|
---|
67 | return capabilities;
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public Party getInstance() throws InstantiationFailedException {
|
---|
72 | try {
|
---|
73 | return partyclass.newInstance();
|
---|
74 | } catch (Exception e) {
|
---|
75 | throw new InstantiationFailedException(
|
---|
76 | "Failed to create instance of party " + this, e);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public void free() {
|
---|
82 | // FAIK we can not unload the class or free resources.
|
---|
83 | // it would be done automatically if our classloader
|
---|
84 | // is garbage collected.
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public int hashCode() {
|
---|
89 | final int prime = 31;
|
---|
90 | int result = 1;
|
---|
91 | result = prime * result + ((name == null) ? 0 : name.hashCode());
|
---|
92 | result = prime * result
|
---|
93 | + ((partyclass == null) ? 0 : partyclass.hashCode());
|
---|
94 | return result;
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public boolean equals(Object obj) {
|
---|
99 | if (this == obj)
|
---|
100 | return true;
|
---|
101 | if (obj == null)
|
---|
102 | return false;
|
---|
103 | if (getClass() != obj.getClass())
|
---|
104 | return false;
|
---|
105 | AvailableJavaParty other = (AvailableJavaParty) obj;
|
---|
106 | if (name == null) {
|
---|
107 | if (other.name != null)
|
---|
108 | return false;
|
---|
109 | } else if (!name.equals(other.name))
|
---|
110 | return false;
|
---|
111 | if (partyclass == null) {
|
---|
112 | if (other.partyclass != null)
|
---|
113 | return false;
|
---|
114 | } else if (!partyclass.equals(other.partyclass))
|
---|
115 | return false;
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public String toString() {
|
---|
121 | return name + " (" + partyclass + ")";
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|