1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.google.common.collect; 18 19 import com.google.common.annotations.Beta; 20 import com.google.common.annotations.GwtCompatible; 21 22 import java.util.Collection; 23 import java.util.Comparator; 24 import java.util.Iterator; 25 import java.util.SortedSet; 26 27 /** 28 * A {@link Multiset} which maintains the ordering of its elements, according to 29 * either their natural order or an explicit {@link Comparator}. In all cases, 30 * this implementation uses {@link Comparable#compareTo} or 31 * {@link Comparator#compare} instead of {@link Object#equals} to determine 32 * equivalence of instances. 33 * 34 * <p><b>Warning:</b> The comparison must be <i>consistent with equals</i> as 35 * explained by the {@link Comparable} class specification. Otherwise, the 36 * resulting multiset will violate the {@link Collection} contract, which it is 37 * specified in terms of {@link Object#equals}. 38 * 39 * @author Louis Wasserman 40 * @since 11.0 41 */ 42 @Beta 43 @GwtCompatible 44 public interface SortedMultiset<E> extends Multiset<E>, SortedIterable<E> { 45 /** 46 * Returns the comparator that orders this multiset, or 47 * {@link Ordering#natural()} if the natural ordering of the elements is used. 48 */ comparator()49 Comparator<? super E> comparator(); 50 51 /** 52 * Returns the entry of the first element in this multiset, or {@code null} if 53 * this multiset is empty. 54 */ firstEntry()55 Entry<E> firstEntry(); 56 57 /** 58 * Returns the entry of the last element in this multiset, or {@code null} if 59 * this multiset is empty. 60 */ lastEntry()61 Entry<E> lastEntry(); 62 63 /** 64 * Returns and removes the entry associated with the lowest element in this 65 * multiset, or returns {@code null} if this multiset is empty. 66 */ pollFirstEntry()67 Entry<E> pollFirstEntry(); 68 69 /** 70 * Returns and removes the entry associated with the greatest element in this 71 * multiset, or returns {@code null} if this multiset is empty. 72 */ pollLastEntry()73 Entry<E> pollLastEntry(); 74 75 /** 76 * Returns a {@link SortedSet} view of the distinct elements in this multiset. 77 */ elementSet()78 @Override SortedSet<E> elementSet(); 79 80 /** 81 * {@inheritDoc} 82 * 83 * <p>The iterator returns the elements in ascending order according to this 84 * multiset's comparator. 85 */ iterator()86 @Override Iterator<E> iterator(); 87 88 /** 89 * Returns a descending view of this multiset. Modifications made to either 90 * map will be reflected in the other. 91 */ descendingMultiset()92 SortedMultiset<E> descendingMultiset(); 93 94 /** 95 * Returns a view of this multiset restricted to the elements less than 96 * {@code upperBound}, optionally including {@code upperBound} itself. The 97 * returned multiset is a view of this multiset, so changes to one will be 98 * reflected in the other. The returned multiset supports all operations that 99 * this multiset supports. 100 * 101 * <p>The returned multiset will throw an {@link IllegalArgumentException} on 102 * attempts to add elements outside its range. 103 */ headMultiset(E upperBound, BoundType boundType)104 SortedMultiset<E> headMultiset(E upperBound, BoundType boundType); 105 106 /** 107 * Returns a view of this multiset restricted to the range between 108 * {@code lowerBound} and {@code upperBound}. The returned multiset is a view 109 * of this multiset, so changes to one will be reflected in the other. The 110 * returned multiset supports all operations that this multiset supports. 111 * 112 * <p>The returned multiset will throw an {@link IllegalArgumentException} on 113 * attempts to add elements outside its range. 114 * 115 * <p>This method is equivalent to 116 * {@code tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, 117 * upperBoundType)}. 118 */ subMultiset(E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType)119 SortedMultiset<E> subMultiset(E lowerBound, BoundType lowerBoundType, 120 E upperBound, BoundType upperBoundType); 121 122 /** 123 * Returns a view of this multiset restricted to the elements greater than 124 * {@code lowerBound}, optionally including {@code lowerBound} itself. The 125 * returned multiset is a view of this multiset, so changes to one will be 126 * reflected in the other. The returned multiset supports all operations that 127 * this multiset supports. 128 * 129 * <p>The returned multiset will throw an {@link IllegalArgumentException} on 130 * attempts to add elements outside its range. 131 */ tailMultiset(E lowerBound, BoundType boundType)132 SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType); 133 } 134