-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatapoint.java
More file actions
114 lines (107 loc) · 3.97 KB
/
Copy pathDatapoint.java
File metadata and controls
114 lines (107 loc) · 3.97 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Represents a general datapoint consisting of a name, numerical columns, and their corresponding column names.
* Implements Comparable to allow ordering based on a specified column.
* @author Showmick Das
* @version 1.0
*/
public class Datapoint implements Comparable<Datapoint> {
protected String name;
protected double[] columns;
public String[] columnNames;
protected int orderColumn;
protected boolean reverseOrder;
/**
* Constructs a Datapoint with the given name, columns, and column names.
* Any column value outside the range [0, 5] is set to 2.5.
* @param name The name of the datapoint
* @param columns Array of numerical values
* @param columnNames Array of corresponding column names
*/
public Datapoint(String name, double[] columns, String[] columnNames) {
this.name = name;
this.columns = new double[columns.length];
for (int i = 0; i < columns.length; i++) {
if (columns[i] < 0 || columns[i] > 5) {
this.columns[i] = 2.5;
} else {
this.columns[i] = columns[i];
}
}
this.columnNames = new String[columnNames.length];
for (int i = 0; i < columnNames.length; i++) {
this.columnNames[i] = columnNames[i];
}
// Default ordering settings
this.orderColumn = 0;
this.reverseOrder = false;
}
/**
* Copy constructor. Creates a deep copy of the given Datapoint.
* @param other The Datapoint to copy from
*/
public Datapoint(Datapoint other) {
this.name = other.name;
this.columns = new double[other.columns.length];
for (int i = 0; i < other.columns.length; i++) {
this.columns[i] = other.columns[i];
}
this.columnNames = new String[other.columnNames.length];
for (int i = 0; i < other.columnNames.length; i++) {
this.columnNames[i] = other.columnNames[i];
}
this.orderColumn = other.orderColumn;
this.reverseOrder = other.reverseOrder;
}
/**
* Sets the order column used for comparisons.
* @param column The index of the column to use for ordering
*/
public void setOrderColumn(int column) {
if (column >= 0 && column < columnNames.length) {
this.orderColumn = column;
}
}
/**
* Sets whether the order should be reversed.
* @param reverse True for reverse order, false otherwise
*/
public void setReverseOrder(boolean reverse) {
this.reverseOrder = reverse;
}
/**
* Returns the name of the datapoint.
* @return The name of the datapoint
*/
public String getName() {
return name;
}
/**
* Compares this Datapoint with the specified Datapoint for order based on the orderColumn.
* The comparison is based on the value at orderColumn in the columns array.
* If reverseOrder is true, the comparison result is flipped.
* @param o The Datapoint to be compared
* @return a negative integer, zero, or a positive integer as the parameter is considered
* greater than, equal to, or less than this Datapoint.
*/
@Override
public int compareTo(Datapoint o) {
double thisValue = this.columns[this.orderColumn];
double otherValue = o.columns[this.orderColumn];
int cmp = Double.compare(thisValue, otherValue);
return reverseOrder ? -cmp : cmp;
}
/**
* Returns a string representation of the Datapoint.
* The format is: {name}| {column0 value}| {column1 value}| ... with each field padded to at least 12 characters.
* Floating point values are formatted to 2 decimal places.
* @return A formatted string representation of the datapoint.
*/
@Override
public String toString() {
String result = String.format("%12s", name);
for (double value : columns) {
result += "|" + String.format("%12.2f", value);
}
return result;
}
}