From 5e7396ba64d1f1ac9c964aad6abc2e1b276cb499 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Wed, 7 Aug 2019 20:53:54 +0430 Subject: [PATCH] Fixed the single line if statements. --- .../baeldung/algorithms/kmeans/Centroid.java | 8 +++++-- .../algorithms/kmeans/EuclideanDistance.java | 4 +++- .../baeldung/algorithms/kmeans/KMeans.java | 24 ++++++++++++++----- .../baeldung/algorithms/kmeans/Record.java | 8 +++++-- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Centroid.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Centroid.java index 462936541b..523d5b56a5 100644 --- a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Centroid.java +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Centroid.java @@ -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()); } diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/EuclideanDistance.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/EuclideanDistance.java index faccd97599..193d9afed1 100644 --- a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/EuclideanDistance.java +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/EuclideanDistance.java @@ -9,7 +9,9 @@ public class EuclideanDistance implements Distance { @Override public double calculate(Map f1, Map 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()) { diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/KMeans.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/KMeans.java index e1152a67d6..1fb8541ff9 100644 --- a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/KMeans.java +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/KMeans.java @@ -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 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 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"); + } } } diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Record.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Record.java index a29e3e054b..d2a7b61c62 100644 --- a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Record.java +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Record.java @@ -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()); }