View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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 net.sf.ehcache.util;
17  
18  import java.util.AbstractCollection;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  /***
26   * Collection for large set. The general purpose is not to iterator through
27   * all the keys for add and remove operations.
28   * @author Nabib El-Rahman
29   * @param <E>
30   */
31  public abstract class LargeCollection < E > extends AbstractCollection < E > {
32  
33      /***
34       * Set that keeps tabs on add add() to collection.
35       */
36      private final Collection < E > addSet;
37  
38      /***
39       * Set that keeps tabs of all remove() to collection.
40       */
41      private final Collection < Object > removeSet;
42  
43      /***
44       * default constructor.
45       */
46      public LargeCollection() {
47        this.addSet = new HashSet();
48        this.removeSet = new HashSet();
49      }
50  
51      /***
52       * {@inheritDoc}
53       */
54      @Override
55      public final boolean add(final E obj) {
56        return this.addSet.add(obj);
57      }
58  
59      /***
60       * {@inheritDoc}
61       */
62      @Override
63      public final boolean contains(final Object obj) {
64        return !removeSet.contains(obj) ? addSet.contains(obj)
65          || super.contains(obj) : false;
66      }
67  
68      /***
69       * {@inheritDoc}
70       */
71      @Override
72      public final boolean remove(final Object obj) {
73        return removeSet.add(obj);
74      }
75  
76      /***
77       * {@inheritDoc}
78       */
79      public final boolean removeAll(final Collection < ? > removeCandidates) {
80        boolean remove = true;
81        for (Iterator iter = removeCandidates.iterator(); iter.hasNext();) {
82           remove = remove(iter.next()) & remove;
83        }
84        return remove;
85      }
86  
87      /***
88       * Iterator for addSet.
89       * @return Iterator < E >
90       */
91      private Iterator < E > additionalIterator() {
92        return addSet.iterator();
93      }
94  
95      /***
96       * {@inheritDoc}
97       */
98      public final Iterator < E > iterator() {
99        List < Iterator < E > > iterators = new ArrayList();
100       iterators.add(sourceIterator());
101       iterators.add(additionalIterator());
102       return new AggregateIterator(removeSet, iterators);
103     }
104 
105     /***
106      * {@inheritDoc}
107      */
108     public final int size() {
109       return sourceSize() + addSet.size() - removeSet.size();
110     }
111 
112     /***
113      * Iterator of initial set of entries.
114      * @return Iterator < E >
115      */
116     public abstract Iterator < E > sourceIterator();
117 
118     /***
119      * Initial set of entries size.
120      * @return integer
121      */
122     public abstract int sourceSize();
123 
124 }