1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow.impl;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import net.sourceforge.mflow.api.Contact;
27 import net.sourceforge.mflow.api.ContactMethod;
28
29 /***
30 * A class for representing an entity with which communication can occur
31 * Serves primarily as a repository for ContactMethods, associated with a name
32 *
33 * @author <a href="mailto:david@carr.name">David Carr</a>
34 * @version $Revision: 1.4 $
35 */
36 public class ContactImpl implements Contact {
37 /***
38 * Private storage for the ContactMethods associated with this Contact
39 */
40 private List contactMethods = new ArrayList();
41 /***
42 * Private storage for the name of this Contact
43 */
44 private String name = "";
45
46 /***
47 * Default constructor, no name set
48 */
49 ContactImpl() {
50 //No name set
51 }
52
53 /***
54 * Constructor accepting a name
55 *
56 * @param name the initial name
57 */
58 ContactImpl(String name) {
59 this();
60 this.name = name;
61 }
62
63 /***
64 * Mutator for the name
65 *
66 * @param name the new name
67 */
68 public void setName(String name) {
69 this.name = name;
70 }
71
72 /***
73 * Accessor for the name
74 *
75 * @return the name
76 */
77 public String getName() {
78 return this.name;
79 }
80
81 /***
82 * Returns whether the specified object equals the current object
83 *
84 * @param o an object to compare with
85 * @return true if the object is a Contact and has the same name and
86 * ContactMethods
87 */
88 public boolean equals(Object o) {
89 if (o instanceof Contact) {
90 Contact c = (Contact) o;
91 String otherName = c.getName();
92 ContactMethod[] otherContactMethods = c.getContactMethods();
93 ContactMethod[] myContactMethods =
94 (ContactMethod[]) this.contactMethods.toArray(
95 new ContactMethod[this.contactMethods.size()]);
96 return Arrays.equals(myContactMethods, otherContactMethods)
97 && this.name.equals(otherName);
98 } else {
99 return false;
100 }
101 }
102
103 /***
104 * @return the hashcode for the class
105 */
106 public int hashCode() {
107 StringBuffer sb = new StringBuffer();
108 sb.append(this.name);
109 for(Iterator it=this.contactMethods.iterator(); it.hasNext();) {
110 ContactMethod cm = (ContactMethod) it.next();
111 sb.append(cm.toString());
112 }
113 return sb.toString().hashCode();
114 }
115
116 /***
117 * Adds a ContactMethod
118 *
119 * @param cm the ContactMethod to add
120 */
121 public void addContactMethod(ContactMethod cm) {
122 if (!this.contactMethods.contains(cm)) {
123 cm.setContact(this);
124 this.contactMethods.add(cm);
125 }
126 }
127
128 /***
129 * Removes a ContactMethod if it is a member of this Contact
130 *
131 * @param cm the ContactMethod to remove
132 */
133 public void removeContactMethod(ContactMethod cm) {
134 if (equals(cm.getContact())) {
135 this.contactMethods.remove(cm);
136 cm.setContact(null);
137 }
138 }
139
140 /***
141 * Accessor for all of the ContactMethods associated with this Contact
142 *
143 * @return an array of ContactMethods
144 */
145 public ContactMethod[] getContactMethods() {
146 return (ContactMethod[]) this.contactMethods.toArray(
147 new ContactMethod[this.contactMethods.size()]);
148 }
149
150 /***
151 * Mutator for all of the ContactMethods associated with this Contact
152 *
153 * @param cms an array of ContactMethods
154 */
155 public void setContactMethods(ContactMethod[] cms) {
156 this.contactMethods.clear();
157 this.contactMethods.addAll(Arrays.asList(cms));
158 }
159
160 /***
161 * Accessor for a single ContactMethod associated with this Contact
162 *
163 * @param index the index of the ContactMethod to return
164 * @return the ContactMethod at the specified index
165 * @throws ArrayIndexOutOfBoundsException if the specified index is invalid
166 */
167 public ContactMethod getContactMethods(int index)
168 throws ArrayIndexOutOfBoundsException {
169 return (ContactMethod) this.contactMethods.get(index);
170 }
171
172 /***
173 * Mutator for a single ContactMethod associated with this Contact
174 *
175 * @param index the index of the ContactMethod to change
176 * @param cm te ContactMethod to set
177 * @throws ArrayIndexOutOfBoundsException if the specified index is invalid
178 */
179 public void setContactMethods(int index, ContactMethod cm)
180 throws ArrayIndexOutOfBoundsException {
181 this.contactMethods.set(index, cm);
182 }
183
184 /***
185 * Returns a string representation of this object
186 *
187 * @return a string representation of this object
188 */
189 public String toString() {
190 if (this.name != null) {
191 return this.name;
192 } else {
193 return "";
194 }
195 }
196 }
This page was automatically generated by Maven