]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/utils/Comparables.java
Continued:
[jcore-utils.git] / src / org / mxchange / jcoreee / utils / Comparables.java
1 /*
2  * Copyright (C) 2018 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcoreee.utils;
18
19 import java.io.Serializable;
20
21 /**
22  * An utilities class for comparison of objects/entities
23  * <p>
24  * @author Roland Häder<roland@mxchange.org>
25  */
26 public class Comparables implements Serializable {
27
28         /**
29          * Serial number
30          */
31         private static final long serialVersionUID = 19_286_457_671_390L;
32
33         /**
34          * Checks all comparison values if they are not zero and returns proper
35          * value. These comparators should come from an implementation of
36          * Comparable.compareTo().
37          * <p>
38          * @param comparators An array of at least one comparator
39          * <p>
40          * @return Comparison value
41          */
42         public static int checkAll (final int[] comparators) {
43                 // Is array empty?
44                 if (comparators.length == 0) {
45                         // Should not happen
46                         throw new IllegalArgumentException("comparators is empty."); //NOI18N
47                 }
48
49                 // Loop through all
50                 for (int i = 0; i < comparators.length; i++) {
51                         // Is it smaller or bigger?
52                         if (comparators[i] < 0) {
53                                 return -1;
54                         } else if (comparators[i] > 0) {
55                                 return 1;
56                         }
57                 }
58
59                 // Assume euqality
60                 return 0;
61         }
62
63         /**
64          * Utility classes should not have instances
65          */
66         private Comparables () {
67         }
68
69 }