| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ComparisonUtils |
|
| 4.714285714285714;4.714 |
| 1 | /* | |
| 2 | * Copyright (c) 2000 | |
| 3 | * Jon Schewe. All rights reserved | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * | |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 24 | * SUCH DAMAGE. | |
| 25 | * | |
| 26 | * I'd appreciate comments/suggestions on the code jpschewe@mtu.net | |
| 27 | */ | |
| 28 | package net.mtu.eggplant.util; | |
| 29 | ||
| 30 | /** | |
| 31 | * Handy comparision routines. | |
| 32 | * | |
| 33 | * @version $Revision$ | |
| 34 | */ | |
| 35 | public final class ComparisonUtils { | |
| 36 | ||
| 37 | 0 | private ComparisonUtils() { |
| 38 | 0 | } |
| 39 | ||
| 40 | /** | |
| 41 | * true is always greater than false. | |
| 42 | **/ | |
| 43 | public static int compareBooleans(final boolean one, | |
| 44 | final boolean two) { | |
| 45 | 0 | if (one == two) { |
| 46 | 0 | return 0; |
| 47 | 0 | } else if (one) { |
| 48 | 0 | return -1; |
| 49 | } else { | |
| 50 | 0 | return 1; |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * Compare two non-floating point numbers. | |
| 56 | **/ | |
| 57 | public static int compareIntegers(final long one, | |
| 58 | final long two) { | |
| 59 | 0 | if (one == two) { |
| 60 | 0 | return 0; |
| 61 | 0 | } else if (one < two) { |
| 62 | 0 | return -1; |
| 63 | } else { | |
| 64 | 0 | return 1; |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Compare two doubles. Comparisions exist for double and float because | |
| 70 | * casting a float to a double and vice versa can cause loss of precision. | |
| 71 | **/ | |
| 72 | public static int compareDoubles(final double one, | |
| 73 | final double two) { | |
| 74 | 0 | if (one == two) { |
| 75 | 0 | return 0; |
| 76 | 0 | } else if (one < two) { |
| 77 | 0 | return -1; |
| 78 | } else { | |
| 79 | 0 | return 1; |
| 80 | } | |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Compare two floats. Comparisions exist for double and float because casting | |
| 85 | * a float to a double and vice versa can cause loss of precision. | |
| 86 | **/ | |
| 87 | public static int compareFloats(final float one, | |
| 88 | final float two) { | |
| 89 | 0 | if (one == two) { |
| 90 | 0 | return 0; |
| 91 | 0 | } else if (one < two) { |
| 92 | 0 | return -1; |
| 93 | } else { | |
| 94 | 0 | return 1; |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Compare two Strings, null is allowed and is greater than every non-null | |
| 100 | * String. | |
| 101 | **/ | |
| 102 | public static int compareStrings(final String one, | |
| 103 | final String two) { | |
| 104 | 0 | if (one == null) { |
| 105 | 0 | if (two == null) { |
| 106 | 0 | return 0; |
| 107 | } else { | |
| 108 | 0 | return 1; |
| 109 | } | |
| 110 | } else { | |
| 111 | 0 | if (two == null) { |
| 112 | 0 | return -1; |
| 113 | } else { | |
| 114 | 0 | return one.compareTo(two); |
| 115 | } | |
| 116 | } | |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * Equals call that handles null without a NullPointerException. | |
| 121 | */ | |
| 122 | public static boolean safeEquals(final Object o1, | |
| 123 | final Object o2) { | |
| 124 | 0 | if (o1 == o2) { |
| 125 | 0 | return true; |
| 126 | 0 | } else if (null == o1) { |
| 127 | 0 | return false; |
| 128 | } else { | |
| 129 | 0 | return o1.equals(o2); |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | } |