Skip to content

Commit 5e7396b

Browse files
committed
Fixed the single line if statements.
1 parent 5a10f94 commit 5e7396b

4 files changed

Lines changed: 33 additions & 11 deletions

File tree

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Centroid.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ public Map<String, Double> getCoordinates() {
2323

2424
@Override
2525
public boolean equals(Object o) {
26-
if (this == o) return true;
27-
if (o == null || getClass() != o.getClass()) return false;
26+
if (this == o) {
27+
return true;
28+
}
29+
if (o == null || getClass() != o.getClass()) {
30+
return false;
31+
}
2832
Centroid centroid = (Centroid) o;
2933
return Objects.equals(getCoordinates(), centroid.getCoordinates());
3034
}

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/EuclideanDistance.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public class EuclideanDistance implements Distance {
99

1010
@Override
1111
public double calculate(Map<String, Double> f1, Map<String, Double> f2) {
12-
if (f1 == null || f2 == null) throw new IllegalArgumentException("Feature vectors can't be null");
12+
if (f1 == null || f2 == null) {
13+
throw new IllegalArgumentException("Feature vectors can't be null");
14+
}
1315

1416
double sum = 0;
1517
for (String key : f1.keySet()) {

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/KMeans.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public static Map<Centroid, List<Record>> fit(List<Record> records, int k, Dista
5555
// if the assignment does not change, then the algorithm terminates
5656
boolean shouldTerminate = isLastIteration || clusters.equals(lastState);
5757
lastState = clusters;
58-
if (shouldTerminate) break;
58+
if (shouldTerminate) {
59+
break;
60+
}
5961

6062
// at the end of each iteration we should relocate the centroids
6163
centroids = relocateCentroids(clusters);
@@ -92,7 +94,9 @@ private static List<Centroid> relocateCentroids(Map<Centroid, List<Record>> clus
9294
*/
9395
private static Centroid average(Centroid centroid, List<Record> records) {
9496
// if this cluster is empty, then we shouldn't move the centroid
95-
if (records == null || records.isEmpty()) return centroid;
97+
if (records == null || records.isEmpty()) {
98+
return centroid;
99+
}
96100

97101
// Since some records don't have all possible attributes, we initialize
98102
// average coordinates equal to current centroid coordinates
@@ -213,12 +217,20 @@ private static List<Centroid> randomCentroids(List<Record> records, int k) {
213217
}
214218

215219
private static void applyPreconditions(List<Record> records, int k, Distance distance, int maxIterations) {
216-
if (records == null || records.isEmpty()) throw new IllegalArgumentException("The dataset can't be empty");
220+
if (records == null || records.isEmpty()) {
221+
throw new IllegalArgumentException("The dataset can't be empty");
222+
}
217223

218-
if (k <= 1) throw new IllegalArgumentException("It doesn't make sense to have less than or equal to 1 cluster");
224+
if (k <= 1) {
225+
throw new IllegalArgumentException("It doesn't make sense to have less than or equal to 1 cluster");
226+
}
219227

220-
if (distance == null) throw new IllegalArgumentException("The distance calculator is required");
228+
if (distance == null) {
229+
throw new IllegalArgumentException("The distance calculator is required");
230+
}
221231

222-
if (maxIterations <= 0) throw new IllegalArgumentException("Max iterations should be a positive number");
232+
if (maxIterations <= 0) {
233+
throw new IllegalArgumentException("Max iterations should be a positive number");
234+
}
223235
}
224236
}

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Record.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ public String toString() {
4848

4949
@Override
5050
public boolean equals(Object o) {
51-
if (this == o) return true;
52-
if (o == null || getClass() != o.getClass()) return false;
51+
if (this == o) {
52+
return true;
53+
}
54+
if (o == null || getClass() != o.getClass()) {
55+
return false;
56+
}
5357
Record record = (Record) o;
5458
return Objects.equals(getDescription(), record.getDescription()) && Objects.equals(getFeatures(), record.getFeatures());
5559
}

0 commit comments

Comments
 (0)