Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / CompareLists.java @ 40561

History | View | Annotate | Download (6.99 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils;
25

    
26
import java.util.Comparator;
27
import java.util.List;
28

    
29
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
30
 *
31
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
32
 *
33
 * This program is free software; you can redistribute it and/or
34
 * modify it under the terms of the GNU General Public License
35
 * as published by the Free Software Foundation; either version 2
36
 * of the License, or (at your option) any later version.
37
 *
38
 * This program is distributed in the hope that it will be useful,
39
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41
 * GNU General Public License for more details.
42
 *
43
 * You should have received a copy of the GNU General Public License
44
 * along with this program; if not, write to the Free Software
45
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
46
 *
47
 * For more information, contact:
48
 *
49
 *  Generalitat Valenciana
50
 *   Conselleria d'Infraestructures i Transport
51
 *   Av. Blasco Ib??ez, 50
52
 *   46010 VALENCIA
53
 *   SPAIN
54
 *
55
 *      +34 963862235
56
 *   gvsig@gva.es
57
 *      www.gvsig.gva.es
58
 *
59
 *    or
60
 *
61
 *   IVER T.I. S.A
62
 *   Salamanca 50
63
 *   46005 Valencia
64
 *   Spain
65
 *
66
 *   +34 963163400
67
 *   dac@iver.es
68
 */
69

    
70
/**
71
 * This class has methods to compare lists of objects
72
 * 
73
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
74
 */
75
public class CompareLists {
76
        /**
77
         * Compares two lists of elements, using the value returned of the <i><b>toString()</b></i> method
78
         *   of each element. <br>
79
         * Each element must also implement the {@link Comparable} interface. <br>
80
         * This method considers case sensitive .
81
         * 
82
         * @param l1 First list of items. @see java.util.List
83
         * @param l2 Second list of items. @see java.util.List
84
         * @return True if the two lists have the same number of elements, and elements are in the same
85
         *   position of the two lists and have the same <i>String</i> value .
86
         */
87
        public synchronized static boolean compare(List l1, List l2) {
88
                // If both are null -> true; if one yes but the other no -> return false
89
                if ((l1 == null) || (l2 == null)) {
90
                        if (l1 == l2)
91
                                return true;
92
                        else
93
                                return false;
94
                }
95
                
96
                // If the length isn't equal
97
                if (l1.size() != l2.size())
98
                        return false;
99

    
100
                // Compares each item: must have the same value in the same position in the list
101
                for (int i = 0; i < l1.size(); i++) {
102
                        if ( l1.get(i).toString().compareTo(l2.get(i).toString()) != 0 )
103
                                return false;
104
                }
105
                
106
                return true;
107
        }
108
        
109
        /**
110
         * Compares two lists of elements, using the value returned of the <i><b>toString()</b></i> method
111
         *   of each element. <br>
112
         * Each element must also implement the {@link Comparable} interface. <br>
113
         * This method ignores case sensitive during the comparation.
114
         * 
115
         * @param l1 First list of items. @see java.util.List
116
         * @param l2 Second list of items. @see java.util.List
117
         * @return True if the two lists have the same number of elements, and elements are in the same
118
         *   position of the two lists and have the same <i>String</i> value .
119
         */
120
        public synchronized static boolean compareIgnoringCaseSensitive(List l1, List l2) {
121
                // If both are null -> true; if one yes but the other no -> return false
122
                if ((l1 == null) || (l2 == null)) {
123
                        if (l1 == l2)
124
                                return true;
125
                        else
126
                                return false;
127
                }
128

    
129
                // If the length isn't equal
130
                if (l1.size() != l2.size())
131
                        return false;
132

    
133
                // Compares each item: must have the same value in the same position in the list
134
                for (int i = 0; i < l1.size(); i++) {
135
                        if ( l1.get(i).toString().compareToIgnoreCase(l2.get(i).toString()) != 0 )
136
                                return false;
137
                }
138
                
139
                return true;
140
        }
141
        
142
        /**
143
         * Compares two lists of elements, using the value returned of the <i><b>toString()</b></i> method
144
         *   of each element. <br>
145
         * Each element must also implement the {@link Comparable} interface. <br>
146
         * 
147
         * @param l1 First list of items. @see java.util.List
148
         * @param l2 Second list of items. @see java.util.List
149
         * @param comp A class which implements the <i><b>compare</b></i> method of the <i>Comparator</i> interface . 
150
         * @return True if the two lists have the same number of elements, and elements are in the same
151
         *   position of the two lists and have the same <i>String</i> value .
152
         */
153
        public synchronized static boolean compare(List l1, List l2, Comparator comp) {
154
                // If both are null -> true; if one yes but the other no -> return false
155
                if ((l1 == null) || (l2 == null)) {
156
                        if (l1 == l2)
157
                                return true;
158
                        else
159
                                return false;
160
                }
161

    
162
                // If the length isn't equal
163
                if (l1.size() != l2.size())
164
                        return false;
165

    
166
                // Compares each item: must have the same value in the same position in the list
167
                for (int i = 0; i < l1.size(); i++) {
168
                        if ( comp.compare(l1.get(i).toString(), (l2.get(i).toString())) != 0 )
169
                                return false;
170
                }
171

    
172
                return true;
173
        }
174
        
175
        /**
176
         * Compares two lists of elements, using the value returned of the <i><b>toString()</b></i> method
177
         *   of each element. <br>
178
         * Each element must also implement the {@link Comparable} interface. <br>
179
         * This method ignores case sensitive during the comparation.
180
         * 
181
         * @param l1 First list of items. @see java.util.List
182
         * @param l2 Second list of items. @see java.util.List
183
         * @param comp A class which implements the <i><b>compare</b></i> method of the <i>Comparator</i> interface . 
184
         * @return True if the two lists have the same number of elements, and elements are in the same
185
         *   position of the two lists and have the same <i>String</i> value .
186
         */
187
        public synchronized static boolean compareIgnoringCaseSensitive(List l1, List l2, Comparator comp) {
188
                // If both are null -> true; if one yes but the other no -> return false
189
                if ((l1 == null) || (l2 == null)) {
190
                        if (l1 == l2)
191
                                return true;
192
                        else
193
                                return false;
194
                }
195

    
196
                // If the length isn't equal
197
                if (l1.size() != l2.size())
198
                        return false;
199

    
200
                // Compares each item: must have the same value in the same position in the list
201
                for (int i = 0; i < l1.size(); i++) {
202
                        if ( comp.compare(l1.get(i).toString().toLowerCase(), (l2.get(i).toString().toLowerCase())) != 0 )
203
                                return false;
204
                }
205

    
206
                return true;
207
        }
208
}