View Javadoc

1   /*------------------------------------------------------------------------------
2    * The contents of this file are subject to the Mozilla Public License Version
3    * 1.1 (the "License"); you may not use this file except in compliance with
4    * the License. You may obtain a copy of the License at
5    * http://www.mozilla.org/MPL/
6    * Software distributed under the License is distributed on an "AS IS" basis,
7    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8    * the specific language governing rights and limitations under the License.
9    *
10   * The Original Code is levelonelabs.com code.
11   * The Initial Developer of the Original Code is Level One Labs. Portions
12   * created by the Initial Developer are Copyright (C) 2001 the Initial
13   * Developer. All Rights Reserved.
14   *
15   *         Contributor(s):
16   *             Scott Oster      (ostersc@alum.rpi.edu)
17   *             Steve Zingelwicz (sez@po.cwru.edu)
18   *             William Gorman   (willgorman@hotmail.com)
19   *
20   * Alternatively, the contents of this file may be used under the terms of
21   * either the GNU General Public License Version 2 or later (the "GPL"), or
22   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23   * in which case the provisions of the GPL or the LGPL are applicable
24   * instead of those above. If you wish to allow use of your version of this
25   * file only under the terms of either the GPL or the LGPL, and not to allow
26   * others to use your version of this file under the terms of the NPL, indicate
27   * your decision by deleting the provisions above and replace them with the
28   * notice and other provisions required by the GPL or the LGPL. If you do not
29   * delete the provisions above, a recipient may use your version of this file
30   * under the terms of any one of the NPL, the GPL or the LGPL.
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 }