Repository navigation
Expand file tree
/
Copy pathHHisItem.java
More file actions
82 lines (71 loc) · 2.03 KB
/
Copy pathHHisItem.java
File metadata and controls
82 lines (71 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Copyright (c) 2011, Brian Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 04 Nov 2011 Brian Frank Creation
//
package org.projecthaystack;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
/**
* HHisItem is used to model a timestamp/value pair
*
* @see <a href='http://project-haystack.org/doc/Ops#hisRead'>Project Haystack</a>
*/
public class HHisItem extends HDict
{
/** Map HGrid to HHisItem[]. Grid must have ts and val columns. */
public static HHisItem[] gridToItems(HGrid grid)
{
HCol ts = grid.col("ts");
HCol val = grid.col("val");
HHisItem[] items = new HHisItem[grid.numRows()];
for (int i=0; i<items.length; ++i)
{
HRow row = grid.row(i);
items[i] = new HHisItem((HDateTime)row.get(ts, true), row.get(val, false));
}
return items;
}
/** Construct from timestamp, value */
public static HHisItem make(HDateTime ts, HVal val)
{
if (ts == null || val == null) throw new IllegalArgumentException("ts or val is null");
return new HHisItem(ts, val);
}
/** Private constructor */
private HHisItem(HDateTime ts, HVal val)
{
this.ts = ts;
this.val = val;
}
/** Timestamp of history sample */
public final HDateTime ts;
/** Value of history sample */
public final HVal val;
public int size() { return 2; }
public HVal get(String name, boolean checked)
{
if (name.equals("ts")) return ts;
if (name.equals("val")) return val;
if (!checked) return null;
throw new UnknownNameException(name);
}
public Iterator iterator() { return new FixedIterator(); }
class FixedIterator implements Iterator
{
public boolean hasNext() { return cur < 1; }
public Object next()
{
++cur;
if (cur == 0) return new MapEntry("ts", ts);
if (cur == 1) return new MapEntry("val", val);
throw new NoSuchElementException();
}
public void remove() { throw new UnsupportedOperationException(); }
int cur = -1;
}
}