package com.jamonapi.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/** Buffer used to keep the last N recent values based on the comparator. Note the Comparator must
* be thread safe.
*/
public class NExtremeBufferHolder implements BufferHolder, Comparator {
private static final long serialVersionUID = 278L;
private ObjectnextToRemove;
private Listlist=new ArrayList();
private static final int EQUALTO=0;
private static final int GREATERTHAN=1;
private Comparatorcomparator;
public NExtremeBufferHolder(Comparatorcomparator) {
this.comparator=comparator;
}
public NExtremeBufferHolder(boolean naturalOrder) {
comparator=new JAMonComparator(naturalOrder);
}
public ListgetCollection() {
return list;
}
public void setComparator(Comparatorcomparator) {
this.comparator=comparator;
}
public ComparatorgetComparator() {
return comparator;
}
private ObjectgetNextToRemove() {
return Collections.min(list, comparator);
}
private boolean isTrue(int newVal) {
return (newVal==GREATERTHAN || newVal==EQUALTO) ? true : false;
}
/**
* Method used by the comparator interface.
* <pre>
* o1 < o2 - returns a negative integer
* o1 == o2 - returns zero
* o1 > o2 - returns a postitive integer
* </pre>
*
* Iterate through all columns that should be compared (in the proper order)
* and call the Comparator for each of the column elements.
* Note the column value is always the first argument and the comparison value is always the second
*
* Returns
* <pre>
* this object is less than - returns a negative integer,
* this object equal to - retutns zero
* this object greater than - return positive integer
* </pre>
*/
public int compare(ObjectnewObj, ObjectexistingObj) {
return comparator.compare(newObj, existingObj);
}
public void add(ObjectreplaceWithObj) {
list.add(replaceWithObj);
}
public void remove(ObjectreplaceWithObj) {
list.remove(nextToRemove);
nextToRemove=getNextToRemove();
}
public boolean shouldReplaceWith(ObjectreplaceWithObj) {
if (nextToRemove==null)
nextToRemove=getNextToRemove();
return isTrue(compare(replaceWithObj, nextToRemove));
}
public ListgetOrderedCollection() {
Collections.sort(list, comparator);
return list;
}
public void setCollection(Listlist) {
this.list=list;
}
public BufferHoldercopy() {
return new NExtremeBufferHolder(comparator);
}
}