Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Time;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Date;
- import javax.lang.model.element.Element;
- class Timestamp<T> implements Comparable<Timestamp<T>>{
- final T element;
- final Date data;
- public Timestamp(Date data, T element){
- this.data = data;
- this.element = element;
- }
- public Date getDate(){
- return this.data;
- }
- public T getElement(){
- return element;
- }
- @Override
- public int compareTo(Timestamp<T> o) {
- if(this.data.before(o.data)) return -1;
- if(this.data.after(o.data)) return 1;
- else return 0;
- }
- public boolean equals(Object obj){
- if(obj==null) return false;
- if(obj.getClass()!=this.getClass()) return false;
- @SuppressWarnings("unchecked")
- Timestamp<T> tmp = (Timestamp<T>) obj;
- return ((this.data.equals(tmp.data))&&(this.element.equals(tmp.element)));
- }
- public String toString(){
- return data.toString()+" "+element.toString();
- }
- }
- class Scheduler<T>{
- ArrayList <Timestamp<T>> stamps;
- private int broj;
- @SuppressWarnings("unchecked")
- public Scheduler(){
- stamps = new ArrayList <Timestamp<T>>();
- broj = 0;
- }
- public void add(Timestamp<T> o) {
- stamps.add(o);
- broj++;
- }
- public boolean remove(Timestamp<T> o) {
- int index = -1;
- for(int i=0;i<broj;i++){
- if(stamps.get(i).equals(o)) index = i;
- }
- if(index==-1) return false;
- stamps.remove(index);
- broj--;
- return true;
- }
- public Timestamp<T> next(){
- Collections.sort(stamps);
- Date segasna = new Date();
- Long vreme = segasna.getTime();
- for(int i=0;i<broj;i++){
- if(stamps.get(i).data.getTime()>vreme) return stamps.get(i);
- }
- return null;
- }
- public Timestamp<T> last(){
- Collections.sort(stamps);
- Date segasna = new Date();
- Long vreme = segasna.getTime();
- for(int i=0;i<broj;i++){
- if(stamps.get(i).data.getTime()>=vreme) return stamps.get(i-1);
- }
- return null;
- }
- public ArrayList<Timestamp<T>> getAll(Date begin, Date end){
- Collections.sort(stamps);
- ArrayList <Timestamp<T>> list = new ArrayList <Timestamp<T>>();
- for(int i=0;i<broj;i++){
- if((stamps.get(i).data.after(begin))&&(stamps.get(i).data.before(end))) list.add(stamps.get(i));
- }
- return list;
- }
- }
- public class TimestampTest {
- @SuppressWarnings("deprecation")
- public static void main(String[] args) {
- Scheduler<String> shcheduler = new Scheduler<String>();
- Date a = new Date();
- shcheduler.add(new Timestamp<String>(a, "asd"));
- Date a1 = new Date();
- a1.setSeconds(a.getSeconds()-5);
- shcheduler.add(new Timestamp<String>(a1, "qwe"));
- Date a2 = new Date();
- a2.setSeconds(a.getSeconds()-10);
- shcheduler.add(new Timestamp<String>(a2, "zxc"));
- Date a3 = new Date();
- a3.setSeconds(a.getSeconds()+5);
- shcheduler.add(new Timestamp<String>(a3, "vbn"));
- Date a4 = new Date();
- a4.setSeconds(a.getSeconds()+10);
- shcheduler.add(new Timestamp<String>(a4, "try"));
- System.out.println(shcheduler.next());
- System.out.println(shcheduler.last());
- Date a5 = new Date();
- a5.setSeconds(a5.getSeconds()-1);
- Date a6 = new Date();
- a6.setSeconds(a5.getSeconds()+35);
- System.out.println(shcheduler.getAll(a5, a6));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement