TimurMahammadov / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Multiset Entry Ordering #97

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Utility class to make certain operations on Multisets easier. For instance,
allows one to use max()/min() in the expected way.

Note: Hard to implement this as a singleton due to limitation of generics.
-can't hold a static instance of a specific type (could use map<class,
instance> trick)
-can't use Enum pattern with generic type (in my limited attempts)

Original issue reported on code.google.com by dereklee...@gmail.com on 24 Sep 2008 at 5:52

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for your submission.  Have you looked at Multisets.frequencyOrder()?  It 
is
close to what you've implemented except that (a) it doesn't break ties between
elements with equal frequency and (b) it's intended for use with elements of a
Multiset, rather than Multiset.Entry instances.

With it, your max() example can be implemented as follows:
Multisets.frequencyOrder(multiset).max(multiset.elementSet())

If you do need to break ties, you could use Comparators.compound():
Comparators.compound(Multisets.frequencyOrder(multiset), Ordering.natural())

FYI, I believe the most recent snapshot has Constraints.notNull() implemented 
as an
enum singleton, despite generics.  You can look at the details there, but the 
main
point is that we perform an "unsafe" cast that turns out to be safe.  It 
requires a
warning suppression, but it lets you create a real singleton.

Original comment by cpov...@google.com on 24 Sep 2008 at 3:18

GoogleCodeExporter commented 9 years ago
No, I somehow missed that. It's practically identical.

Thanks for the enum tips!

Original comment by dereklee...@gmail.com on 25 Sep 2008 at 2:36

GoogleCodeExporter commented 9 years ago
Closing, since the functionality is the same as Multisets.frequencyOrder(). 

Original comment by jared.l....@gmail.com on 25 Sep 2008 at 8:19