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 either their 028 * natural order or an explicit {@link Comparator}. This order is reflected when iterating over the 029 * sorted multiset, either directly, or through its {@code elementSet} or {@code entrySet} views. In 030 * all cases, this implementation uses {@link Comparable#compareTo} or {@link Comparator#compare} 031 * instead of {@link Object#equals} to determine equivalence of instances. 032 * 033 * <p><b>Warning:</b> The comparison must be <i>consistent with equals</i> as explained by the 034 * {@link Comparable} class specification. Otherwise, the resulting multiset will violate the {@link 035 * Collection} contract, which it is specified in terms of {@link Object#equals}. 036 * 037 * <p>See the Guava User Guide article on <a href= 038 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset"> {@code 039 * Multiset}</a>. 040 * 041 * @author Louis Wasserman 042 * @since 11.0 043 */ 044@GwtCompatible(emulated = true) 045public interface SortedMultiset<E> extends SortedMultisetBridge<E>, SortedIterable<E> { 046 /** 047 * Returns the comparator that orders this multiset, or {@link Ordering#natural()} if the natural 048 * ordering of the elements is used. 049 */ 050 Comparator<? super E> comparator(); 051 052 /** 053 * Returns the entry of the first element in this multiset, or {@code null} if this multiset is 054 * empty. 055 */ 056 Entry<E> firstEntry(); 057 058 /** 059 * Returns the entry of the last element in this multiset, or {@code null} if this multiset is 060 * empty. 061 */ 062 Entry<E> lastEntry(); 063 064 /** 065 * Returns and removes the entry associated with the lowest element in this multiset, or returns 066 * {@code null} if this multiset is empty. 067 */ 068 Entry<E> pollFirstEntry(); 069 070 /** 071 * Returns and removes the entry associated with the greatest element in this multiset, or returns 072 * {@code null} if this multiset is empty. 073 */ 074 Entry<E> pollLastEntry(); 075 076 /** 077 * Returns a {@link NavigableSet} view of the distinct elements in this multiset. 078 * 079 * @since 14.0 (present with return type {@code SortedSet} since 11.0) 080 */ 081 @Override 082 NavigableSet<E> elementSet(); 083 084 /** 085 * {@inheritDoc} 086 * 087 * <p>The {@code entrySet}'s iterator returns entries in ascending element order according to the 088 * this multiset's comparator. 089 */ 090 @Override 091 Set<Entry<E>> entrySet(); 092 093 /** 094 * {@inheritDoc} 095 * 096 * <p>The iterator returns the elements in ascending order according to this multiset's 097 * comparator. 098 */ 099 @Override 100 Iterator<E> iterator(); 101 102 /** 103 * Returns a descending view of this multiset. Modifications made to either map will be reflected 104 * in the other. 105 */ 106 SortedMultiset<E> descendingMultiset(); 107 108 /** 109 * Returns a view of this multiset restricted to the elements less than {@code upperBound}, 110 * optionally including {@code upperBound} itself. The returned multiset is a view of this 111 * multiset, so changes to one will be reflected in the other. The returned multiset supports all 112 * operations that this multiset supports. 113 * 114 * <p>The returned multiset will throw an {@link IllegalArgumentException} on attempts to add 115 * elements outside its range. 116 */ 117 SortedMultiset<E> headMultiset(E upperBound, BoundType boundType); 118 119 /** 120 * Returns a view of this multiset restricted to the range between {@code lowerBound} and {@code 121 * upperBound}. The returned multiset is a view of this multiset, so changes to one will be 122 * reflected in the other. The returned multiset supports all operations that this multiset 123 * supports. 124 * 125 * <p>The returned multiset will throw an {@link IllegalArgumentException} on attempts to add 126 * elements outside its range. 127 * 128 * <p>This method is equivalent to {@code tailMultiset(lowerBound, 129 * lowerBoundType).headMultiset(upperBound, upperBoundType)}. 130 */ 131 SortedMultiset<E> subMultiset( 132 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType); 133 134 /** 135 * Returns a view of this multiset restricted to the elements greater than {@code lowerBound}, 136 * optionally including {@code lowerBound} itself. The returned multiset is a view of this 137 * multiset, so changes to one will be reflected in the other. The returned multiset supports all 138 * operations that this multiset supports. 139 * 140 * <p>The returned multiset will throw an {@link IllegalArgumentException} on attempts to add 141 * elements outside its range. 142 */ 143 SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType); 144}