All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.soulgalore.velocity.PopularListTool Maven / Gradle / Ivy

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;
    }

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy