persolijn

an efficient router for busses
Log | Files | Refs

Interval.java (1666B)


      1 package osm.routing.entity;
      2 
      3 public class Interval {
      4     public static final long DEFAULT_INTERVAL = 30 * 60; // 30 min
      5 
      6     public static enum IntervalFinal {
      7         START,
      8         END,
      9         NONE
     10     }
     11 
     12     public final IntervalFinal isFinal;
     13     public final long start;
     14     public final long end;
     15 
     16     public Interval(IntervalFinal isFinal, long start, long end) {
     17         this.isFinal = isFinal;
     18         this.start = start;
     19         this.end = end;
     20     }
     21 
     22     public static Interval fromInterval(IntervalFinal isFinal, long time) {
     23         return fromInterval(isFinal, time, DEFAULT_INTERVAL);
     24     }
     25 
     26     public static Interval fromInterval(IntervalFinal isFinal, long time, long offset) {
     27         long start, end;
     28         switch (isFinal) {
     29             case START:
     30                 start = time;
     31                 end = time + offset;
     32                 break;
     33             case END:
     34                 start = time - offset;
     35                 end = time;
     36                 break;
     37             default: // NONE
     38                 start = time - (offset / 2);
     39                 end = time + (offset / 2);
     40         }
     41         return new Interval(isFinal, start, end);
     42     }
     43 
     44     public boolean isBetweenTime(long time) {
     45         return switch (isFinal) {
     46             case START -> time > start;
     47             case END -> time < end;
     48             case NONE -> true;
     49         };
     50     }
     51 
     52     public long outsideTime(long time) {
     53         if (!isBetweenTime(time))
     54             return -1;
     55 
     56         return Math.max(0, switch (isFinal) {
     57             case START -> time - end;
     58             case END -> start - time;
     59             case NONE -> Math.max(start - time, time - end);
     60         });
     61     }
     62 
     63 }