Advertisement
jovanovski

НП Лаб3 - Задача 2

Oct 27th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. import java.sql.Time;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.Date;
  6.  
  7. import javax.lang.model.element.Element;
  8.  
  9.  
  10. class Timestamp<T> implements Comparable<Timestamp<T>>{
  11.     final T element;
  12.     final Date data;
  13.    
  14.     public Timestamp(Date data, T element){
  15.         this.data = data;
  16.         this.element = element;
  17.     }
  18.    
  19.     public Date getDate(){
  20.         return this.data;
  21.     }
  22.    
  23.     public T getElement(){
  24.         return element;
  25.     }
  26.  
  27.     @Override
  28.     public int compareTo(Timestamp<T> o) {
  29.         if(this.data.before(o.data)) return -1;
  30.         if(this.data.after(o.data)) return 1;
  31.         else return 0;
  32.     }  
  33.    
  34.     public boolean equals(Object obj){
  35.         if(obj==null) return false;
  36.         if(obj.getClass()!=this.getClass()) return false;
  37.         @SuppressWarnings("unchecked")
  38.         Timestamp<T> tmp = (Timestamp<T>) obj;
  39.         return ((this.data.equals(tmp.data))&&(this.element.equals(tmp.element)));
  40.     }
  41.    
  42.     public String toString(){
  43.         return data.toString()+" "+element.toString();
  44.     }
  45. }
  46.  
  47. class Scheduler<T>{
  48.     ArrayList <Timestamp<T>> stamps;
  49.     private int broj;
  50.     @SuppressWarnings("unchecked")
  51.     public Scheduler(){
  52.         stamps = new ArrayList <Timestamp<T>>();
  53.         broj = 0;
  54.     }
  55.    
  56.     public void add(Timestamp<T> o) {
  57.         stamps.add(o);
  58.         broj++;
  59.     }
  60.    
  61.     public boolean remove(Timestamp<T> o) {
  62.         int index = -1;
  63.         for(int i=0;i<broj;i++){
  64.             if(stamps.get(i).equals(o)) index = i;
  65.         }
  66.         if(index==-1) return false;
  67.         stamps.remove(index);
  68.         broj--;
  69.         return true;
  70.     }
  71.    
  72.     public Timestamp<T> next(){
  73.         Collections.sort(stamps);
  74.         Date segasna = new Date();
  75.         Long vreme = segasna.getTime();
  76.         for(int i=0;i<broj;i++){
  77.             if(stamps.get(i).data.getTime()>vreme) return stamps.get(i);
  78.         }
  79.         return null;
  80.     }
  81.    
  82.     public Timestamp<T> last(){
  83.         Collections.sort(stamps);
  84.         Date segasna = new Date();
  85.         Long vreme = segasna.getTime();
  86.         for(int i=0;i<broj;i++){
  87.             if(stamps.get(i).data.getTime()>=vreme) return stamps.get(i-1);
  88.         }
  89.         return null;
  90.     }
  91.    
  92.     public ArrayList<Timestamp<T>> getAll(Date begin, Date end){
  93.         Collections.sort(stamps);
  94.         ArrayList <Timestamp<T>> list  = new ArrayList <Timestamp<T>>();
  95.             for(int i=0;i<broj;i++){
  96.                 if((stamps.get(i).data.after(begin))&&(stamps.get(i).data.before(end))) list.add(stamps.get(i));
  97.             }
  98.         return list;
  99.     }
  100.    
  101.  
  102. }
  103.  
  104. public class TimestampTest {
  105.  
  106.     @SuppressWarnings("deprecation")
  107.     public static void main(String[] args) {
  108.         Scheduler<String> shcheduler = new Scheduler<String>();
  109.         Date a = new Date();
  110.         shcheduler.add(new Timestamp<String>(a, "asd"));
  111.         Date a1 = new Date();
  112.         a1.setSeconds(a.getSeconds()-5);
  113.         shcheduler.add(new Timestamp<String>(a1, "qwe"));
  114.         Date a2 = new Date();
  115.         a2.setSeconds(a.getSeconds()-10);
  116.         shcheduler.add(new Timestamp<String>(a2, "zxc"));
  117.         Date a3 = new Date();
  118.         a3.setSeconds(a.getSeconds()+5);
  119.         shcheduler.add(new Timestamp<String>(a3, "vbn"));
  120.         Date a4 = new Date();
  121.         a4.setSeconds(a.getSeconds()+10);
  122.         shcheduler.add(new Timestamp<String>(a4, "try"));
  123.         System.out.println(shcheduler.next());
  124.         System.out.println(shcheduler.last());
  125.         Date a5 = new Date();
  126.         a5.setSeconds(a5.getSeconds()-1);
  127.         Date a6 = new Date();
  128.         a6.setSeconds(a5.getSeconds()+35);
  129.         System.out.println(shcheduler.getAll(a5, a6));
  130.         }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement