1 11 12 package org.jivesoftware.messenger.user; 13 14 import java.util.Iterator ; 15 import java.util.NoSuchElementException ; 16 import java.util.AbstractCollection ; 17 18 25 public class UserCollection extends AbstractCollection { 26 27 private String [] elements; 28 private int currentIndex = -1; 29 private Object nextElement = null; 30 31 34 public UserCollection(String [] elements) { 35 this.elements = elements; 36 } 37 38 public Iterator iterator() { 39 return new UserIterator(); 40 } 41 42 public int size() { 43 return elements.length; 44 } 45 46 private class UserIterator implements Iterator { 47 48 public boolean hasNext() { 49 if (currentIndex + 1 >= elements.length && nextElement == null) { 52 return false; 53 } 54 if (nextElement == null) { 57 nextElement = getNextElement(); 58 if (nextElement == null) { 59 return false; 60 } 61 } 62 return true; 63 } 64 65 public Object next() throws java.util.NoSuchElementException { 66 Object 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 (); 75 } 76 } 77 return element; 78 } 79 80 public void remove() throws UnsupportedOperationException { 81 throw new UnsupportedOperationException (); 82 } 83 84 89 private Object getNextElement() { 90 while (currentIndex + 1 < elements.length) { 91 currentIndex++; 92 Object 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 |