]> git.mxchange.org Git - jcore-utils.git/blobdiff - src/org/mxchange/jcoreutils/comparable/ComparableUtils.java
Continued:
[jcore-utils.git] / src / org / mxchange / jcoreutils / comparable / ComparableUtils.java
diff --git a/src/org/mxchange/jcoreutils/comparable/ComparableUtils.java b/src/org/mxchange/jcoreutils/comparable/ComparableUtils.java
new file mode 100644 (file)
index 0000000..bfc6063
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2022 Free Software Foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcoreutils.comparable;
+
+import java.io.Serializable;
+
+/**
+ * An utilities class for comparison of objects/entities
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class ComparableUtils implements Serializable {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 19_286_457_671_390L;
+
+       /**
+        * Checks all comparison values if they are not zero and returns proper
+        * value. These comparators should come from an implementation of
+        * Comparable.compareTo().
+        * <p>
+        * @param comparators An array of at least one comparator
+        * <p>
+        * @return Comparison value
+        */
+       public static int checkAll (final int[] comparators) {
+               // Is array empty?
+               if (comparators.length == 0) {
+                       // Should not happen
+                       throw new IllegalArgumentException("comparators is empty."); //NOI18N
+               }
+
+               // Loop through all
+               for (int i = 0; i < comparators.length; i++) {
+                       // Is it smaller or bigger?
+                       if (comparators[i] < 0) {
+                               return -1;
+                       } else if (comparators[i] > 0) {
+                               return 1;
+                       }
+               }
+
+               // Assume equality
+               return 0;
+       }
+
+       /**
+        * Utility classes should not have instances
+        */
+       private ComparableUtils () {
+       }
+
+}