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
10
11
12
13 public class TimeSelection {
14
15
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
23 public String name;
24
25 public String lowerEdgeString;
26
27 public String upperEdgeString;
28
29
30
31 private boolean persist;
32 private long lastUsed;
33
34
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
45
46
47 public long getLowerEdge() {
48 return getEdge(lowerEdgeString);
49 }
50
51
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
70 public boolean isPersistent() {
71 return persist;
72 }
73
74
75 public void setPersistent(boolean persistent) {
76 persist = persistent;
77 }
78
79
80 public long getLastUseTime() {
81 return lastUsed;
82 }
83
84
85 public void touch() {
86 lastUsed = System.currentTimeMillis();
87 }
88
89
90 @Override
91 public String toString() {
92 return name +" : "+ lowerEdgeString +" through "+ upperEdgeString;
93 }
94
95
96 public String toCompressedString() {
97 return name + DELIMITER + lowerEdgeString + DELIMITER + upperEdgeString;
98 }
99
100
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
115
116
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
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 }