1 16 package org.apache.commons.collections.bag; 17 18 import java.util.Set ; 19 20 import org.apache.commons.collections.Bag; 21 import org.apache.commons.collections.collection.SynchronizedCollection; 22 import org.apache.commons.collections.set.SynchronizedSet; 23 24 38 public class SynchronizedBag 39 extends SynchronizedCollection implements Bag { 40 41 42 private static final long serialVersionUID = 8084674570753837109L; 43 44 51 public static Bag decorate(Bag bag) { 52 return new SynchronizedBag(bag); 53 } 54 55 62 protected SynchronizedBag(Bag bag) { 63 super(bag); 64 } 65 66 73 protected SynchronizedBag(Bag bag, Object lock) { 74 super(bag, lock); 75 } 76 77 82 protected Bag getBag() { 83 return (Bag) collection; 84 } 85 86 public boolean add(Object object, int count) { 88 synchronized (lock) { 89 return getBag().add(object, count); 90 } 91 } 92 93 public boolean remove(Object object, int count) { 94 synchronized (lock) { 95 return getBag().remove(object, count); 96 } 97 } 98 99 public Set uniqueSet() { 100 synchronized (lock) { 101 Set set = getBag().uniqueSet(); 102 return new SynchronizedBagSet(set, lock); 103 } 104 } 105 106 public int getCount(Object object) { 107 synchronized (lock) { 108 return getBag().getCount(object); 109 } 110 } 111 112 116 class SynchronizedBagSet extends SynchronizedSet { 117 122 SynchronizedBagSet(Set set, Object lock) { 123 super(set, lock); 124 } 125 } 126 127 } 128 | Popular Tags |