/
DiscordRecords.java
103 lines (92 loc) · 2.63 KB
/
DiscordRecords.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
package net.seninp.jmotif.sax.discord;
import java.util.Iterator;
import java.util.List;
import net.seninp.util.SortedArrayList;
/**
* The discord records collection.
*
* @author Pavel Senin
*
*/
public class DiscordRecords implements Iterable<DiscordRecord> {
/** Storage container. */
private final SortedArrayList<DiscordRecord> discords;
/**
* Constructor.
*/
public DiscordRecords() {
this.discords = new SortedArrayList<DiscordRecord>();
}
/**
* Add a new discord to the list.
*
* @param discord The discord instance to add.
*/
public void add(DiscordRecord discord) {
this.discords.insertSorted(discord);
}
/**
* Returns the number of the top hits.
*
* @param num The number of instances to return. If the number larger than the storage size -
* returns the storage as is.
* @return the top discord hits.
*/
public List<DiscordRecord> getTopHits(Integer num) {
if (num >= this.discords.size()) {
return this.discords;
}
List<DiscordRecord> res = this.discords.subList(this.discords.size() - num,
this.discords.size());
return res;
}
/**
* Get the minimal distance found among all instances in the collection.
*
* @return The minimal distance found among all instances in the collection.
*/
public double getMinDistance() {
if (this.discords.size() > 0) {
return discords.get(0).getNNDistance();
}
return -1D;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer(128);
for (int i = 0; i < discords.size(); i++) {
DiscordRecord record = discords.get(i);
if (record.getPayload().isEmpty()) {
sb.append("discord #" + i + " \"\", at " + record.getPosition()
+ " distance to closest neighbor: " + record.getNNDistance() + "\"\n");
}
else {
sb.append("discord #" + i + " \"" + record.getPayload() + "\", at " + record.getPosition()
+ " distance to closest neighbor: " + record.getNNDistance() + ", info string: \""
+ record.getInfo() + "\"\n");
}
}
return sb.toString();
}
@Override
public Iterator<DiscordRecord> iterator() {
return this.discords.iterator();
}
/**
* Get the collection size.
*
* @return the discords collection size.
*/
public int getSize() {
return this.discords.size();
}
/**
* Get a discord record by its index.
*
* @param i the index.
* @return the discord record.
*/
public DiscordRecord get(int i) {
return this.discords.get(i);
}
}