1 5 package org.easymock.samples; 6 7 import java.util.HashMap ; 8 import java.util.HashSet ; 9 import java.util.Map ; 10 import java.util.Set ; 11 12 public class ClassUnderTest { 13 14 private Set <Collaborator> listeners = new HashSet <Collaborator>(); 15 16 private Map <String , byte[]> documents = new HashMap <String , byte[]>(); 17 18 public void addListener(Collaborator listener) { 19 listeners.add(listener); 20 } 21 22 public void addDocument(String title, byte[] document) { 23 boolean documentChange = documents.containsKey(title); 24 documents.put(title, document); 25 if (documentChange) { 26 notifyListenersDocumentChanged(title); 27 } else { 28 notifyListenersDocumentAdded(title); 29 } 30 } 31 32 public boolean removeDocument(String title) { 33 if (!documents.containsKey(title)) { 34 return true; 35 } 36 37 if (!listenersAllowRemoval(title)) { 38 return false; 39 } 40 41 documents.remove(title); 42 notifyListenersDocumentRemoved(title); 43 44 return true; 45 } 46 47 public boolean removeDocuments(String [] titles) { 48 if (!listenersAllowRemovals(titles)) { 49 return false; 50 } 51 52 for (String title : titles) { 53 documents.remove(title); 54 notifyListenersDocumentRemoved(title); 55 } 56 return true; 57 } 58 59 private void notifyListenersDocumentAdded(String title) { 60 for (Collaborator listener : listeners) { 61 listener.documentAdded(title); 62 } 63 } 64 65 private void notifyListenersDocumentChanged(String title) { 66 for (Collaborator listener : listeners) { 67 listener.documentChanged(title); 68 } 69 } 70 71 private void notifyListenersDocumentRemoved(String title) { 72 for (Collaborator listener : listeners) { 73 listener.documentRemoved(title); 74 } 75 } 76 77 private boolean listenersAllowRemoval(String title) { 78 int result = 0; 79 for (Collaborator listener : listeners) { 80 result += listener.voteForRemoval(title); 81 } 82 return result > 0; 83 } 84 85 private boolean listenersAllowRemovals(String [] titles) { 86 int result = 0; 87 for (Collaborator listener : listeners) { 88 result += listener.voteForRemovals(titles); 89 } 90 return result > 0; 91 } 92 93 } 94 | Popular Tags |