Advertisement
Filip_Markoski

[NP] 4.2 Генерички распоредувач (Solved)

Oct 25th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.55 KB | None | 0 0
  1. import java.time.Instant;
  2. import java.time.LocalDateTime;
  3. import java.time.ZoneId;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.stream.Collectors;
  9.  
  10. class Timestamp<T> implements Comparable<Timestamp<?>> {
  11.     private final LocalDateTime time;
  12.     private final T element;
  13.  
  14.     public Timestamp(LocalDateTime time, T element) {
  15.         this.time = time;
  16.         this.element = element;
  17.     }
  18.  
  19.     @Override
  20.     public String toString() {
  21.         return time.toString() + " " + element;
  22.     }
  23.  
  24.     public T getElement() {
  25.         return element;
  26.     }
  27.  
  28.     public LocalDateTime getTime() {
  29.         return time;
  30.     }
  31.  
  32.     @Override
  33.     public int compareTo(Timestamp<?> that) {
  34.         return this.time.compareTo(that.time);
  35.     }
  36.  
  37.     @Override
  38.     public boolean equals(Object o) {
  39.         //if (this == o) return true;
  40.         //if (o == null || getClass() != o.getClass()) return false;
  41.  
  42.         Timestamp<?> timestamp = (Timestamp<?>) o;
  43.  
  44.         //if (time != null ? !time.equals(timestamp.time) : timestamp.time != null) return false;
  45.         //element != null ? element.equals(timestamp.element) : timestamp.element == null;
  46.         // && timestamp.element.equals(element);
  47.         return this.time.equals(timestamp.time);
  48.     }
  49.  
  50.     @Override
  51.     public int hashCode() {
  52.         int result = time != null ? time.hashCode() : 0;
  53.         result = 31 * result + (element != null ? element.hashCode() : 0);
  54.         return result;
  55.     }
  56. }
  57.  
  58. class Scheduler<T> {
  59.     /* If the task desires add & remove methods use an ArrayList */
  60.     private ArrayList<Timestamp<T>> timestamps;
  61.  
  62.     public Scheduler() {
  63.         this.timestamps = new ArrayList<Timestamp<T>>();
  64.     }
  65.  
  66.     public void add(Timestamp<T> stamp) {
  67.         timestamps.add(stamp);
  68.     }
  69.  
  70.     public boolean remove(Timestamp<T> stamp) {
  71.         if (timestamps.contains(stamp)) {
  72.             timestamps.remove(stamp);
  73.             return true;
  74.         } else {
  75.             return false;
  76.         }
  77.     }
  78.  
  79.     public Timestamp<T> next() {
  80.         LocalDateTime now = LocalDateTime.now();
  81.         LocalDateTime next = LocalDateTime.MAX;
  82.         T temp = null;
  83.         for (int i = 0; i < timestamps.size(); i++) {
  84.             LocalDateTime current = timestamps.get(i).getTime();
  85.             /* Basically current needs to be in the range(present, future)*/
  86.             if (now.isBefore(current)) {
  87.                 if (current.isBefore(next)) {
  88.                     next = current;
  89.                     temp = timestamps.get(i).getElement();
  90.                 }
  91.             }
  92.         }
  93.         return new Timestamp<T>(next, temp);
  94.     }
  95.  
  96.     public Timestamp<T> last() {
  97.         LocalDateTime now = LocalDateTime.now();
  98.         LocalDateTime last = LocalDateTime.MIN;
  99.         T temp = null;
  100.         for (Timestamp<T> t : timestamps) {
  101.             /* Basically current needs to be in the range(past, present)*/
  102.             LocalDateTime current = t.getTime();
  103.             if (current.isBefore(now)) {
  104.                 if (current.isAfter(last)) {
  105.                     last = current;
  106.                     temp = t.getElement();
  107.                 }
  108.             }
  109.         }
  110.         return new Timestamp<T>(last, temp);
  111.     }
  112.  
  113.     public List<Timestamp<T>> getAll(LocalDateTime begin, LocalDateTime end) {
  114.         List<Timestamp<T>> result = new ArrayList<Timestamp<T>>();
  115.         for (Timestamp<T> t : timestamps) {
  116.             /* current should belong to range(begin, end)*/
  117.             LocalDateTime current = t.getTime();
  118.             if (current.isAfter(begin) && current.isBefore(end)) {
  119.                 result.add(t);
  120.             }
  121.         }
  122.         return result;
  123.     }
  124. }
  125.  
  126. public class SchedulerTest {
  127.  
  128.     static final LocalDateTime TIME = LocalDateTime.of(2016, 10, 25, 10, 15);
  129.  
  130.     public static void main(String[] args) {
  131.         Scanner jin = new Scanner(System.in);
  132.         int k = jin.nextInt();
  133.         if (k == 0) { //test Timestamp with String
  134.             Timestamp<String> t = new Timestamp<>(TIME, jin.next());
  135.             System.out.println(t);
  136.             System.out.println(t.getTime());
  137.             System.out.println(t.getElement());
  138.         }
  139.         if (k == 1) { //test Timestamp with ints
  140.             Timestamp<Integer> t1 = new Timestamp<>(TIME, jin.nextInt());
  141.             System.out.println(t1);
  142.             System.out.println(t1.getTime());
  143.             System.out.println(t1.getElement());
  144.             Timestamp<Integer> t2 = new Timestamp<>(TIME.plusDays(10), jin.nextInt());
  145.             System.out.println(t2);
  146.             System.out.println(t2.getTime());
  147.             System.out.println(t2.getElement());
  148.             System.out.println(t1.compareTo(t2));
  149.             System.out.println(t2.compareTo(t1));
  150.             System.out.println(t1.equals(t2));
  151.             System.out.println(t2.equals(t1));
  152.         }
  153.         if (k == 2) {//test Timestamp with String, complex
  154.             Timestamp<String> t1 = new Timestamp<>(ofEpochMS(jin.nextLong()), jin.next());
  155.             System.out.println(t1);
  156.             System.out.println(t1.getTime());
  157.             System.out.println(t1.getElement());
  158.             Timestamp<String> t2 = new Timestamp<>(ofEpochMS(jin.nextLong()), jin.next());
  159.             System.out.println(t2);
  160.             System.out.println(t2.getTime());
  161.             System.out.println(t2.getElement());
  162.             System.out.println(t1.compareTo(t2));
  163.             System.out.println(t2.compareTo(t1));
  164.             System.out.println(t1.equals(t2));
  165.             System.out.println(t2.equals(t1));
  166.         }
  167.         if (k == 3) { //test Scheduler with String
  168.             Scheduler<String> scheduler = new Scheduler<>();
  169.             LocalDateTime now = LocalDateTime.now();
  170.             scheduler.add(new Timestamp<>(now.minusHours(2), jin.next()));
  171.             scheduler.add(new Timestamp<>(now.minusHours(1), jin.next()));
  172.             scheduler.add(new Timestamp<>(now.minusHours(4), jin.next()));
  173.             scheduler.add(new Timestamp<>(now.plusHours(2), jin.next()));
  174.             scheduler.add(new Timestamp<>(now.plusHours(4), jin.next()));
  175.             scheduler.add(new Timestamp<>(now.plusHours(1), jin.next()));
  176.             scheduler.add(new Timestamp<>(now.plusHours(5), jin.next()));
  177.             System.out.println(scheduler.next().getElement());
  178.             System.out.println(scheduler.last().getElement());
  179.             List<Timestamp<String>> result = scheduler.getAll(now.minusHours(3), now.plusHours(4).plusMinutes(15));
  180.             String out = result.stream()
  181.                     .sorted()
  182.                     .map(Timestamp::getElement)
  183.                     .collect(Collectors.joining(", "));
  184.             System.out.println(out);
  185.         }
  186.         if (k == 4) {//test Scheduler with ints complex
  187.             Scheduler<Integer> scheduler = new Scheduler<>();
  188.             int counter = 0;
  189.             ArrayList<Timestamp<Integer>> forRemoval = new ArrayList<>();
  190.             while (jin.hasNextLong()) {
  191.                 Timestamp<Integer> ti = new Timestamp<>(ofEpochMS(jin.nextLong()), jin.nextInt());
  192.                 if ((counter & 7) == 0) {
  193.                     forRemoval.add(ti);
  194.                 }
  195.                 scheduler.add(ti);
  196.                 ++counter;
  197.             }
  198.             jin.next();
  199.  
  200.             while (jin.hasNextLong()) {
  201.                 LocalDateTime left = ofEpochMS(jin.nextLong());
  202.                 LocalDateTime right = ofEpochMS(jin.nextLong());
  203.                 List<Timestamp<Integer>> res = scheduler.getAll(left, right);
  204.                 Collections.sort(res);
  205.                 System.out.println(left + " <: " + print(res) + " >: " + right);
  206.             }
  207.             System.out.println("test");
  208.             List<Timestamp<Integer>> res = scheduler.getAll(ofEpochMS(0), ofEpochMS(Long.MAX_VALUE));
  209.             Collections.sort(res);
  210.             System.out.println(print(res));
  211.             forRemoval.forEach(scheduler::remove);
  212.             res = scheduler.getAll(ofEpochMS(0), ofEpochMS(Long.MAX_VALUE));
  213.             Collections.sort(res);
  214.             System.out.println(print(res));
  215.         }
  216.     }
  217.  
  218.     private static LocalDateTime ofEpochMS(long ms) {
  219.         return LocalDateTime.ofInstant(Instant.ofEpochMilli(ms), ZoneId.systemDefault());
  220.     }
  221.  
  222.     private static <T> String print(List<Timestamp<T>> res) {
  223.         if (res == null || res.size() == 0) return "NONE";
  224.         return res.stream()
  225.                 .map(each -> each.getElement().toString())
  226.                 .collect(Collectors.joining(", "));
  227.     }
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement