View Javadoc

1   package org.lsst.ccs.plugin.jas3.trending.timeselection;
2   
3   import java.text.ParseException;
4   import java.text.SimpleDateFormat;
5   import java.util.Comparator;
6   import java.util.Date;
7   
8   /**
9    * Time window for use by trending plots.
10   *
11   * @author turri
12   */
13  public class TimeSelection {
14      
15  // -- Public fields : ----------------------------------------------------------
16      
17      static public final String DATE_PATTERN = "MM/dd/yyyy HH:mm:ss";
18      static public final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(DATE_PATTERN);
19      static private final String DELIMITER_RE = "\\$";
20      static private final String DELIMITER = "$";
21      
22      /** Time window name. */
23      public String name;
24      /** String that defines time window start. */
25      public String lowerEdgeString;
26      /** String that defines time window end. */
27      public String upperEdgeString;
28      
29  // -- Private parts : ----------------------------------------------------------
30      
31      private boolean persist; // true if this time window should be persisted between sessions
32      private long lastUsed; // time when this window was last selected
33      
34  // -- Construction : -----------------------------------------------------------
35      
36      public TimeSelection(String name, String start, String end, boolean persistent) {
37          this.name = name;
38          lowerEdgeString = start;
39          upperEdgeString = end;
40          persist = persistent;
41          lastUsed = System.currentTimeMillis();
42      }
43      
44  // -- Setters and getters : ----------------------------------------------------
45      
46      /** Returns the current value of this window start time in milliseconds. */
47      public long getLowerEdge() {
48          return getEdge(lowerEdgeString);
49      }
50      
51      /** Returns the current value of this window end time in milliseconds. */
52      public long getUpperEdge() {
53          return getEdge(upperEdgeString);
54      }
55      
56      private long getEdge(String edgeString) {
57          try {
58              Date d = DATE_FORMAT.parse(edgeString);
59              return d.getTime();
60          } catch (ParseException x) {
61          }
62          try {
63              return (long)(1000*TimeSelectionUtils.getSecondsForString(edgeString));        
64          } catch (Throwable t) {
65              throw new RuntimeException(t);
66          }
67      }
68      
69      /** Returns <tt>true</tt> if this time window should be persisted between sessions. */
70      public boolean isPersistent() {
71          return persist;
72      }
73      
74      /** Sets the flag that indicate whether or not this time window should be persisted between sessions. */
75      public void setPersistent(boolean persistent) {
76          persist = persistent;
77      }
78      
79      /** Returns the time in milliseconds when this time window was last selected. */
80      public long getLastUseTime() {
81          return lastUsed;
82      }
83      
84      /** Sets the time when this time window was last selected to the current system time. */
85      public void touch() {
86          lastUsed = System.currentTimeMillis();
87      }
88  
89      /** Returns user-readable string representation of this time window. */
90      @Override
91      public String toString() {
92          return name +" : "+ lowerEdgeString +" through "+ upperEdgeString;
93      }
94      
95      /** Returns string representation of this time window used for saving it in application properties. */
96      public String toCompressedString() {
97          return name + DELIMITER + lowerEdgeString + DELIMITER + upperEdgeString;
98      }
99      
100     /** Parses string representation of this time window used for saving it in application properties. */
101     static public TimeSelection parseCompressedString(String s) {
102         String[] tokens = s.split(DELIMITER_RE);
103         try {
104             TimeSelection ts = new TimeSelection(tokens[0], tokens[1], tokens[2], true);
105             ts.getLowerEdge();
106             ts.getUpperEdge();
107             return ts;
108         } catch (RuntimeException x) {
109             throw new IllegalArgumentException(x);
110         }
111     
112     }
113     
114 // -- Comparators : ------------------------------------------------------------
115     
116     /** Returns comparator that compares time windows by names. */
117     static public Comparator<TimeSelection> compareByName() {
118         return new Comparator<TimeSelection>() {
119             public int compare(TimeSelection o1, TimeSelection o2) {
120                 return o1.name.compareTo(o2.name);
121             }
122         };
123     }
124     
125     /** Returns comparator that compares time windows by last use times, most recent first. */
126     static public Comparator compareByTime() {
127         return new Comparator() {
128             public int compare(Object o1, Object o2) {
129                 return (int) Math.signum(((TimeSelection)o2).getLastUseTime() - ((TimeSelection)o1).getLastUseTime());
130             }
131         };
132     }
133     
134     
135 }