org.limewire.collection
Class FixedSizeSortedSet<E>
java.lang.Object
org.limewire.collection.FixedSizeSortedSet<E>
- All Implemented Interfaces:
- Iterable<E>
public class FixedSizeSortedSet<E>
- extends Object
- implements Iterable<E>
Gives a sorted Set of elements with a maximum size. Elements are sorted
upon insertion to the list, but only a fixed number of unique items are kept.
Therefore, a newly inserted element can push out items depending on its
insertion location. All inserted elements must implement the
Comparable interface. Additionally, FixedSizeSortedSet
does not accept duplicate elements.
hashCode() and equals(Object) are used to determine if an
existing item needs to be replaced, but compareTo is
used to determine which items are ejected. compareTo CAN be
different from hashCode and equals (which is
explicitly against the strong suggestion of compareTo and
equals, but is necessary for FixedSizeSortedSet to
function).
Note: FixedSizeSortedSet can use a class that has a natural
ordering that is inconsistent with equals.
FixedSizeSortedSet is not thread-safe.
//Consistent with equals
public class MyPersonName implements Comparable {
private int age ;
private String name;
MyPersonName(String name, int age){
this.name = name;
this.age = age;
}
public int age(){
return age;
}
public String name(){
return name;
}
public int compareTo(Object o){
MyPersonName p = (MyPersonName) o;
return this.name.compareTo(p.name());
}
public boolean equals(Object o){
MyPersonName p = (MyPersonName) o;
return name.equals(p.name());
}
public int hashCode(){
return 37*name.hashCode();
}
public String toString(){
return name + "'s age: " + age;
}
}
//Note: This class has a natural ordering that is inconsistent with equals
public class MyPersonAge implements Comparable {
private int age ;
private String name;
MyPersonAge(String name, int age){
this.name = name;
this.age = age;
}
public int age(){
return age;
}
public String name(){
return name;
}
public int compareTo(Object o){
MyPersonAge p = (MyPersonAge) o;
return this.age - p.age() ;
}
public boolean equals(Object o){
MyPersonAge p = (MyPersonAge) o;
return name.equals(p.name());
}
public int hashCode(){
return 37*name.hashCode();
}
public String toString(){
return name + "'s age: " + age;
}
}
void sampleCodeFixedSizeSortedSet(){
//compareTo by Age
System.out.println("When max. size met, evict people by oldest person.");
FixedSizeSortedSet<MyPersonAge> age = new FixedSizeSortedSet<MyPersonAge>(5);
age.add(new MyPersonAge("Abby", 23));
if(!age.add(new MyPersonAge("Abby", 27)))
System.out.println("Abby already contained in the collection; 23 is replaced with 27");
age.add(new MyPersonAge("Chris", 20));
age.add(new MyPersonAge("Bob", 28));
age.add(new MyPersonAge("Dan", 25));
age.add(new MyPersonAge("Eric", 24));
//max reached
age.add(new MyPersonAge("Fred", 22));
for(MyPersonAge o : age)
System.out.println(o.toString());
System.out.println("");
//compareTo by Name
System.out.println("When max. size met, evict people by alphabetical name.");
FixedSizeSortedSet<MyPersonName> name = new FixedSizeSortedSet<MyPersonName>(5);
name.add(new MyPersonName("Abby", 23));
if(!name.add(new MyPersonName("Abby", 27)))
System.out.println("Abby already contained in the collection; 23 is replaced with 27");
name.add(new MyPersonName("Chris", 20));
name.add(new MyPersonName("Bob", 28));
name.add(new MyPersonName("Dan", 25));
name.add(new MyPersonName("Eric", 24));
//max reached
name.add(new MyPersonName("Fred", 22));
for(MyPersonName o : name)
System.out.println(o.toString());
}
Output:
When max. size met, evict people by oldest person.
Abby already contained in the collection; 23 is replaced with 27
Chris's age: 20
Fred's age: 22
Eric's age: 24
Dan's age: 25
Abby's age: 27
When max. size met, evict people by alphabetical name.
Abby already contained in the collection; 23 is replaced with 27
Abby's age: 27
Bob's age: 28
Chris's age: 20
Dan's age: 25
Fred's age: 22
|
Constructor Summary |
FixedSizeSortedSet()
Constructs a FixedSizeSortedSet with a maximum size of 50. |
FixedSizeSortedSet(Comparator<? super E> c)
Constructs a FixedSizeSortedSet with the specified comparator
for the SortedSet and a maximum size of 50. |
FixedSizeSortedSet(Comparator<? super E> c,
int maxSize)
Constructs a FixedSizeSortedSet with the specified comparator
and maximum size. |
FixedSizeSortedSet(int size)
Constructs a FixedSizeSortedSet with a specified maximum size. |
|
Method Summary |
boolean |
add(E o)
Adds the object to the set. |
boolean |
addAll(Collection<? extends E> c)
Adds all the elements of the specified collection to this set. |
void |
clear()
Clears this FixedSizeSortedSet. |
Object |
clone()
|
boolean |
contains(Object o)
Determines if this set contains the specified object. |
boolean |
equals(Object o)
|
E |
first()
Returns the first element in the sorted set. |
E |
get(E o)
Retrieves the element that has an equals comparison with this
object and is in this FixedSizeSortedSet. |
int |
hashCode()
|
boolean |
isEmpty()
|
Iterator<E> |
iterator()
|
E |
last()
Returns the last element in the sorted set. |
boolean |
remove(E o)
Removes the specified object from this sorted set. |
int |
size()
|
FixedSizeSortedSet
public FixedSizeSortedSet()
- Constructs a FixedSizeSortedSet with a maximum size of 50.
FixedSizeSortedSet
public FixedSizeSortedSet(int size)
- Constructs a FixedSizeSortedSet with a specified maximum size.
FixedSizeSortedSet
public FixedSizeSortedSet(Comparator<? super E> c)
- Constructs a FixedSizeSortedSet with the specified comparator
for the SortedSet and a maximum size of 50.
FixedSizeSortedSet
public FixedSizeSortedSet(Comparator<? super E> c,
int maxSize)
- Constructs a FixedSizeSortedSet with the specified comparator
and maximum size.
clone
public Object clone()
throws CloneNotSupportedException
- Overrides:
clone in class Object
- Throws:
CloneNotSupportedException
add
public boolean add(E o)
- Adds the object to the set. If the object is already present,
(as specified by the Map's equals comparison), then it is ejected
and this newer version is used.
addAll
public boolean addAll(Collection<? extends E> c)
- Adds all the elements of the specified collection to this set.
get
public E get(E o)
- Retrieves the element that has an equals comparison with this
object and is in this FixedSizeSortedSet.
last
public E last()
- Returns the last element in the sorted set.
first
public E first()
- Returns the first element in the sorted set.
remove
public boolean remove(E o)
- Removes the specified object from this sorted set.
Equality is determined by equals, not compareTo.
clear
public void clear()
- Clears this FixedSizeSortedSet.
contains
public boolean contains(Object o)
- Determines if this set contains the specified object.
Equality is determined by equals, not compareTo.
equals
public boolean equals(Object o)
- Overrides:
equals in class Object
hashCode
public int hashCode()
- Overrides:
hashCode in class Object
isEmpty
public boolean isEmpty()
iterator
public Iterator<E> iterator()
- Specified by:
iterator in interface Iterable<E>
size
public int size()
Copyright © 2008 Lime Wire LLC. All Rights Reserved.