gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / CompareUtils.java @ 2884
History | View | Annotate | Download (8.37 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.tools.util; |
24 |
|
25 |
import java.math.BigDecimal; |
26 |
import java.math.BigInteger; |
27 |
import java.util.Comparator; |
28 |
import org.apache.commons.io.FilenameUtils; |
29 |
import org.apache.commons.io.IOCase; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
|
32 |
/**
|
33 |
*
|
34 |
* @author gvSIG Team
|
35 |
*/
|
36 |
public class CompareUtils { |
37 |
public static final Comparator EQUALS_IGNORECASE_COMPARATOR = new NullSafeComparator() { |
38 |
@Override
|
39 |
public int safeCompare(Object o1, Object o2) { |
40 |
return StringUtils.compareIgnoreCase(o1.toString(), o2.toString());
|
41 |
} |
42 |
}; |
43 |
|
44 |
public static abstract class NullSafeComparator<T> implements Comparator<T> { |
45 |
|
46 |
private final boolean nullIsLess; |
47 |
|
48 |
public NullSafeComparator(boolean nullIsLess) { |
49 |
this.nullIsLess = nullIsLess;
|
50 |
} |
51 |
|
52 |
public NullSafeComparator() {
|
53 |
this(true); |
54 |
} |
55 |
|
56 |
@Override
|
57 |
public final int compare(T o1, T o2) { |
58 |
if (o1 == o2) {
|
59 |
return 0; |
60 |
} |
61 |
if (o1 == null) { |
62 |
return nullIsLess ? -1 : 1; |
63 |
} |
64 |
if (o2 == null) { |
65 |
return nullIsLess ? 1 : - 1; |
66 |
} |
67 |
return safeCompare(o1, o2);
|
68 |
} |
69 |
|
70 |
public abstract int safeCompare(T o1, T o2); |
71 |
|
72 |
} |
73 |
|
74 |
public static boolean equals(String str1, String str2) { |
75 |
return compare(str1, str2, true)==0; |
76 |
} |
77 |
|
78 |
public static int compare(Comparable c1, Comparable c2) { |
79 |
return compare(c1, c2, true); |
80 |
} |
81 |
|
82 |
public static int compare(Comparator comparator, Object c1, Object c2) { |
83 |
return compare(comparator, c1, c2, true); |
84 |
} |
85 |
|
86 |
public static int compare(Comparator comparator, Object c1, Object c2, boolean nullIsLess) { |
87 |
if (c1 == c2) {
|
88 |
return 0; |
89 |
} |
90 |
if (c1 == null) { |
91 |
return nullIsLess ? -1 : 1; |
92 |
} |
93 |
if (c2 == null) { |
94 |
return nullIsLess ? 1 : - 1; |
95 |
} |
96 |
if( comparator!=null ) { |
97 |
return comparator.compare(c1, c2);
|
98 |
} |
99 |
if( c1 instanceof Comparable && c2 instanceof Comparable ) { |
100 |
return ((Comparable)c1).compareTo(((Comparable)c2)); |
101 |
} |
102 |
return EQUALS_IGNORECASE_COMPARATOR.compare(c1, c2);
|
103 |
} |
104 |
|
105 |
public static int compare(Comparable c1, Comparable c2, boolean nullIsLess) { |
106 |
if (c1 == c2) {
|
107 |
return 0; |
108 |
} |
109 |
if (c1 == null) { |
110 |
return nullIsLess ? -1 : 1; |
111 |
} |
112 |
if (c2 == null) { |
113 |
return nullIsLess ? 1 : - 1; |
114 |
} |
115 |
if( c1 instanceof CharSequence || c2 instanceof CharSequence ) { |
116 |
return compare(c1.toString(), c2.toString(), true); |
117 |
} |
118 |
|
119 |
if( c2 instanceof Number ){ |
120 |
if (c1 instanceof Double) { // Double - Number |
121 |
return c1.compareTo(((Number)c2).doubleValue()); |
122 |
} |
123 |
if (c1 instanceof Float) { // Float - Number |
124 |
return c1.compareTo(((Number)c2).floatValue()); |
125 |
} |
126 |
if (c1 instanceof BigDecimal) { // BigDecimal - Number |
127 |
if (c2 instanceof BigDecimal) { // BigDecimal - BigDecimal |
128 |
return c1.compareTo(c2);
|
129 |
} else { // BigDecimal - Number |
130 |
double value = ((Number)c2).doubleValue(); |
131 |
if(value == Double.NEGATIVE_INFINITY){ |
132 |
return 1; |
133 |
} |
134 |
if(value == Double.POSITIVE_INFINITY){ |
135 |
return -1; |
136 |
} |
137 |
return c1.compareTo(BigDecimal.valueOf(value)); |
138 |
} |
139 |
} |
140 |
if (c1 instanceof BigInteger) { // BigInteger - Number |
141 |
if (c2 instanceof BigInteger) { // BigInteger - BigInteger |
142 |
return c1.compareTo(c2);
|
143 |
} else {
|
144 |
long value = ((Number)c2).longValue(); |
145 |
return c1.compareTo(BigInteger.valueOf(value)); |
146 |
} |
147 |
} |
148 |
if (c1 instanceof Number) { |
149 |
if (c2 instanceof Double) { // Number - Double |
150 |
return Double.valueOf(((Number)c1).doubleValue()).compareTo(((Double)c2)); |
151 |
} |
152 |
if (c2 instanceof Float) { // Number - Float |
153 |
return Float.valueOf(((Number)c1).floatValue()).compareTo(((Float)c2)); |
154 |
} |
155 |
if (c2 instanceof BigDecimal) { // Number - BigDecimal |
156 |
double value = ((Number)c1).doubleValue(); |
157 |
if(value == Double.NEGATIVE_INFINITY){ |
158 |
return -1; |
159 |
} |
160 |
if(value == Double.POSITIVE_INFINITY){ |
161 |
return 1; |
162 |
} |
163 |
|
164 |
return BigDecimal.valueOf(value).compareTo((BigDecimal)c2); |
165 |
} |
166 |
if (c2 instanceof BigInteger) { // Number - BigInteger |
167 |
return BigInteger.valueOf(((Number)c1).longValue()).compareTo((BigInteger)c2); |
168 |
} |
169 |
|
170 |
// (Long, Int, Byte) - (Long, Int, Byte)
|
171 |
return Long.valueOf(((Number)c1).longValue()).compareTo(((Number)c2).longValue()); |
172 |
} |
173 |
} |
174 |
return c1.compareTo(c2);
|
175 |
} |
176 |
|
177 |
public static int compare(String str1, String str2) { |
178 |
return compare(str1, str2, true); |
179 |
} |
180 |
|
181 |
@SuppressWarnings("StringEquality") |
182 |
public static int compare(String str1, String str2, boolean nullIsLess) { |
183 |
if (str1 == str2) {
|
184 |
return 0; |
185 |
} |
186 |
if (str1 == null) { |
187 |
return nullIsLess ? -1 : 1; |
188 |
} |
189 |
if (str2 == null) { |
190 |
return nullIsLess ? 1 : - 1; |
191 |
} |
192 |
str2 = StringUtils.stripAccents(str2); |
193 |
str1 = StringUtils.stripAccents(str1); |
194 |
|
195 |
return StringUtils.compareIgnoreCase(str1, str2, nullIsLess);
|
196 |
} |
197 |
|
198 |
public static boolean match(Object value, String matcher) { |
199 |
if( value == null ) { |
200 |
return false; |
201 |
} |
202 |
return match(value.toString(),matcher);
|
203 |
} |
204 |
|
205 |
public static boolean match(String value, String matcher) { |
206 |
if ( StringUtils.isBlank(value) ) {
|
207 |
return false; |
208 |
} |
209 |
if (StringUtils.isBlank(matcher)) {
|
210 |
return true; |
211 |
} |
212 |
matcher = StringUtils.stripAccents(matcher); |
213 |
value = StringUtils.stripAccents(value); |
214 |
|
215 |
return FilenameUtils.wildcardMatch(value, matcher, IOCase.INSENSITIVE);
|
216 |
} |
217 |
|
218 |
public static boolean matchCaseSensitive(String value, String matcher) { |
219 |
if ( StringUtils.isBlank(value) ) {
|
220 |
return false; |
221 |
} |
222 |
if (StringUtils.isBlank(matcher)) {
|
223 |
return true; |
224 |
} |
225 |
matcher = StringUtils.stripAccents(matcher); |
226 |
value = StringUtils.stripAccents(value); |
227 |
|
228 |
return FilenameUtils.wildcardMatch(value, matcher, IOCase.SENSITIVE);
|
229 |
} |
230 |
|
231 |
public static boolean matchAccentsSensitive(String value, String matcher) { |
232 |
if ( StringUtils.isBlank(value) ) {
|
233 |
return false; |
234 |
} |
235 |
if (StringUtils.isBlank(matcher)) {
|
236 |
return true; |
237 |
} |
238 |
return FilenameUtils.wildcardMatch(value, matcher, IOCase.INSENSITIVE);
|
239 |
} |
240 |
|
241 |
public static boolean matchCaseAndAccentsSensitive(String value, String matcher) { |
242 |
if ( StringUtils.isBlank(value) ) {
|
243 |
return false; |
244 |
} |
245 |
if (StringUtils.isBlank(matcher)) {
|
246 |
return true; |
247 |
} |
248 |
return FilenameUtils.wildcardMatch(value, matcher, IOCase.SENSITIVE);
|
249 |
} |
250 |
} |