View Javadoc

1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.util;
22  
23  import java.util.Collection;
24  import java.util.Comparator;
25  import java.util.Iterator;
26  import java.util.SortedSet;
27  import java.util.TreeSet;
28  
29  /**
30   * Simple {@link java.util.SortedSet} implementation that uses an internal
31   * {@link java.util.TreeSet} to provide ordering. All mutation operations
32   * create a new copy of the <code>TreeSet</code> instance, so are very
33   * expensive.  This class is only intended for use on small, very rarely
34   * written collections that expect highly concurrent reads. Read operations
35   * are performed on a reference to the internal <code>TreeSet</code> at the
36   * time of invocation, so will not see any mutations to the collection during
37   * their operation.
38   *
39   * <p>Note that due to the use of a {@link java.util.TreeSet} internally,
40   * a {@link java.util.Comparator} instance must be provided, or collection
41   * elements must implement {@link java.lang.Comparable}.
42   * </p>
43   * @param <E> A class implementing {@link java.lang.Comparable} or able to be
44   * compared by a provided comparator.
45   */
46  public class SortedCopyOnWriteSet<E> implements SortedSet<E> {
47    private SortedSet<E> internalSet;
48  
49    public SortedCopyOnWriteSet() {
50      this.internalSet = new TreeSet<E>();
51    }
52  
53    public SortedCopyOnWriteSet(Collection<? extends E> c) {
54      this.internalSet = new TreeSet<E>(c);
55    }
56  
57    public SortedCopyOnWriteSet(Comparator<? super E> comparator) {
58      this.internalSet = new TreeSet<E>(comparator);
59    }
60  
61    @Override
62    public int size() {
63      return internalSet.size();
64    }
65  
66    @Override
67    public boolean isEmpty() {
68      return internalSet.isEmpty();
69    }
70  
71    @Override
72    public boolean contains(Object o) {
73      return internalSet.contains(o);
74    }
75  
76    @Override
77    public Iterator<E> iterator() {
78      return internalSet.iterator();
79    }
80  
81    @Override
82    public Object[] toArray() {
83      return internalSet.toArray();
84    }
85  
86    @Override
87    public <T> T[] toArray(T[] a) {
88      return internalSet.toArray(a);
89    }
90  
91    @Override
92    public synchronized boolean add(E e) {
93      SortedSet<E> newSet = new TreeSet<E>(internalSet);
94      boolean added = newSet.add(e);
95      internalSet = newSet;
96      return added;
97    }
98  
99    @Override
100   public synchronized boolean remove(Object o) {
101     SortedSet<E> newSet = new TreeSet<E>(internalSet);
102     boolean removed = newSet.remove(o);
103     internalSet = newSet;
104     return removed;
105   }
106 
107   @Override
108   public boolean containsAll(Collection<?> c) {
109     return internalSet.containsAll(c);
110   }
111 
112   @Override
113   public synchronized boolean addAll(Collection<? extends E> c) {
114     SortedSet<E> newSet = new TreeSet<E>(internalSet);
115     boolean changed = newSet.addAll(c);
116     internalSet = newSet;
117     return changed;
118   }
119 
120   @Override
121   public synchronized boolean retainAll(Collection<?> c) {
122     SortedSet<E> newSet = new TreeSet<E>(internalSet);
123     boolean changed = newSet.retainAll(c);
124     internalSet = newSet;
125     return changed;
126   }
127 
128   @Override
129   public synchronized boolean removeAll(Collection<?> c) {
130     SortedSet<E> newSet = new TreeSet<E>(internalSet);
131     boolean changed = newSet.removeAll(c);
132     internalSet = newSet;
133     return changed;
134   }
135 
136   @Override
137   public synchronized void clear() {
138     Comparator<? super E> comparator = internalSet.comparator();
139     if (comparator != null) {
140       internalSet = new TreeSet<E>(comparator);
141     } else {
142       internalSet = new TreeSet<E>();
143     }
144   }
145 
146   @Override
147   public Comparator<? super E> comparator() {
148     return internalSet.comparator();
149   }
150 
151   @Override
152   public SortedSet<E> subSet(E fromElement, E toElement) {
153     return internalSet.subSet(fromElement, toElement);
154   }
155 
156   @Override
157   public SortedSet<E> headSet(E toElement) {
158     return internalSet.headSet(toElement);
159   }
160 
161   @Override
162   public SortedSet<E> tailSet(E fromElement) {
163     return internalSet.tailSet(fromElement);
164   }
165 
166   @Override
167   public E first() {
168     return internalSet.first();
169   }
170 
171   @Override
172   public E last() {
173     return internalSet.last();
174   }
175 }