aboutsummaryrefslogtreecommitdiffstats
path: root/guava/src/com/google/common/collect/SortedMultiset.java
blob: 0713151431a56989ad73fad2046804f6fa91b1a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (C) 2011 The Guava Authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.common.collect;

import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedSet;

/**
 * A {@link Multiset} which maintains the ordering of its elements, according to
 * either their natural order or an explicit {@link Comparator}. In all cases,
 * this implementation uses {@link Comparable#compareTo} or
 * {@link Comparator#compare} instead of {@link Object#equals} to determine
 * equivalence of instances.
 * 
 * <p><b>Warning:</b> The comparison must be <i>consistent with equals</i> as
 * explained by the {@link Comparable} class specification. Otherwise, the
 * resulting multiset will violate the {@link Collection} contract, which it is
 * specified in terms of {@link Object#equals}.
 * 
 * @author Louis Wasserman
 * @since 11.0
 */
@Beta
@GwtCompatible
public interface SortedMultiset<E> extends Multiset<E>, SortedIterable<E> {
  /**
   * Returns the comparator that orders this multiset, or
   * {@link Ordering#natural()} if the natural ordering of the elements is used.
   */
  Comparator<? super E> comparator();

  /**
   * Returns the entry of the first element in this multiset, or {@code null} if
   * this multiset is empty.
   */
  Entry<E> firstEntry();

  /**
   * Returns the entry of the last element in this multiset, or {@code null} if
   * this multiset is empty.
   */
  Entry<E> lastEntry();

  /**
   * Returns and removes the entry associated with the lowest element in this
   * multiset, or returns {@code null} if this multiset is empty.
   */
  Entry<E> pollFirstEntry();

  /**
   * Returns and removes the entry associated with the greatest element in this
   * multiset, or returns {@code null} if this multiset is empty.
   */
  Entry<E> pollLastEntry();

  /**
   * Returns a {@link SortedSet} view of the distinct elements in this multiset.
   */
  @Override SortedSet<E> elementSet();

  /**
   * {@inheritDoc}
   *
   * <p>The iterator returns the elements in ascending order according to this
   * multiset's comparator.
   */
  @Override Iterator<E> iterator();

  /**
   * Returns a descending view of this multiset. Modifications made to either
   * map will be reflected in the other.
   */
  SortedMultiset<E> descendingMultiset();

  /**
   * Returns a view of this multiset restricted to the elements less than
   * {@code upperBound}, optionally including {@code upperBound} itself. The
   * returned multiset is a view of this multiset, so changes to one will be
   * reflected in the other. The returned multiset supports all operations that
   * this multiset supports.
   * 
   * <p>The returned multiset will throw an {@link IllegalArgumentException} on
   * attempts to add elements outside its range.
   */
  SortedMultiset<E> headMultiset(E upperBound, BoundType boundType);

  /**
   * Returns a view of this multiset restricted to the range between
   * {@code lowerBound} and {@code upperBound}. The returned multiset is a view
   * of this multiset, so changes to one will be reflected in the other. The
   * returned multiset supports all operations that this multiset supports.
   * 
   * <p>The returned multiset will throw an {@link IllegalArgumentException} on
   * attempts to add elements outside its range.
   * 
   * <p>This method is equivalent to
   * {@code tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound,
   * upperBoundType)}.
   */
  SortedMultiset<E> subMultiset(E lowerBound, BoundType lowerBoundType,
      E upperBound, BoundType upperBoundType);

  /**
   * Returns a view of this multiset restricted to the elements greater than
   * {@code lowerBound}, optionally including {@code lowerBound} itself. The
   * returned multiset is a view of this multiset, so changes to one will be
   * reflected in the other. The returned multiset supports all operations that
   * this multiset supports.
   * 
   * <p>The returned multiset will throw an {@link IllegalArgumentException} on
   * attempts to add elements outside its range.
   */
  SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType);
}