KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > user > UserCollection


1 /**
2  * $RCSfile: UserCollection.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2004/12/13 18:06:53 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.user;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.NoSuchElementException JavaDoc;
16 import java.util.AbstractCollection JavaDoc;
17
18 /**
19  * Provides a view of an array of usernames as a Collection of User objects. If
20  * any of the usernames cannot be loaded, they are transparently skipped when
21  * iteratating over the collection.
22  *
23  * @author Matt Tucker
24  */

25 public class UserCollection extends AbstractCollection JavaDoc {
26
27     private String JavaDoc[] elements;
28     private int currentIndex = -1;
29     private Object JavaDoc nextElement = null;
30
31     /**
32      * Constructs a new UserIterator.
33      */

34     public UserCollection(String JavaDoc [] elements) {
35         this.elements = elements;
36     }
37
38     public Iterator JavaDoc iterator() {
39         return new UserIterator();
40     }
41
42     public int size() {
43         return elements.length;
44     }
45
46     private class UserIterator implements Iterator JavaDoc {
47
48         public boolean hasNext() {
49             // If we are at the end of the list, there can't be any more elements
50
// to iterate through.
51
if (currentIndex + 1 >= elements.length && nextElement == null) {
52                 return false;
53             }
54             // Otherwise, see if nextElement is null. If so, try to load the next
55
// element to make sure it exists.
56
if (nextElement == null) {
57                 nextElement = getNextElement();
58                 if (nextElement == null) {
59                     return false;
60                 }
61             }
62             return true;
63         }
64
65         public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
66             Object JavaDoc element = null;
67             if (nextElement != null) {
68                 element = nextElement;
69                 nextElement = null;
70             }
71             else {
72                 element = getNextElement();
73                 if (element == null) {
74                     throw new NoSuchElementException JavaDoc();
75                 }
76             }
77             return element;
78         }
79
80         public void remove() throws UnsupportedOperationException JavaDoc {
81             throw new UnsupportedOperationException JavaDoc();
82         }
83
84         /**
85          * Returns the next available element, or null if there are no more elements to return.
86          *
87          * @return the next available element.
88          */

89         private Object JavaDoc getNextElement() {
90             while (currentIndex + 1 < elements.length) {
91                 currentIndex++;
92                 Object JavaDoc element = null;
93                 try {
94                     element = UserManager.getInstance().getUser(elements[currentIndex]);
95                 }
96                 catch (UserNotFoundException unfe) { }
97                 if (element != null) {
98                     return element;
99                 }
100             }
101             return null;
102         }
103     }
104 }
Popular Tags