com.soulgalore.velocity.PopularListTool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xml-velocity Show documentation
Show all versions of xml-velocity Show documentation
Merge a XML file with a Velocity template
The newest version!
package com.soulgalore.velocity;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class PopularListTool {
/**
* Hold all the assets
*/
private Map urlAndData = new HashMap();
// TODO sync
public void push(String url) {
UrlRequests data = urlAndData.get(url);
if (data == null)
urlAndData.put(url, new UrlRequests(url, 1));
else {
urlAndData.put(url, new UrlRequests(url, data.getNrOfTimes() + 1));
}
}
public Collection getMostPopularUrls(int maxUrls) {
List list = new LinkedList(urlAndData.values());
Collections.sort(list, new UrlRequestsComparator());
return list.subList(0, maxUrls > list.size() ? list.size() : maxUrls);
}
private class UrlRequestsComparator implements Comparator {
@Override
public int compare(UrlRequests o1, UrlRequests o2) {
if (o1.getNrOfTimes() < o2.getNrOfTimes())
return 1;
else if (o1.getNrOfTimes() > o2.getNrOfTimes())
return -1;
else
return 0;
}
}
}