Skip to content

Commit 6b8fd2f

Browse files
authored
Merge pull request #29 from AndyMacDroo/master
A few minor code quality improvements
2 parents 59eec48 + 844d9bb commit 6b8fd2f

File tree

10 files changed

+43
-39
lines changed

10 files changed

+43
-39
lines changed

src/main/java/de/dmi3y/behaiv/kernel/Kernel.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.FileWriter;
1212
import java.io.IOException;
1313
import java.util.ArrayList;
14+
import java.util.List;
1415

1516
public abstract class Kernel {
1617

@@ -32,12 +33,12 @@ public void setId(String id) {
3233

3334

3435
//list<features>, label
35-
protected ArrayList<Pair<ArrayList<Double>, String>> data = new ArrayList<>();
36+
protected List<Pair<List<Double>, String>> data = new ArrayList<>();
3637

3738

3839
public abstract boolean isEmpty();
3940

40-
public abstract void fit(ArrayList<Pair<ArrayList<Double>, String>> data);
41+
public abstract void fit(List<Pair<List<Double>, String>> data);
4142

4243
public void fit() {
4344
fit(this.data);
@@ -51,18 +52,18 @@ public boolean readyToPredict() {
5152
return data.size() > treshold;
5253
}
5354

54-
public void update(ArrayList<Pair<ArrayList<Double>, String>> data) {
55+
public void update(List<Pair<List<Double>, String>> data) {
5556
}
5657

5758
public boolean isPartialFitAllowed() {
5859
return partialFitAllowed;
5960
}
6061

61-
public void updateSingle(ArrayList<Double> features, String label) {
62+
public void updateSingle(List<Double> features, String label) {
6263
data.add(new Pair<>(features, label));
6364
}
6465

65-
public abstract String predictOne(ArrayList<Double> features);
66+
public abstract String predictOne(List<Double> features);
6667

6768
public boolean isAlwaysKeepData() {
6869
return alwaysKeepData;
@@ -80,7 +81,7 @@ public void save(BehaivStorage storage) throws IOException {
8081
}
8182

8283
public void restore(BehaivStorage storage) throws IOException {
83-
final TypeReference<ArrayList<Pair<ArrayList<Double>, String>>> typeReference = new TypeReference<ArrayList<Pair<ArrayList<Double>, String>>>() {
84+
final TypeReference<List<Pair<List<Double>, String>>> typeReference = new TypeReference<List<Pair<List<Double>, String>>>() {
8485
};
8586
try (final BufferedReader reader = new BufferedReader(new FileReader(storage.getDataFile(id)))) {
8687
final String content = reader.readLine();

src/main/java/de/dmi3y/behaiv/kernel/LogisticRegressionKernel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public boolean isEmpty() {
4040

4141

4242
@Override
43-
public void fit(ArrayList<Pair<ArrayList<Double>, String>> data) {
43+
public void fit(List<Pair<List<Double>, String>> data) {
4444
this.data = data;
4545
labels = toDistinctListOfPairValues(data);
4646
if (readyToPredict()) {
@@ -86,12 +86,12 @@ public boolean readyToPredict() {
8686
}
8787

8888
@Override
89-
public void updateSingle(ArrayList<Double> features, String label) {
89+
public void updateSingle(List<Double> features, String label) {
9090
super.updateSingle(features, label);
9191
}
9292

9393
@Override
94-
public String predictOne(ArrayList<Double> features) {
94+
public String predictOne(List<Double> features) {
9595

9696

9797
final double[] doubles = ArrayUtils.toPrimitive(features.toArray(new Double[0]));
@@ -153,7 +153,7 @@ public void restore(BehaivStorage storage) throws IOException {
153153
}
154154

155155
try (final BufferedReader reader = new BufferedReader(new FileReader(storage.getNetworkMetadataFile(id)))) {
156-
final TypeReference<ArrayList<String>> typeReference = new TypeReference<ArrayList<String>>() {
156+
final TypeReference<List<String>> typeReference = new TypeReference<List<String>>() {
157157
};
158158
final String labelsData = reader.readLine();
159159
if (labelsData == null) {

src/main/java/de/dmi3y/behaiv/provider/Provider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
*/
1111
public interface Provider {
1212

13-
public List<String> availableFeatures();
13+
List<String> availableFeatures();
1414

15-
public Observable<List<Double>> subscribeFeatures();
15+
Observable<List<Double>> subscribeFeatures();
1616

17-
public Single<List<Double>> getFeature();
17+
Single<List<Double>> getFeature();
1818

1919
}

src/main/java/de/dmi3y/behaiv/provider/ProviderCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
public interface ProviderCallback {
88

9-
public void onFeaturesCaptured(List<Pair<Double, String>> features);
9+
void onFeaturesCaptured(List<Pair<Double, String>> features);
1010
}

src/main/java/de/dmi3y/behaiv/storage/BehaivStorage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
public interface BehaivStorage {
77

8-
public boolean containsNetworkFile(String id);
8+
boolean containsNetworkFile(String id);
99

10-
public File getDataFile(String id) throws IOException;
10+
File getDataFile(String id) throws IOException;
1111

12-
public File getNetworkFile(String id) throws IOException;
12+
File getNetworkFile(String id) throws IOException;
1313

14-
public File getNetworkMetadataFile(String id) throws IOException;
14+
File getNetworkMetadataFile(String id) throws IOException;
1515

1616
}

src/main/java/de/dmi3y/behaiv/tools/DataMappingUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ private DataMappingUtils() {
1515
}
1616

1717

18-
public static List<String> toDistinctListOfPairValues(List<Pair<ArrayList<Double>, String>> data) {
18+
public static List<String> toDistinctListOfPairValues(List<Pair<List<Double>, String>> data) {
1919
Set<String> setOfValues = new HashSet<>();
20-
for(Pair<ArrayList<Double>, String> arrayListStringPair : data ) {
20+
for(Pair<List<Double>, String> arrayListStringPair : data ) {
2121
setOfValues.add(arrayListStringPair.getValue());
2222
}
2323
return new ArrayList<>(setOfValues);
@@ -31,10 +31,10 @@ public static ArrayList<Double> toListOfPairKeys(List<Pair<Double, String>> feat
3131
return doubleArrayList;
3232
}
3333

34-
public static double[][] toInput2dArray(List<Pair<ArrayList<Double>, String>> data) {
34+
public static double[][] toInput2dArray(List<Pair<List<Double>, String>> data) {
3535
double[][] input2dArray = new double[data.size()][];
3636
int i = 0;
37-
for(Pair<ArrayList<Double>, String> dataPair : data) {
37+
for(Pair<List<Double>, String> dataPair : data) {
3838
double[] doubleArray = ArrayUtils.toPrimitive(dataPair.getKey().toArray(new Double[0]));
3939
input2dArray[i] = doubleArray;
4040
i++;

src/test/java/de/dmi3y/behaiv/LibraryTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.junit.Test;
1212

1313
import java.util.ArrayList;
14+
import java.util.List;
1415

1516
import static de.dmi3y.behaiv.kernel.KernelTest.WORK;
1617
import static org.junit.Assert.assertEquals;
@@ -19,7 +20,7 @@ public class LibraryTest {
1920
Behaiv behaiv;
2021
private TestSleepProvider positionProvider;
2122
private TestSleepProvider timeProvider;
22-
private ArrayList<Pair<ArrayList<Double>, String>> data;
23+
private List<Pair<List<Double>, String>> data;
2324

2425
public static final String WORK_SCREEN = "WORK_SCREEN";
2526

@@ -35,8 +36,8 @@ public void setUp() throws Exception {
3536

3637
@Test
3738
public void behaivTest_basicTestFlow_predictsJob() throws Exception {
38-
for (Pair<ArrayList<Double>, String> fToL : data) {
39-
ArrayList<Double> features = fToL.getKey();
39+
for (Pair<List<Double>, String> fToL : data) {
40+
List<Double> features = fToL.getKey();
4041
timeProvider.next(new Double[]{features.get(0)});
4142
positionProvider.next(new Double[]{features.get(1), features.get(2)});
4243
capture(fToL.getValue());

src/test/java/de/dmi3y/behaiv/kernel/KernelTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.File;
1111
import java.io.IOException;
1212
import java.util.ArrayList;
13+
import java.util.List;
1314

1415
import static org.junit.Assert.assertFalse;
1516
import static org.junit.Assert.assertTrue;
@@ -39,8 +40,8 @@ public void setUp() throws Exception {
3940
@Test
4041
public void setTreshold() {
4142
dummyKernel.setTreshold(1L);
42-
dummyKernel.data.add(new Pair<ArrayList<Double>, String>(null, null));
43-
dummyKernel.data.add(new Pair<ArrayList<Double>, String>(null, null));
43+
dummyKernel.data.add(new Pair<List<Double>, String>(null, null));
44+
dummyKernel.data.add(new Pair<List<Double>, String>(null, null));
4445
boolean readyToPredict = dummyKernel.readyToPredict();
4546
assertTrue(readyToPredict);
4647
dummyKernel.setTreshold(10L);
@@ -66,9 +67,9 @@ public void kernel_saveAndRestore_expectNormalFlow() throws IOException {
6667

6768
}
6869

69-
public static ArrayList<Pair<ArrayList<Double>, String>> getTrainingData() {
70-
ArrayList<Double> list = new ArrayList<>();
71-
ArrayList<Pair<ArrayList<Double>, String>> data = new ArrayList<>();
70+
public static List<Pair<List<Double>, String>> getTrainingData() {
71+
List<Double> list = new ArrayList<>();
72+
List<Pair<List<Double>, String>> data = new ArrayList<>();
7273

7374

7475
list.add((5 * 60 + 00.0) / (24 * 60));

src/test/java/de/dmi3y/behaiv/kernel/LogisticRegressionKernelTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.IOException;
1111
import java.util.ArrayList;
1212
import java.util.Arrays;
13+
import java.util.List;
1314
import java.util.Random;
1415

1516
import static de.dmi3y.behaiv.kernel.KernelTest.HOME;
@@ -34,7 +35,7 @@ public void setUp() throws Exception {
3435

3536
@Test
3637
public void predictOne() {
37-
ArrayList<Pair<ArrayList<Double>, String>> data = KernelTest.getTrainingData();
38+
List<Pair<List<Double>, String>> data = KernelTest.getTrainingData();
3839
Kernel testKernel = new LogisticRegressionKernel("testId");
3940
testKernel.fit(data);
4041
ArrayList<Double> predictList = new ArrayList<>();
@@ -58,7 +59,7 @@ public void predictOne() {
5859

5960
@Test
6061
public void storeResults() throws IOException, ClassNotFoundException {
61-
ArrayList<Pair<ArrayList<Double>, String>> data = KernelTest.getTrainingData();
62+
List<Pair<List<Double>, String>> data = KernelTest.getTrainingData();
6263
Kernel kernel = new LogisticRegressionKernel("testId");
6364
kernel.setId("storeTest");
6465
kernel.fit(data);
@@ -84,7 +85,7 @@ public void storeResults() throws IOException, ClassNotFoundException {
8485

8586
@Test
8687
public void storeResults_addAdditionalLabel_shouldFail() throws IOException, ClassNotFoundException {
87-
ArrayList<Pair<ArrayList<Double>, String>> data = KernelTest.getTrainingData();
88+
List<Pair<List<Double>, String>> data = KernelTest.getTrainingData();
8889
Kernel kernel = new LogisticRegressionKernel("testId");
8990
kernel.setAlwaysKeepData(false);
9091
kernel.setId("storeTest");
@@ -142,7 +143,7 @@ public void storeResults_saveWhenDataIsNull_expectException() throws IOException
142143

143144
@Test
144145
public void storeResults_saveDataAndThenTheta_expectNormalFlow() throws IOException, ClassNotFoundException {
145-
ArrayList<Pair<ArrayList<Double>, String>> data = KernelTest.getTrainingData();
146+
List<Pair<List<Double>, String>> data = KernelTest.getTrainingData();
146147
LogisticRegressionKernel kernel = new LogisticRegressionKernel("storeTest", new Random());
147148
kernel.data = data;
148149
kernel.labels = Arrays.asList("time", "lat", "lon", "headphones");

src/test/java/de/dmi3y/behaiv/tools/DataMappingUtilsTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class DataMappingUtilsTest {
1515

1616
@Test
1717
public void toDistinctListOfPairValues_withInputData_returnsExpectedDistinctListOfPairValues() {
18-
List<Pair<ArrayList<Double>, String>> mockData = Arrays.asList(
18+
List<Pair<List<Double>, String>> mockData = Arrays.asList(
1919
getMockPairListDoubleStringData(), getMockPairListDoubleStringData()
2020
);
2121
List<String> actual = DataMappingUtils.toDistinctListOfPairValues(mockData);
@@ -25,7 +25,7 @@ public void toDistinctListOfPairValues_withInputData_returnsExpectedDistinctList
2525

2626
@Test
2727
public void toDistinctListOfPairValues_withEmptyListOfInputData_returnsEmptyList() {
28-
List<String> actual = DataMappingUtils.toDistinctListOfPairValues(Collections.<Pair<ArrayList<Double>, String>>emptyList());
28+
List<String> actual = DataMappingUtils.toDistinctListOfPairValues(Collections.<Pair<List<Double>, String>>emptyList());
2929
assertThat(actual, notNullValue());
3030
assertTrue(actual.isEmpty());
3131
}
@@ -67,7 +67,7 @@ public void toInput2dArray_withInputData_returnsExpected2dArray() {
6767

6868
@Test
6969
public void toInput2dArray_withEmptyListOfInputData_returnsEmpty2dArray() {
70-
double[][] actual = DataMappingUtils.toInput2dArray(Collections.<Pair<ArrayList<Double>, String>>emptyList());
70+
double[][] actual = DataMappingUtils.toInput2dArray(Collections.<Pair<List<Double>, String>>emptyList());
7171
assertThat(actual.length, is(0));
7272
}
7373

@@ -81,8 +81,8 @@ private Pair<Double, String> getMockPairDoubleStringData() {
8181
return new Pair<>(20d, "Fish");
8282
}
8383

84-
private Pair<ArrayList<Double>, String> getMockPairListDoubleStringData() {
85-
ArrayList<Double> myDoubleList = new ArrayList<>();
84+
private Pair<List<Double>, String> getMockPairListDoubleStringData() {
85+
List<Double> myDoubleList = new ArrayList<>();
8686
myDoubleList.add(20d);
8787
return new Pair<>(myDoubleList, "Potato");
8888
}

0 commit comments

Comments
 (0)