Fixed the single line if statements.

This commit is contained in:
Ali Dehghani 2019-08-07 20:53:54 +04:30
parent 5a10f94946
commit 5e7396ba64
4 changed files with 33 additions and 11 deletions

View File

@ -23,8 +23,12 @@ public class Centroid {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Centroid centroid = (Centroid) o;
return Objects.equals(getCoordinates(), centroid.getCoordinates());
}

View File

@ -9,7 +9,9 @@ public class EuclideanDistance implements Distance {
@Override
public double calculate(Map<String, Double> f1, Map<String, Double> f2) {
if (f1 == null || f2 == null) throw new IllegalArgumentException("Feature vectors can't be null");
if (f1 == null || f2 == null) {
throw new IllegalArgumentException("Feature vectors can't be null");
}
double sum = 0;
for (String key : f1.keySet()) {

View File

@ -55,7 +55,9 @@ public class KMeans {
// if the assignment does not change, then the algorithm terminates
boolean shouldTerminate = isLastIteration || clusters.equals(lastState);
lastState = clusters;
if (shouldTerminate) break;
if (shouldTerminate) {
break;
}
// at the end of each iteration we should relocate the centroids
centroids = relocateCentroids(clusters);
@ -92,7 +94,9 @@ public class KMeans {
*/
private static Centroid average(Centroid centroid, List<Record> records) {
// if this cluster is empty, then we shouldn't move the centroid
if (records == null || records.isEmpty()) return centroid;
if (records == null || records.isEmpty()) {
return centroid;
}
// Since some records don't have all possible attributes, we initialize
// average coordinates equal to current centroid coordinates
@ -213,12 +217,20 @@ public class KMeans {
}
private static void applyPreconditions(List<Record> records, int k, Distance distance, int maxIterations) {
if (records == null || records.isEmpty()) throw new IllegalArgumentException("The dataset can't be empty");
if (records == null || records.isEmpty()) {
throw new IllegalArgumentException("The dataset can't be empty");
}
if (k <= 1) throw new IllegalArgumentException("It doesn't make sense to have less than or equal to 1 cluster");
if (k <= 1) {
throw new IllegalArgumentException("It doesn't make sense to have less than or equal to 1 cluster");
}
if (distance == null) throw new IllegalArgumentException("The distance calculator is required");
if (distance == null) {
throw new IllegalArgumentException("The distance calculator is required");
}
if (maxIterations <= 0) throw new IllegalArgumentException("Max iterations should be a positive number");
if (maxIterations <= 0) {
throw new IllegalArgumentException("Max iterations should be a positive number");
}
}
}

View File

@ -48,8 +48,12 @@ public class Record {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Record record = (Record) o;
return Objects.equals(getDescription(), record.getDescription()) && Objects.equals(getFeatures(), record.getFeatures());
}