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 package com.levelonelabs.aim;
35
36
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.NodeList;
40
41 import java.util.ArrayList;
42 import java.util.Iterator;
43 import java.util.ListIterator;
44
45
46 /***
47 * Container for a group of AIMBuddies
48 *
49 * @author Scott Oster
50 *
51 * @created May 28, 2002
52 */
53 public class AIMGroup implements XMLizable {
54 private ArrayList buddies=new ArrayList();
55 private String name;
56
57 /***
58 * Constructor for the AIMGroup object
59 *
60 * @param name
61 */
62 public AIMGroup(String name) {
63 this.name=name;
64 }
65
66 /***
67 * Gets the name attribute of the AIMGroup object
68 *
69 * @return The name value
70 */
71 public String getName() {
72 return this.name;
73 }
74
75
76 /***
77 * Gets the list attribute of the AIMGroup object
78 *
79 * @return The list value
80 *
81 * @todo Need to make a real enumer or deep clone, to lazy right now
82 */
83 public ArrayList getList() {
84 return this.buddies;
85 }
86
87
88 /***
89 * Add a buddy to the list
90 *
91 * @param buddy
92 *
93 * @return true if the buddy wasnt already part of the list
94 */
95 public boolean add(String buddy) {
96 if(!this.buddies.contains(buddy)) {
97 return this.buddies.add(buddy);
98 }
99 return false;
100 }
101
102
103 /***
104 * Remove a buddy from the list
105 *
106 * @param buddy
107 *
108 * @return true if the buddy was part of the list and removed
109 */
110 public boolean remove(String buddy) {
111 if(this.buddies.contains(buddy)) {
112 this.buddies.remove(this.buddies.indexOf(buddy));
113 return true;
114 }
115 return false;
116 }
117
118
119 /***
120 * Returns the number of unique buddies in the group
121 *
122 * @return the size
123 */
124 public int size() {
125 return this.buddies.size();
126 }
127
128
129 /***
130 * List the buddies in the group
131 *
132 * @return string of space separated buddy names
133 */
134 public String toString() {
135 StringBuffer sb=new StringBuffer();
136 for(ListIterator it=this.buddies.listIterator(); it.hasNext();) {
137 sb.append(it.next()+" ");
138 }
139 return sb.toString();
140 }
141
142
143 /***
144 * @see com.levelonelabs.aim.XMLizable#readState(Element)
145 */
146 public void readState(Element fullStateElement) {
147 buddies=new ArrayList();
148 NodeList list=fullStateElement.getElementsByTagName("buddy");
149 for(int i=0; i < list.getLength(); i++) {
150 Element buddyElem=(Element) list.item(i);
151 String name=buddyElem.getAttribute("name");
152 add(name);
153 }
154 }
155
156
157 /***
158 * @see com.levelonelabs.aim.XMLizable#writeState(Element)
159 */
160 public void writeState(Element emptyStateElement) {
161 Document doc=emptyStateElement.getOwnerDocument();
162 emptyStateElement.setAttribute("name", this.getName());
163
164 Iterator buds=buddies.listIterator();
165 while(buds.hasNext()) {
166 String bud=(String) buds.next();
167 Element budElem=doc.createElement("buddy");
168 budElem.setAttribute("name", bud);
169 emptyStateElement.appendChild(budElem);
170 }
171 }
172 }