View Javadoc
1   /*
2    * Copyright (C) 2012 The Guava 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 com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.j2objc.annotations.WeakOuter;
21  import java.util.Comparator;
22  import java.util.Iterator;
23  import java.util.NavigableSet;
24  import java.util.Set;
25  
26  /**
27   * A skeleton implementation of a descending multiset.  Only needs
28   * {@code forwardMultiset()} and {@code entryIterator()}.
29   *
30   * @author Louis Wasserman
31   */
32  @GwtCompatible(emulated = true)
33  abstract class DescendingMultiset<E> extends ForwardingMultiset<E> implements SortedMultiset<E> {
34    abstract SortedMultiset<E> forwardMultiset();
35  
36    private transient Comparator<? super E> comparator;
37  
38    @Override
39    public Comparator<? super E> comparator() {
40      Comparator<? super E> result = comparator;
41      if (result == null) {
42        return comparator = Ordering.from(forwardMultiset().comparator()).<E>reverse();
43      }
44      return result;
45    }
46  
47    private transient NavigableSet<E> elementSet;
48  
49    @Override
50    public NavigableSet<E> elementSet() {
51      NavigableSet<E> result = elementSet;
52      if (result == null) {
53        return elementSet = new SortedMultisets.NavigableElementSet<E>(this);
54      }
55      return result;
56    }
57  
58    @Override
59    public Entry<E> pollFirstEntry() {
60      return forwardMultiset().pollLastEntry();
61    }
62  
63    @Override
64    public Entry<E> pollLastEntry() {
65      return forwardMultiset().pollFirstEntry();
66    }
67  
68    @Override
69    public SortedMultiset<E> headMultiset(E toElement, BoundType boundType) {
70      return forwardMultiset().tailMultiset(toElement, boundType).descendingMultiset();
71    }
72  
73    @Override
74    public SortedMultiset<E> subMultiset(
75        E fromElement, BoundType fromBoundType, E toElement, BoundType toBoundType) {
76      return forwardMultiset()
77          .subMultiset(toElement, toBoundType, fromElement, fromBoundType)
78          .descendingMultiset();
79    }
80  
81    @Override
82    public SortedMultiset<E> tailMultiset(E fromElement, BoundType boundType) {
83      return forwardMultiset().headMultiset(fromElement, boundType).descendingMultiset();
84    }
85  
86    @Override
87    protected Multiset<E> delegate() {
88      return forwardMultiset();
89    }
90  
91    @Override
92    public SortedMultiset<E> descendingMultiset() {
93      return forwardMultiset();
94    }
95  
96    @Override
97    public Entry<E> firstEntry() {
98      return forwardMultiset().lastEntry();
99    }
100 
101   @Override
102   public Entry<E> lastEntry() {
103     return forwardMultiset().firstEntry();
104   }
105 
106   abstract Iterator<Entry<E>> entryIterator();
107 
108   private transient Set<Entry<E>> entrySet;
109 
110   @Override
111   public Set<Entry<E>> entrySet() {
112     Set<Entry<E>> result = entrySet;
113     return (result == null) ? entrySet = createEntrySet() : result;
114   }
115 
116   Set<Entry<E>> createEntrySet() {
117     @WeakOuter
118     class EntrySetImpl extends Multisets.EntrySet<E> {
119       @Override
120       Multiset<E> multiset() {
121         return DescendingMultiset.this;
122       }
123 
124       @Override
125       public Iterator<Entry<E>> iterator() {
126         return entryIterator();
127       }
128 
129       @Override
130       public int size() {
131         return forwardMultiset().entrySet().size();
132       }
133     }
134     return new EntrySetImpl();
135   }
136 
137   @Override
138   public Iterator<E> iterator() {
139     return Multisets.iteratorImpl(this);
140   }
141 
142   @Override
143   public Object[] toArray() {
144     return standardToArray();
145   }
146 
147   @Override
148   public <T> T[] toArray(T[] array) {
149     return standardToArray(array);
150   }
151 
152   @Override
153   public String toString() {
154     return entrySet().toString();
155   }
156 }