-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDatabase.java
More file actions
90 lines (79 loc) · 2.14 KB
/
Database.java
File metadata and controls
90 lines (79 loc) · 2.14 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
83
84
85
86
87
88
89
90
package com.farrellf.TelemetryGUI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
/**
* A simple place for data storage and retrieval.
* A Map of ArrayLists is currently used as the storage medium.
*
* @author Farrell Farahbod
* @version 1.0
*/
public class Database {
// names of items being tracked by the database
private List<String> names;
private Map<String, List<Integer>> db;
public Database() {
names = new ArrayList<String>();
db = new HashMap<String, List<Integer>>();
}
/** Insert a new value for the specified item
*
* @param key Name of tracked item
* @param value New value
*/
public void addValue(String key, int value) {
if(!names.contains(key)) {
// add new list to the map if it doesn't already exist
db.put(key, Collections.synchronizedList(new ArrayList<Integer>()));
names.add(key);
}
db.get(key).add(value);
}
/** Get the most recent value from the database
*
* @param key Name of tracked item
* @return Most recent value
*/
public int getLastValue(String key) {
if(!names.contains(key)) {
//System.err.println("Item \"" + key + "\" does not yet exist in the database.");
return -1;
}
List<Integer> al = db.get(key);
int lastIndex = al.size() - 1;
if(lastIndex == -1) {
return -1; // no values exist in the database
} else {
return al.get(lastIndex);
}
}
/** Get the sample count for an item
*
* @param key Name of tracked item
* @return Count of values stored in the db
*/
public int getListSize(String key) {
if(!names.contains(key)) {
//System.err.println("Item \"" + key + "\" does not yet exist in the database.");
return -1;
} else {
return db.get(key).size();
}
}
/** Get a list containing the history of values for an item
*
* @param key Name of tracked item
* @return List<Integer> of values
*/
public List<Integer> getList(String key) {
if(!names.contains(key)) {
//System.err.println("Item \"" + key + "\" does not yet exist in the database.");
return new ArrayList<Integer>();
} else {
return db.get(key);
}
}
}