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.Comparator;
19  import java.util.SortedSet;
20  
21  import org.osgi.framework.BundleContext;
22  import org.osgi.framework.Filter;
23  import org.springframework.osgi.service.importer.support.internal.aop.ServiceProxyCreator;
24  
25  
26  /**
27   * OSGi service dynamic collection - allows iterating while the underlying
28   * storage is being shrunk/expanded. This collection is read-only - its content
29   * is being retrieved dynamically from the OSGi platform.
30   * 
31   * <p/> This collection and its iterators are thread-safe. That is, multiple
32   * threads can access the collection. However, since the collection is
33   * read-only, it cannot be modified by the client.
34   * 
35   * @author Costin Leau
36   * 
37   */
38  public class OsgiServiceSortedSet extends OsgiServiceSet implements SortedSet {
39  
40  	/**
41  	 * cast the collection to a specialized collection
42  	 */
43  	private SortedSet storage;
44  
45  	private final Comparator comparator;
46  
47  	public OsgiServiceSortedSet(Filter filter, BundleContext context, ClassLoader classLoader,
48  			ServiceProxyCreator proxyCreator) {
49  		this(filter, context, classLoader, null, proxyCreator);
50  	}
51  
52  	public OsgiServiceSortedSet(Filter filter, BundleContext context, ClassLoader classLoader, Comparator comparator,
53  			ServiceProxyCreator proxyCreator) {
54  		super(filter, context, classLoader, proxyCreator);
55  		this.comparator = comparator;
56  	}
57  
58  	protected DynamicCollection createInternalDynamicStorage() {
59  		storage = new DynamicSortedSet(comparator);
60  		return (DynamicCollection) storage;
61  	}
62  
63  	public Comparator comparator() {
64  		return storage.comparator();
65  	}
66  
67  	public Object first() {
68  		mandatoryServiceCheck();
69  		return storage.first();
70  	}
71  
72  	public Object last() {
73  		mandatoryServiceCheck();
74  		return storage.last();
75  	}
76  
77  	public SortedSet tailSet(Object fromElement) {
78  		mandatoryServiceCheck();
79  		return storage.tailSet(fromElement);
80  	}
81  
82  	public SortedSet headSet(Object toElement) {
83  		mandatoryServiceCheck();
84  		return storage.headSet(toElement);
85  	}
86  
87  	public SortedSet subSet(Object fromElement, Object toElement) {
88  		mandatoryServiceCheck();
89  		return storage.subSet(fromElement, toElement);
90  	}
91  
92  }