gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / CompareUtils.java @ 2753
History | View | Annotate | Download (5.83 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.util.Comparator; |
26 |
import org.apache.commons.io.FilenameUtils; |
27 |
import org.apache.commons.io.IOCase; |
28 |
import org.apache.commons.lang3.StringUtils; |
29 |
|
30 |
/**
|
31 |
*
|
32 |
* @author gvSIG Team
|
33 |
*/
|
34 |
public class CompareUtils { |
35 |
public static final Comparator EQUALS_IGNORECASE_COMPARATOR = new NullSafeComparator() { |
36 |
@Override
|
37 |
public int safeCompare(Object o1, Object o2) { |
38 |
return StringUtils.compareIgnoreCase(o1.toString(), o2.toString());
|
39 |
} |
40 |
}; |
41 |
|
42 |
public static abstract class NullSafeComparator<T> implements Comparator<T> { |
43 |
|
44 |
private final boolean nullIsLess; |
45 |
|
46 |
public NullSafeComparator(boolean nullIsLess) { |
47 |
this.nullIsLess = nullIsLess;
|
48 |
} |
49 |
|
50 |
public NullSafeComparator() {
|
51 |
this(true); |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public final int compare(T o1, T o2) { |
56 |
if (o1 == o2) {
|
57 |
return 0; |
58 |
} |
59 |
if (o1 == null) { |
60 |
return nullIsLess ? -1 : 1; |
61 |
} |
62 |
if (o2 == null) { |
63 |
return nullIsLess ? 1 : - 1; |
64 |
} |
65 |
return safeCompare(o1, o2);
|
66 |
} |
67 |
|
68 |
public abstract int safeCompare(T o1, T o2); |
69 |
|
70 |
} |
71 |
|
72 |
public static boolean equals(String str1, String str2) { |
73 |
return compare(str1, str2, true)==0; |
74 |
} |
75 |
|
76 |
public static int compare(Comparable c1, Comparable c2) { |
77 |
return compare(c1, c2, true); |
78 |
} |
79 |
|
80 |
public static int compare(Comparator comparator, Object c1, Object c2) { |
81 |
return compare(comparator, c1, c2, true); |
82 |
} |
83 |
|
84 |
public static int compare(Comparator comparator, Object c1, Object c2, boolean nullIsLess) { |
85 |
if (c1 == c2) {
|
86 |
return 0; |
87 |
} |
88 |
if (c1 == null) { |
89 |
return nullIsLess ? -1 : 1; |
90 |
} |
91 |
if (c2 == null) { |
92 |
return nullIsLess ? 1 : - 1; |
93 |
} |
94 |
if( comparator!=null ) { |
95 |
return comparator.compare(c1, c2);
|
96 |
} |
97 |
if( c1 instanceof Comparable && c2 instanceof Comparable ) { |
98 |
return ((Comparable)c1).compareTo(((Comparable)c2)); |
99 |
} |
100 |
return EQUALS_IGNORECASE_COMPARATOR.compare(c1, c2);
|
101 |
} |
102 |
|
103 |
public static int compare(Comparable c1, Comparable c2, boolean nullIsLess) { |
104 |
if( c1 instanceof CharSequence && c2 instanceof CharSequence ) { |
105 |
return compare(c1.toString(), c2.toString(), true); |
106 |
} |
107 |
if (c1 == c2) {
|
108 |
return 0; |
109 |
} |
110 |
if (c1 == null) { |
111 |
return nullIsLess ? -1 : 1; |
112 |
} |
113 |
if (c2 == null) { |
114 |
return nullIsLess ? 1 : - 1; |
115 |
} |
116 |
return c1.compareTo(c2);
|
117 |
} |
118 |
|
119 |
public static int compare(String str1, String str2) { |
120 |
return compare(str1, str2, true); |
121 |
} |
122 |
|
123 |
@SuppressWarnings("StringEquality") |
124 |
public static int compare(String str1, String str2, boolean nullIsLess) { |
125 |
if (str1 == str2) {
|
126 |
return 0; |
127 |
} |
128 |
if (str1 == null) { |
129 |
return nullIsLess ? -1 : 1; |
130 |
} |
131 |
if (str2 == null) { |
132 |
return nullIsLess ? 1 : - 1; |
133 |
} |
134 |
str2 = StringUtils.stripAccents(str2); |
135 |
str1 = StringUtils.stripAccents(str1); |
136 |
|
137 |
return StringUtils.compareIgnoreCase(str1, str2, nullIsLess);
|
138 |
} |
139 |
|
140 |
public static boolean match(Object value, String matcher) { |
141 |
if( value == null ) { |
142 |
return false; |
143 |
} |
144 |
return match(value.toString(),matcher);
|
145 |
} |
146 |
|
147 |
public static boolean match(String value, String matcher) { |
148 |
if ( StringUtils.isBlank(value) ) {
|
149 |
return false; |
150 |
} |
151 |
if (StringUtils.isBlank(matcher)) {
|
152 |
return true; |
153 |
} |
154 |
matcher = StringUtils.stripAccents(matcher); |
155 |
value = StringUtils.stripAccents(value); |
156 |
|
157 |
return FilenameUtils.wildcardMatch(value, matcher, IOCase.INSENSITIVE);
|
158 |
} |
159 |
|
160 |
public static boolean matchCaseSensitive(String value, String matcher) { |
161 |
if ( StringUtils.isBlank(value) ) {
|
162 |
return false; |
163 |
} |
164 |
if (StringUtils.isBlank(matcher)) {
|
165 |
return true; |
166 |
} |
167 |
matcher = StringUtils.stripAccents(matcher); |
168 |
value = StringUtils.stripAccents(value); |
169 |
|
170 |
return FilenameUtils.wildcardMatch(value, matcher, IOCase.SENSITIVE);
|
171 |
} |
172 |
|
173 |
public static boolean matchAccentsSensitive(String value, String matcher) { |
174 |
if ( StringUtils.isBlank(value) ) {
|
175 |
return false; |
176 |
} |
177 |
if (StringUtils.isBlank(matcher)) {
|
178 |
return true; |
179 |
} |
180 |
return FilenameUtils.wildcardMatch(value, matcher, IOCase.INSENSITIVE);
|
181 |
} |
182 |
|
183 |
public static boolean matchCaseAndAccentsSensitive(String value, String matcher) { |
184 |
if ( StringUtils.isBlank(value) ) {
|
185 |
return false; |
186 |
} |
187 |
if (StringUtils.isBlank(matcher)) {
|
188 |
return true; |
189 |
} |
190 |
return FilenameUtils.wildcardMatch(value, matcher, IOCase.SENSITIVE);
|
191 |
} |
192 |
} |