View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.osgi.service.importer.support.internal.collection;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.Set;
21  
22  /**
23   * Wrapper extension to {@link DynamicCollection} which prevents duplicates.
24   * 
25   * @see DynamicCollection
26   * @see Set
27   * @author Costin Leau
28   * 
29   */
30  public class DynamicSet extends DynamicCollection implements Set {
31  
32  	
33  	public DynamicSet() {
34  		super();
35  	}
36  
37  	public DynamicSet(Collection c) {
38  		super(c);
39  	}
40  
41  	public DynamicSet(int size) {
42  		super(size);
43  	}
44  
45  	public boolean add(Object o) {
46  		synchronized (storage) {
47  			if (storage.contains(o))
48  				return false;
49  			storage.add(o);
50  		}
51  		return true;
52  	}
53  
54  	public boolean addAll(Collection c) {
55  		if (c == null)
56  			throw new NullPointerException();
57  		boolean result = false;
58  		synchronized (storage) {
59  			for (Iterator iter = c.iterator(); iter.hasNext();) {
60  				result |= add(iter.next());
61  			}
62  		}
63  
64  		return result;
65  	}
66  
67  }