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  
17  package org.springframework.osgi.service.importer.support.internal.collection;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Comparator;
22  
23  import org.springframework.util.Assert;
24  
25  /**
26   * A specilized subtype of DynamicList which impose an order between its
27   * elements.
28   * 
29   * @author Costin Leau
30   * 
31   */
32  public class DynamicSortedList extends DynamicList {
33  
34  	private final Comparator comparator;
35  
36  
37  	public DynamicSortedList() {
38  		this((Comparator) null);
39  	}
40  
41  	public DynamicSortedList(Comparator c) {
42  		super();
43  		this.comparator = c;
44  
45  	}
46  
47  	public DynamicSortedList(Collection c) {
48  		this.comparator = null;
49  		addAll(c);
50  	}
51  
52  	public DynamicSortedList(int size) {
53  		super(size);
54  		this.comparator = null;
55  	}
56  
57  	// this is very similar but not identical from DynamicSortedSet
58  	// the main difference is that duplicates are accepted
59  	public boolean add(Object o) {
60  		Assert.notNull(o);
61  
62  		if (comparator == null && !(o instanceof Comparable))
63  			throw new ClassCastException("given object does not implement " + Comparable.class.getName()
64  					+ " and no Comparator is set on the collection");
65  
66  		int index = 0;
67  
68  		synchronized (storage) {
69  			index = Collections.binarySearch(storage, o, comparator);
70  			// duplicate found; it's okay since it's a list
71  			boolean duplicate = (index >= 0);
72  
73  			// however, make sure we add the element at the end of the
74  			// duplicates
75  			if (duplicate) {
76  				boolean stillEqual = true;
77  				while (index + 1 < storage.size() && stillEqual) {
78  
79  					stillEqual = false;
80  					Object next = storage.get(index + 1);
81  
82  					if ((comparator != null ? comparator.compare(o, next) == 0 : ((Comparable) o).compareTo(next) == 0)) {
83  						stillEqual = true;
84  						index++;
85  					}
86  				}
87  			}
88  
89  			// translate index
90  			else
91  				index = -index - 1;
92  
93  			if (duplicate)
94  				super.add(index + 1, o);
95  			else
96  				super.add(index, o);
97  		}
98  		return true;
99  	}
100 
101 	//
102 	// DISABLED OPERATIONS
103 	// 
104 
105 	public void add(int index, Object o) {
106 		throw new UnsupportedOperationException("This is a sorted list; it is illegal to specify the element position");
107 	}
108 
109 	public boolean addAll(int index, Collection c) {
110 		throw new UnsupportedOperationException("This is a sorted list; it is illegal to specify the element position");
111 	}
112 
113 	public Object set(int index, Object o) {
114 		throw new UnsupportedOperationException("This is a sorted list; it is illegal to specify the element position");
115 	}
116 
117 }