有谁知道一个可以根据时间间隔来操纵/组织数据的图书馆吗?

例如:


我有一个按日期/时间映射到对象的数据集合[可以是任何东西,不相关数据的列表,字符串,数字等]
我希望某个Collection具有足够的通用性,以便它可以处理区间搜索,组织,合并,交集,差异和区间更改[即集合将重新组织15分钟的间隔,而间隔为1天]


我当时正在考虑编写其中之一,但我不想重蹈覆辙。

另外,最后一条规定是,我希望它使用Java。

例如:


  您具有以下数据:
  
  
  1/1/11 1:01-“鲍勃进入了房间”
  1/1/11 12:01-“杰里进入房间”
  1/1/11 1:31-“萨利进入房间”
  1/1/11 1:51-“乔治进入房间”
  1/1/11 2:01-“迪尔伯特进入了房间”
  11/1/21 1:01-“鲍勃进入了房间”
  11/1/3/12:“杰里进入房间”
  11/1/21 1:31-“萨利进入房间”
  11/1/21 1:51-“乔治进入房间”
  
  
  此处所述的所有输入项(作为2值基准)将进入集合[即]:add(Date,object)
     但是,在初始化时将设置间隔。 [即15分钟]
     因此,如果间隔为15分钟[并查询了特定时间。它会产生:
  
  
  1/1/11 12:00-12:15
  
  
  “杰里进入房间了”
  
  1/1/11 1:00-1:15
      -“鲍勃进入了房间”
  1/1/11 1:15-1:30
  1/1/11 1:30-1:45
  
  
  1/1/11 1:31-“萨利进入房间”
  ....
  
  
  如果您尝试查询1/1/11 12:09,则会得到1/1/11 12:00-12:15的结果。
  是的,我知道有一些极端情况,但这是一个例子。

最佳答案

这是TreeMap包装器的草图,该包装器基本上完成了示例所显示的内容:

public class CalendarMap<V> {

    private TreeMap<Calendar, V> map = new TreeMap<Calendar, V>();

    public void put(Calendar d, V v){
        map.put(d, v);
    }

    public void query(Calendar d, int intervalUnit, int intervalValue){
         DateFormat df = new SimpleDateFormat("MMM dd, yyyy");
         DateFormat tf = new SimpleDateFormat("HH:mm");

        // snap closest prior unit
        d.set(intervalUnit, (d.get(intervalUnit) / intervalValue)* intervalValue);
        Calendar next = new GregorianCalendar();
        next.setTime(d.getTime());
        next.add(intervalUnit, intervalValue);

        Calendar lastHit = null; // last hit

        while(d.before(map.lastKey())){
            SortedMap<Calendar, V> hits = map.subMap(d, true, next, false);
            if(!hits.isEmpty()){
                if(lastHit != null){
                    System.out.println(df.format(lastHit.getTime()) + " " + tf.format(lastHit.getTime()) + " - " +
                            df.format(d.getTime()) + " " + tf.format(d.getTime()) + ": N/A");
                    lastHit = null;
                }
                System.out.println(df.format(d.getTime()) + " " + tf.format(d.getTime()) + "-" + tf.format(next.getTime()) + ":");
                for(Entry<Calendar, V> entry : hits.entrySet()){
                    System.out.println("  " + tf.format(entry.getKey().getTime()) + " - " + entry.getValue());
                }
            }else if(lastHit == null){
                lastHit = new GregorianCalendar();
                lastHit.setTime(d.getTime());
            }
            d.add(intervalUnit, intervalValue);
            next.add(intervalUnit, intervalValue);
        }
    }

    public static void main(String[] args){
        CalendarMap<String> map = new CalendarMap<String>();
        map.put(new GregorianCalendar(2011, 1, 1, 13, 1), "Bob entered the room");
        map.put(new GregorianCalendar(2011, 1, 1, 12, 1), "Jerry entered the room");
        map.put(new GregorianCalendar(2011, 1, 1, 13, 31), "Sally entered the room");
        map.put(new GregorianCalendar(2011, 1, 1, 14, 1), "Dilbert entered the room");
        map.put(new GregorianCalendar(2011, 1, 2, 13, 1), "Bob entered the room");
        map.put(new GregorianCalendar(2011, 1, 3, 12, 1), "Jerry entered the room");
        map.put(new GregorianCalendar(2011, 1, 2, 13, 31), "Sally entered the room");
        map.put(new GregorianCalendar(2011, 1, 2, 13, 51), "Jorge entered the room");

        map.query(new GregorianCalendar(2011, 1, 1, 12, 9), Calendar.MINUTE, 15);
    }

}


这将产生:

Feb 01, 2011 12:00-12:15:
  12:01 - Jerry entered the room
Feb 01, 2011 12:15 - Feb 01, 2011 13:00: N/A
Feb 01, 2011 13:00-13:15:
  13:01 - Bob entered the room
Feb 01, 2011 13:15 - Feb 01, 2011 13:30: N/A
Feb 01, 2011 13:30-13:45:
  13:31 - Sally entered the room
Feb 01, 2011 13:45 - Feb 01, 2011 14:00: N/A
Feb 01, 2011 14:00-14:15:
  14:01 - Dilbert entered the room
Feb 01, 2011 14:15 - Feb 02, 2011 13:00: N/A
Feb 02, 2011 13:00-13:15:
  13:01 - Bob entered the room
Feb 02, 2011 13:15 - Feb 02, 2011 13:30: N/A
Feb 02, 2011 13:30-13:45:
  13:31 - Sally entered the room
Feb 02, 2011 13:45-14:00:
  13:51 - Jorge entered the room
Feb 02, 2011 14:00 - Feb 03, 2011 12:00: N/A
Feb 03, 2011 12:00-12:15:
  12:01 - Jerry entered the room

Suppose instead you execute this:

map.query(new GregorianCalendar(2011, 1, 1, 12, 9), Calendar.HOUR, 1);


然后输出是:

2011年2月1日12:09-13:09:
  13:01-鲍勃进入房间
2011年2月1日13:09-14:09:
  13:31-萨利进入房间
  14:01-Dilbert进入房间
2011年2月1日14:09-2011年2月2日12:09:不适用
2011年2月2日12:09-13:09:
  13:01-鲍勃进入房间
2011年2月2日13:09-14:09:
  13:31-萨利进入房间
  13:51-豪尔赫进入房间
2011年2月2日14:09-2011年2月3日11:09:不适用
2011年2月3日11:09-12:09:
  12:01-杰里进入房间

关于java - 有谁知道一个库会根据时间间隔处理数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6886169/

10-11 22:25
查看更多