| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| EqualsUtils |
|
| 5.4;5.4 |
| 1 | package org.jtheque.utils.bean; | |
| 2 | ||
| 3 | import java.io.File; | |
| 4 | ||
| 5 | /* | |
| 6 | * This file is part of JTheque. | |
| 7 | * | |
| 8 | * JTheque is free software: you can redistribute it and/or modify | |
| 9 | * it under the terms of the GNU General Public License as published by | |
| 10 | * the Free Software Foundation, either version 3 of the License. | |
| 11 | * | |
| 12 | * JTheque is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU General Public License | |
| 18 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
| 19 | */ | |
| 20 | ||
| 21 | /** | |
| 22 | * An utility class for equality test. | |
| 23 | * | |
| 24 | * @author Baptiste Wicht | |
| 25 | */ | |
| 26 | public final class EqualsUtils { | |
| 27 | /** | |
| 28 | * Construct a new DatabaseUtils. This class is an utility class, it cannot be instantiated. | |
| 29 | */ | |
| 30 | private EqualsUtils() { | |
| 31 | 0 | super(); |
| 32 | 0 | } |
| 33 | ||
| 34 | /** | |
| 35 | * Test if 2 objects are incompatible for equality test. | |
| 36 | * | |
| 37 | * @param object The first object to test. | |
| 38 | * @param other The second object to test. | |
| 39 | * @return true if the objects are incompatibles else false. | |
| 40 | */ | |
| 41 | public static boolean areObjectIncompatible(Object object, Object other) { | |
| 42 | 70 | if (other == null) { |
| 43 | 0 | return true; |
| 44 | } | |
| 45 | ||
| 46 | 70 | return object.getClass() != other.getClass(); |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Test if 2 object are not equals. | |
| 51 | * | |
| 52 | * @param object The first object to test. | |
| 53 | * @param other The second object to test. | |
| 54 | * @return true if the objects aren't equals else false. | |
| 55 | */ | |
| 56 | public static boolean areNotEquals(Object object, Object other) { | |
| 57 | 76 | if (object == null) { |
| 58 | 6 | if (other != null) { |
| 59 | 2 | return true; |
| 60 | } | |
| 61 | 70 | } else if (!object.equals(other)) { |
| 62 | 56 | return true; |
| 63 | } | |
| 64 | ||
| 65 | 18 | return false; |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Test if the two objects are equals. | |
| 70 | * | |
| 71 | * @param bean The bean to test. | |
| 72 | * @param other The other bean to test for equality with the first one. | |
| 73 | * @param properties The properties to compare one by one. The properties n is compared to the property | |
| 74 | * n + (properties.length / 2). This array must be pair. | |
| 75 | * | |
| 76 | * @return A boolean indicating if the two objects are equals or not. | |
| 77 | */ | |
| 78 | public static boolean areEqualsDirect(Object bean, Object other, Object... properties){ | |
| 79 | 0 | if (bean == other){ |
| 80 | 0 | return true; |
| 81 | } | |
| 82 | ||
| 83 | 0 | if (EqualsUtils.areObjectIncompatible(bean, other)){ |
| 84 | 0 | return false; |
| 85 | } | |
| 86 | ||
| 87 | 0 | int numberOfProperties = properties.length / 2; |
| 88 | ||
| 89 | 0 | for (int i = 0; i < numberOfProperties; i++){ |
| 90 | 0 | Object propertyBean = properties[i]; |
| 91 | 0 | Object propertyOther = properties[i + numberOfProperties]; |
| 92 | ||
| 93 | 0 | if (propertyBean == null){ |
| 94 | 0 | if (propertyOther != null){ |
| 95 | 0 | return false; |
| 96 | } | |
| 97 | 0 | } else if (!propertyBean.equals(propertyOther)){ |
| 98 | 0 | return false; |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | 0 | return true; |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Test if 2 files are not the sames files. | |
| 107 | * | |
| 108 | * @param file The first file to test. | |
| 109 | * @param other The second file to test. | |
| 110 | * @return true if the files are not the same else false. | |
| 111 | */ | |
| 112 | public static boolean areNotSameFile(File file, File other) { | |
| 113 | 8 | if (file == null) { |
| 114 | 0 | if (other != null) { |
| 115 | 0 | return true; |
| 116 | } | |
| 117 | 8 | } else if (!file.getAbsolutePath().equals(other.getAbsolutePath())) { |
| 118 | 4 | return true; |
| 119 | } | |
| 120 | ||
| 121 | 4 | return false; |
| 122 | } | |
| 123 | } |