Skip to content

Instantly share code, notes, and snippets.

@l-ray
Created May 2, 2014 11:32
Show Gist options
  • Save l-ray/11472824 to your computer and use it in GitHub Desktop.
Save l-ray/11472824 to your computer and use it in GitHub Desktop.
package de.lray.intercom.highscore;
import de.lray.intercom.pattern.Strategy;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.assertArrayEquals;
/**
* Creates a topN - list of the highest numbers being added.
* Concrete Implementation of the {@link de.lray.intercom.pattern.Strategy}.
*/
public class TopNStrategy implements Strategy {
public static final int DEFAULT_HIGHSCORE_LENGTH = 5;
// representation of highest x numbers
private TreeSet<Integer> highscore;
// max number of ranking positions
private int numPos;
/**
* static constructor
* @param positions - number of ranks to hold
* @return - instance of TopNStrategy
*/
public static TopNStrategy createInstance(int positions) {
TopNStrategy instance = new TopNStrategy();
instance.numPos = positions;
return instance;
}
/**
* Should be private, but as the tests are in the same file, and those need an
* public constructor, here it is.
*/
public TopNStrategy() {
this.init();
}
/**
* Checks a given number whether it candidates for the current ranking.
*
* If the parameter is higher than the lowest rank or the rank is not already on max size,
* it will be added to the ranking.
*
* If the ranking now holds more elements then the max. number of ranks, the lowest rank is removed.
*
* @param number - candidate for ranking
*/
@Override
public void execute(Integer number) {
if (highscore.size() < this.numPos || number > highscore.last()) {
highscore.add(number);
}
if (highscore.size() > this.numPos) {
highscore.remove(highscore.last());
}
}
private void init() {
Comparator<Integer> reversedOrderComparator = new Comparator<Integer>(){
@Override
public int compare( Integer s1, Integer s2) {
return Integer.compare(s2,s1);
}
};
highscore = new TreeSet<>(reversedOrderComparator);
numPos = DEFAULT_HIGHSCORE_LENGTH;
}
/**
* Converts a given Integer Colection into a typed array.
*
* @param collection - flat list of Integers.
* @return - array with the same objects.
*/
private static Integer[] toArray(Collection<Integer> collection) {
return collection.toArray(new Integer[collection.size()]);
}
@Override
public Integer[] getResult() {
return toArray(highscore);
}
@Before
public void addDummyData(){
highscore.clear();
numPos = 5;
}
@Test
public void testFillingUp() {
numPos=3;
execute(20);
assertArrayEquals(new Integer[]{20}, getResult());
execute(2);
assertArrayEquals(new Integer[]{20, 2}, getResult());
execute(5);
assertArrayEquals(new Integer[]{20, 5, 2}, getResult());
execute(19);
assertArrayEquals(new Integer[]{20, 19, 5}, getResult());
}
@Test
public void testExecuteWithSmallerNumber() {
highscore.addAll(Arrays.asList(10, 9, 8, 6, 5));
execute(4);
assertArrayEquals(new Integer[]{10, 9, 8, 6, 5}, getResult());
}
@Test
public void testExecuteWithHigherNumber() {
highscore.addAll(Arrays.asList(10, 9, 8, 6, 5));
execute(11);
assertArrayEquals(new Integer[]{11, 10, 9, 8, 6}, getResult());
execute(Integer.MAX_VALUE);
assertArrayEquals(new Integer[]{Integer.MAX_VALUE, 11, 10, 9, 8}, getResult());
}
@Test
public void testExecuteWithExistingNumber() {
highscore.addAll(Arrays.asList(10, 9, 8, 6, 5));
execute(8);
assertArrayEquals(new Integer[]{10, 9, 8, 6, 5}, getResult());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment