@@ -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}
0 commit comments