一、 现有一事件处理系统,事件的数据结构如下:
package com.cyaline;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Event {
/**
* Event ID, uniquly identify the event in the system
*/
private int id;
/**
* Name of the event.
*/
private String name;
/**
* Description of the event;
*/
private String description;
/**
* Time in milliseconds of the event
*/
private long time;
private Priority priority;
public enum Priority {
Critical, Warning, Information
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("ID:").append(id).append("\n");
sb.append("Name:").append(name).append("\n");
sb.append("Description:").append(description).append("\n");
sb.append("Time:").append(time).append("\n");
sb.append("Priority:").append(priority).append("\n");
return sb.toString();
}
/**
* If the event.time is equal, then return the most recent events in its
* priority order.
*
* @param events
* @return
*/
public static Event[] findMostRecentEvent(List<Event> events) {
//TODO:找出最近的事件,如果有多个最近的事件(事件时间相同),则按照优先度的重要性排序由高到低次序返回).
}
public static void main(String[] args) {
List<Event> events = new ArrayList<Event>();
{
Event event = new Event();
event.id = 1;
event.name = "PrinterError";
event.description = "Shortage of paper";
event.time = System.currentTimeMillis() - 2000000;
event.priority = Priority.Warning;
events.add(event);
}
{
Event event = new Event();
event.id = 2;
event.name = "DeviceMaintenance";
event.description = "Device is out of service now.";
event.time = System.currentTimeMillis() - 1000000;
event.priority = Priority.Information;
events.add(event);
}
{
Event event = new Event();
event.id = 3;
event.name = "DeviceError";
event.description = "Device is not connected.";
event.time = System.currentTimeMillis() - 1000000;
event.priority = Priority.Critical;
events.add(event);
}
{
Event event = new Event();
event.id = 4;
event.name = "DeviceBusy";
event.description = "Device is busy.";
event.time = System.currentTimeMillis() - 1000000;
event.priority = Priority.Warning;
events.add(event);
}
{
Event event = new Event();
event.id = 6;
event.name = "PrinterError";
event.description = "Shortage of paper";
event.time = System.currentTimeMillis() - 1500000;
event.priority = Priority.Warning;
events.add(event);
}
Event[] evs = Event.findMostRecentEvent(events);
for(Event event:evs){
System.out.println(event);
}
}
}