Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / CompareUtils.java @ 2350

History | View | Annotate | Download (4.05 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 org.apache.commons.io.FilenameUtils;
26
import org.apache.commons.io.IOCase;
27
import org.apache.commons.lang3.StringUtils;
28

    
29
/**
30
 *
31
 * @author gvSIG Team
32
 */
33
public class CompareUtils {
34

    
35
    public static boolean equals(String str1, String str2) {
36
        return compare(str1, str2, true)==0;
37
    }
38

    
39
    public static int compare(Comparable c1, Comparable c2) {
40
        return compare(c1, c2, true);
41
    }
42
    
43
    public static int compare(Comparable c1, Comparable c2, boolean nullIsLess) {
44
        if( c1 instanceof CharSequence && c2 instanceof CharSequence ) {
45
            return compare(c1.toString(), c2.toString(), true);
46
        }
47
        if (c1 == c2) {
48
            return 0;
49
        }
50
        if (c1 == null) {
51
            return nullIsLess ? -1 : 1;
52
        }
53
        if (c2 == null) {
54
            return nullIsLess ? 1 : - 1;
55
        }
56
        return c1.compareTo(c2);        
57
    }
58
    
59
    public static int compare(String str1, String str2) {
60
        return compare(str1, str2, true);
61
    }
62
    
63
    @SuppressWarnings("StringEquality")
64
    public static int compare(String str1, String str2, boolean nullIsLess) {
65
        if (str1 == str2) {
66
            return 0;
67
        }
68
        if (str1 == null) {
69
            return nullIsLess ? -1 : 1;
70
        }
71
        if (str2 == null) {
72
            return nullIsLess ? 1 : - 1;
73
        }
74
        str2 = StringUtils.stripAccents(str2);
75
        str1 = StringUtils.stripAccents(str1);
76
        
77
        return StringUtils.compareIgnoreCase(str1, str2, nullIsLess);
78
    }
79

    
80
    public static boolean match(Object value, String matcher) {
81
        if( value == null ) {
82
            return false;
83
        }
84
        return match(value.toString(),matcher);
85
    }
86
    
87
    public static boolean match(String value, String matcher) {
88
        if ( StringUtils.isBlank(value) ) {
89
            return false;
90
        }
91
        if (StringUtils.isBlank(matcher)) {
92
            return true;
93
        }
94
        matcher = StringUtils.stripAccents(matcher);
95
        value = StringUtils.stripAccents(value);
96
        
97
        return FilenameUtils.wildcardMatch(value, matcher, IOCase.INSENSITIVE);
98
    }
99

    
100
    public static boolean matchCaseSensitive(String value, String matcher) {
101
        if ( StringUtils.isBlank(value) ) {
102
            return false;
103
        }
104
        if (StringUtils.isBlank(matcher)) {
105
            return true;
106
        }
107
        matcher = StringUtils.stripAccents(matcher);
108
        value = StringUtils.stripAccents(value);
109
        
110
        return FilenameUtils.wildcardMatch(value, matcher, IOCase.SENSITIVE);
111
    }
112

    
113
    public static boolean matchAccentsSensitive(String value, String matcher) {
114
        if ( StringUtils.isBlank(value) ) {
115
            return false;
116
        }
117
        if (StringUtils.isBlank(matcher)) {
118
            return true;
119
        }
120
        return FilenameUtils.wildcardMatch(value, matcher, IOCase.INSENSITIVE);
121
    }
122

    
123
    public static boolean matchCaseAndAccentsSensitive(String value, String matcher) {
124
        if ( StringUtils.isBlank(value) ) {
125
            return false;
126
        }
127
        if (StringUtils.isBlank(matcher)) {
128
            return true;
129
        }
130
        return FilenameUtils.wildcardMatch(value, matcher, IOCase.SENSITIVE);
131
    }
132
}