001/* 002 * Copyright (C) 2011 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 017package com.google.common.collect; 018 019import com.google.common.annotations.GwtCompatible; 020import java.util.Collection; 021import java.util.Comparator; 022import java.util.Iterator; 023import java.util.NavigableSet; 024import java.util.Set; 025 026/** 027 * A {@link Multiset} which maintains the ordering of its elements, according to 028 * either their natural order or an explicit {@link Comparator}. This order is 029 * reflected when iterating over the sorted multiset, either directly, or through 030 * its {@code elementSet} or {@code entrySet} views. In all cases, 031 * this implementation uses {@link Comparable#compareTo} or 032 * {@link Comparator#compare} instead of {@link Object#equals} to determine 033 * equivalence of instances. 034 * 035 * <p><b>Warning:</b> The comparison must be <i>consistent with equals</i> as 036 * explained by the {@link Comparable} class specification. Otherwise, the 037 * resulting multiset will violate the {@link Collection} contract, which it is 038 * specified in terms of {@link Object#equals}. 039 * 040 * <p>See the Guava User Guide article on <a href= 041 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset"> 042 * {@code Multiset}</a>. 043 * 044 * @author Louis Wasserman 045 * @since 11.0 046 */ 047@GwtCompatible(emulated = true) 048public interface SortedMultiset<E> extends SortedMultisetBridge<E>, SortedIterable<E> { 049 /** 050 * Returns the comparator that orders this multiset, or 051 * {@link Ordering#natural()} if the natural ordering of the elements is used. 052 */ 053 Comparator<? super E> comparator(); 054 055 /** 056 * Returns the entry of the first element in this multiset, or {@code null} if 057 * this multiset is empty. 058 */ 059 Entry<E> firstEntry(); 060 061 /** 062 * Returns the entry of the last element in this multiset, or {@code null} if 063 * this multiset is empty. 064 */ 065 Entry<E> lastEntry(); 066 067 /** 068 * Returns and removes the entry associated with the lowest element in this 069 * multiset, or returns {@code null} if this multiset is empty. 070 */ 071 Entry<E> pollFirstEntry(); 072 073 /** 074 * Returns and removes the entry associated with the greatest element in this 075 * multiset, or returns {@code null} if this multiset is empty. 076 */ 077 Entry<E> pollLastEntry(); 078 079 /** 080 * Returns a {@link NavigableSet} view of the distinct elements in this multiset. 081 * 082 * @since 14.0 (present with return type {@code SortedSet} since 11.0) 083 */ 084 @Override 085 NavigableSet<E> elementSet(); 086 087 /** 088 * {@inheritDoc} 089 * 090 * <p>The {@code entrySet}'s iterator returns entries in ascending element 091 * order according to the this multiset's comparator. 092 */ 093 @Override 094 Set<Entry<E>> entrySet(); 095 096 /** 097 * {@inheritDoc} 098 * 099 * <p>The iterator returns the elements in ascending order according to this 100 * multiset's comparator. 101 */ 102 @Override 103 Iterator<E> iterator(); 104 105 /** 106 * Returns a descending view of this multiset. Modifications made to either 107 * map will be reflected in the other. 108 */ 109 SortedMultiset<E> descendingMultiset(); 110 111 /** 112 * Returns a view of this multiset restricted to the elements less than 113 * {@code upperBound}, optionally including {@code upperBound} itself. The 114 * returned multiset is a view of this multiset, so changes to one will be 115 * reflected in the other. The returned multiset supports all operations that 116 * this multiset supports. 117 * 118 * <p>The returned multiset will throw an {@link IllegalArgumentException} on 119 * attempts to add elements outside its range. 120 */ 121 SortedMultiset<E> headMultiset(E upperBound, BoundType boundType); 122 123 /** 124 * Returns a view of this multiset restricted to the range between 125 * {@code lowerBound} and {@code upperBound}. The returned multiset is a view 126 * of this multiset, so changes to one will be reflected in the other. The 127 * returned multiset supports all operations that this multiset supports. 128 * 129 * <p>The returned multiset will throw an {@link IllegalArgumentException} on 130 * attempts to add elements outside its range. 131 * 132 * <p>This method is equivalent to 133 * {@code tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, 134 * upperBoundType)}. 135 */ 136 SortedMultiset<E> subMultiset( 137 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType); 138 139 /** 140 * Returns a view of this multiset restricted to the elements greater than 141 * {@code lowerBound}, optionally including {@code lowerBound} itself. The 142 * returned multiset is a view of this multiset, so changes to one will be 143 * reflected in the other. The returned multiset supports all operations that 144 * this multiset supports. 145 * 146 * <p>The returned multiset will throw an {@link IllegalArgumentException} on 147 * attempts to add elements outside its range. 148 */ 149 SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType); 150}