Revision 11746

View differences:

trunk/libraries/libIverUtiles/src-test/com/iver/utiles/vectorUtilities/TestVectorUtilities.java
1 1
package com.iver.utiles.vectorUtilities;
2 2

  
3 3
import java.text.Collator;
4
import java.util.Comparator;
5 4
import java.util.Locale;
6 5
import java.util.Vector;
7 6

  
......
67 66
	 */
68 67
	protected void setUp() throws Exception {
69 68
		super.setUp();
70

  
71
//		v1 = new Vector();
72
//		v2 = new Vector();
73

  
69
		
74 70
		// Initialize the StringComparator
75 71
		stringComparator = new StringComparator();
76

  
77
		// Initialize the StringComparatorForItems
78
//		stringComparatorForItems = new StringComparatorForItems();
79

  
80
		// Initialize the vector of Items (for test methods without the parameter Comparator)
81
//		v1 = new Vector();
82
//		v1.add(new Item("extWMS"));
83
//		v1.add(new Item("libUI"));
84
//		v1.add(new Item("extWMS"));
85
//		v1.add(new Item("libRemoteServices"));
86
//		v1.add(new Item("extWFS2"));
87
//		v1.add(new Item("libNomenclatorIGN"));
88
//		v1.add(new Item("libNomenclatorIGN_GUI"));
89
//		v1.add(new Item("libIverUtiles"));
90
//		v1.add(new Item("extWFS2"));
91
//		v1.add(new Item("EXTWFS2"));
92
//		v1.add(new Item("extWFS2"));
93
//		v1.add(new Item("libGDBMS"));
94
//		v1.add(new Item("libInternationalization"));
95
//		v1.add(new Item("libFMap"));
96
//		v1.add(new Item("6"));
97
//		v1.add(new Item("4"));
98
//		v1.add(new Item("3"));
99
//		v1.add(new Item("5"));
100
//		v1.add(new Item("2"));
101
//		v1.add(new Item("1"));
102
//		v1.add(new Item("extWFS2"));
103
//		v1.add(new Item("libExceptions"));
104
//		v1.add(new Item("libDriverManager"));
105
//		v1.add(new Item("libCq CMS for java"));
106
//		v1.add(new Item("libCorePlugin"));
107
//		v1.add(new Item("extWFS2"));
108
//		v1.add(new Item("extAddIDEELayers"));
109
//		v1.add(new Item("extAlertCClient"));
110
//		v1.add(new Item("extCAD"));
111
//		v1.add(new Item("extWFS2"));
112
//		v1.add(new Item("?and?"));
113
//		v1.add(new Item("?and?"));
114
//		v1.add(new Item("extJDBC"));
115
//		v1.add(new Item("extMyPlugin"));
116
//		v1.add(new Item("extRasterTools"));
117
//		v1.add(new Item("extScripting"));
118
//		v1.add(new Item("extWCS"));
119
//		v1.add(new Item("extWFS2"));
120
//		v1.add(new Item("extwfs2"));
121
//		v1.add(new Item("extWMS"));
122
//		v1.add(new Item("extWMS"));
123
//		v1.add(new Item("extWCS"));
124
//		v1.add(new Item("7"));
125
//		v1.add(new Item("9"));
126
//		v1.add(new Item("8"));
127
//		v1.add(new Item("0"));
128
//		v1.add(new Item("EXTWCS"));
129
//		v1.add(new Item("?and?"));
130
//		v1.add(new Item("extensi?n"));
131
//		v1.add(new Item("?AND?"));
132
//		v1.add(new Item("_fwAndami"));
133
//		v1.add(new Item("abcdefghijklmn?opqrstuvwxyz"));
134
//		v1.add(new Item("?"));
135
//		v1.add(new Item("appgvSIG"));
136
//		v1.add(new Item("la casa"));
137
//		v1.add(new Item("la monta?a"));
138
//		v1.add(new Item("la colina"));
139
//		v1.add(new Item("las abejas"));
140
//		v1.add(new Item("las nutrias"));
141
//		v1.add(new Item("las ballenas"));
142
//		v1.add(new Item("lasa?a"));
143
//		v1.add(new Item("Vector"));
144
//		v1.add(new Item("?e"));
145

  
146
		// Initialize the vector of Strings (for test methods with the parameter Comparator)
147
//		v2 = new Vector();
148
//		v2.add("extWMS");
149
//		v2.add("libUI");
150
//		v2.add("extWMS");
151
//		v2.add("libRemoteServices");
152
//		v2.add("extWFS2");
153
//		v2.add("libNomenclatorIGN");
154
//		v2.add("libNomenclatorIGN_GUI");
155
//		v2.add("libIverUtiles");
156
//		v2.add("extWFS2");
157
//		v2.add("EXTWFS2");
158
//		v2.add("extWFS2");
159
//		v2.add("libGDBMS");
160
//		v2.add("libInternationalization");
161
//		v2.add("libFMap");
162
//		v2.add("6");
163
//		v2.add("4");
164
//		v2.add("3");
165
//		v2.add("5");
166
//		v2.add("2");
167
//		v2.add("1");
168
//		v2.add("extWFS2");
169
//		v2.add("libExceptions");
170
//		v2.add("libDriverManager");
171
//		v2.add("libCq CMS for java");
172
//		v2.add("libCorePlugin");
173
//		v2.add("extWFS2");
174
//		v2.add("extAddIDEELayers");
175
//		v2.add("extAlertCClient");
176
//		v2.add("extCAD");
177
//		v2.add("extWFS2");
178
//		v2.add("?and?");
179
//		v2.add("?and?");
180
//		v2.add("extJDBC");
181
//		v2.add("extMyPlugin");
182
//		v2.add("extRasterTools");
183
//		v2.add("extScripting");
184
//		v2.add("extWCS");
185
//		v2.add("extWFS2");
186
//		v2.add("extwfs2");
187
//		v2.add("extWMS");
188
//		v2.add("extWMS");
189
//		v2.add("extWCS");
190
//		v2.add("7");
191
//		v2.add("9");
192
//		v2.add("8");
193
//		v2.add("0");
194
//		v2.add("EXTWCS");
195
//		v2.add("?and?");
196
//		v2.add("extensi?n");
197
//		v2.add("?AND?");
198
//		v2.add("_fwAndami");
199
//		v2.add("abcdefghijklmn?opqrstuvwxyz");
200
//		v2.add("?");
201
//		v2.add("appgvSIG");
202
//		v2.add("la casa");
203
//		v2.add("la monta?a");
204
//		v2.add("la colina");
205
//		v2.add("las abejas");
206
//		v2.add("las nutrias");
207
//		v2.add("las ballenas");
208
//		v2.add("lasa?a");
209
//		v2.add("Vector");
210
//		v2.add("?e");
211
//		
72
		
212 73
		// Sort items of the two vectors
213 74
		Collator collator = Collator.getInstance(new Locale("es_ES"));
214
//		stringComparatorForItems.setLocaleRules(stringComparatorForItems.new LocaleRules(true, collator));
215
//		Collections.sort(v1.subList(0, v1.size()), stringComparatorForItems);
216 75
		
217
		// The algorithm of sort is the default used by Java (that makes some subsets: one for numbers, one for words that starts in big letters,
218
		//   another that stats in small letters, another with 'special' characters, etc)
219
//		Collections.sort(v1.subList(0, v1.size())); 
220
		
221 76
		stringComparator.setLocaleRules(stringComparator.new LocaleRules(true, collator));
222
//		Collections.sort(v2.subList(0, v2.size()), stringComparator);
223 77
	}
224 78

  
225 79
	/*
......
524 378
	}
525 379
	
526 380
	///// END TEST OPERATION: addAlphabeticallyOrdered(Vector, Object, Comparator) /////
527

  
528
	///// AUXILIAR CLASS THAT REPRESENTS AN ITEM WITH AN STRING VALUE AND IMPLEMENTING THE COMPARABLE INTERFACE /////
529
	
530
	/**
531
	 * This class is used for represent an item, with an string value, and implementing the interface Comparable for allow searches.
532
	 * 
533
	 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
534
	 */
535
	private static class Item implements Comparable {
536
		String value;
537

  
538
		/**
539
		 * Default contructor with 1 parameter
540
		 * 
541
		 * @param v1 Text value
542
		 */
543
		public Item (String v) {
544
			value = v;
545
		}
546
		
547
		/*
548
		 * (non-Javadoc)
549
		 * @see java.lang.Object#toString()
550
		 */
551
		public String toString() {
552
			return value;
553
		}
554

  
555
		/*
556
		 *  (non-Javadoc)
557
		 * @see java.lang.Comparable#compareTo(java.lang.Object)
558
		 */
559
		public int compareTo(Object o) {
560
			if (!(o instanceof Item))
561
				throw new ClassCastException("Se esperaba un objeto de tipo Item.");
562

  
563
			return this.value.compareTo(o.toString());
564
		}
565
	}
566

  
567
	///// AUXILIAR CLASS THAT REPRESENTS AN ITEM WITH AN STRING VALUE AND IMPLEMENTING THE COMPARABLE INTERFACE /////
568

  
569
    ///// END AUXILIAR CLASS THAT HAS A METHOD FOR COMPARE TWO ITEMS /////
570

  
571
	/**
572
	 * Compares two items (with String value) alphabetically
573
	 *
574
	 * @author Pablo Piqueras Bartolom?
575
	 */
576
	public class StringComparatorForItems implements Comparator {
577
		private boolean caseSensitive = true;
578
		private LocaleRules localeRules = null;
579

  
580
	    /**
581
	     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
582
	     */
583
	    public int compare(Object o1, Object o2) {
584
	        String s1 = ((Item) o1).toString();
585
	        String s2 = ((Item) o2).toString();
586

  
587
	        // If localeRules is null -> use the default rules
588
	        if (localeRules == null) {
589
	        	if (caseSensitive) {
590
	        		return s1.compareTo(s2);
591
	        	}
592
	        	else {
593
	        		return s1.compareToIgnoreCase(s2);
594
	        	}
595
	        }
596
	        else {
597
	        	if (localeRules.isUseLocaleRules()) {
598
	        		Collator collator = localeRules.getCollator();
599
	        		
600
	        		if (caseSensitive) {
601
	        			return collator.compare(s1, s2);
602
	        		}
603
	        		else {
604
	        			return collator.compare(s1.toLowerCase(), s2.toLowerCase());
605
	        		}
606
	        	}
607
	        	else {
608
	            	if (caseSensitive) {
609
	            		return s1.compareTo(s2);
610
	            	}
611
	            	else {
612
	            		return s1.compareToIgnoreCase(s2);
613
	            	}
614
	        	}
615
	        }
616
	    }
617

  
618
	    /**
619
	     * Returns if the comparator is sensitive to small and big letters
620
	     *
621
	     * @return
622
	     */
623
	    public boolean isCaseSensitive() {
624
	        return caseSensitive;
625
	    }
626

  
627
	    /**
628
	     * Establece la sensibilidad del comparador a las mayusculas y minusculas
629
	     *
630
	     * @param b
631
	     */
632
	    public void setCaseSensitive(boolean b) {
633
	        caseSensitive = b;
634
	    }
635
	    
636
	    /**
637
	     * Gets an object with the information for use the locale rules in comparation between strings. <br>
638
	     * <ul>
639
	     * <li>A boolean value -> if want or not use the locale rules</li>
640
	     * <li>A reference to the locale rules</li>
641
	     * </ul>
642
	     * 
643
	     * @return @see LocaleRules
644
	     */
645
	    public LocaleRules getLocaleRules() {
646
	    	return localeRules;
647
	    }    
648
	    /**
649
	     * Sets an object with the information for use the locale rules in comparation between strings. <br>
650
	     * <ul>
651
	     * <li>A boolean value -> if want or not use the locale rules</li>
652
	     * <li>A reference to the locale rules</li>
653
	     * </ul>
654
	     * 
655
	     * @param @see LocaleRules
656
	     */
657
	    public void setLocaleRules(LocaleRules locRules) {
658
	    	localeRules = locRules;
659
	    }
660
	    
661
	    /**
662
	     * Represents the information needed by <i>StringComparator</i> for use or not locale-sensitive String comparison-rules in the <b><i>compare</i></b> method
663
	     * 
664
	     * @author Pablo Piqueras Bartolom?
665
	     */
666
	    public class LocaleRules {
667
	    	 private boolean useLocaleRules;
668
	    	 private Collator _collator;
669
	    	 
670
	    	 /**
671
	    	  * Default constructor without parameters
672
	    	  */
673
	    	 public LocaleRules() {
674
	    		 useLocaleRules = false;
675
	    		 _collator = null;
676
	    	 }
677
	    	 
678
	    	 /**
679
	    	  * Default constructor with two parameters
680
	    	  * 
681
	    	  * @param b Use locale rules
682
	    	  * @param collator A reference to an object configurated for locale-sensitive String comparison
683
	    	  */
684
	    	 public LocaleRules(boolean b, Collator collator) {
685
	    		 useLocaleRules = b;
686
	    		 _collator = collator;
687
	    	 }
688
	    	 
689
	 		/**
690
	 		 * Gets the value of the inner attribute <i>_collator</i>
691
	 		 * 
692
	 		 * @return Returns A reference to an object configurated for locale-sensitive String comparison
693
	 		 */
694
	 		public Collator getCollator() {
695
	 			return _collator;
696
	 		}
697

  
698
	 		/**
699
	 		 * Sets a value to the inner attribute <i>_collator</i>
700
	 		 * 
701
	 		 * @param collator A reference to an object configurated for locale-sensitive String comparison
702
	 		 */
703
	 		public void setCollator(Collator collator) {
704
	 			this._collator = collator;
705
	 		}
706

  
707
			/**
708
			 * Gets the value of the inner attribute <i>useLocaleRules</i>
709
			 * 
710
			 * @return Returns the useLocaleRules.
711
			 */
712
			public boolean isUseLocaleRules() {
713
				return useLocaleRules;
714
			}
715

  
716
			/**
717
			 * Sets a value to the inner attribute <i>useLocaleRules</i>
718
			 * 
719
			 * @param useLocaleRules The useLocaleRules to set.
720
			 */
721
			public void setUseLocaleRules(boolean useLocaleRules) {
722
				this.useLocaleRules = useLocaleRules;
723
			}
724
	    }
725
	}
726
	///// END AUXILIAR CLASS THAT HAS A METHOD FOR COMPARE TWO ITEMS /////
727 381
}

Also available in: Unified diff