View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.comparators;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.Serializable;
24  import java.util.Arrays;
25  import java.util.Comparator;
26  import java.util.LinkedList;
27  import java.util.List;
28  
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests for ComparatorChain.
33   */
34  public class ComparatorChainTest extends AbstractComparatorTest<ComparatorChainTest.PseudoRow> {
35  
36      public static class ColumnComparator implements Comparator<PseudoRow>, Serializable {
37          private static final long serialVersionUID = -2284880866328872105L;
38  
39          protected int colIndex;
40  
41          public ColumnComparator(final int colIndex) {
42              this.colIndex = colIndex;
43          }
44  
45          @Override
46          public int compare(final PseudoRow o1, final PseudoRow o2) {
47              return Integer.compare(o1.getColumn(colIndex), o2.getColumn(colIndex));
48          }
49  
50          @Override
51          public boolean equals(final Object that) {
52              return that instanceof ColumnComparator && colIndex == ((ColumnComparator) that).colIndex;
53          }
54  
55          @Override
56          public int hashCode() {
57              return colIndex;
58          }
59      }
60  
61      public static class PseudoRow implements Serializable {
62  
63          /**
64           * Generated serial version ID.
65           */
66          private static final long serialVersionUID = 8085570439751032499L;
67          public int[] cols = new int[3];
68  
69          public PseudoRow(final int col1, final int col2, final int col3) {
70              cols[0] = col1;
71              cols[1] = col2;
72              cols[2] = col3;
73          }
74  
75          @Override
76          public boolean equals(final Object o) {
77              if (!(o instanceof PseudoRow)) {
78                  return false;
79              }
80  
81              final PseudoRow row = (PseudoRow) o;
82  
83              return getColumn(0) == row.getColumn(0) && getColumn(1) == row.getColumn(1) && getColumn(2) == row.getColumn(2);
84          }
85  
86          @Override
87          public int hashCode() {
88              return Arrays.hashCode(cols);
89          }
90  
91          public int getColumn(final int colIndex) {
92              return cols[colIndex];
93          }
94  
95          @Override
96          public String toString() {
97              return "[" + cols[0] + "," + cols[1] + "," + cols[2] + "]";
98          }
99      }
100 
101 //    public void testCreate() throws Exception {
102 //        writeExternalFormToDisk((java.io.Serializable) makeObject(), "src/test/resources/data/test/ComparatorChain.version4.obj");
103 //    }
104 
105     @Override
106     public List<PseudoRow> getComparableObjectsOrdered() {
107         // this is the correct order assuming a
108         // "0th forward, 1st reverse, 2nd forward" sort
109         return new LinkedList<>(Arrays.asList(new PseudoRow(1, 2, 3), new PseudoRow(2, 3, 5),
110                 new PseudoRow(2, 2, 4), new PseudoRow(2, 2, 8), new PseudoRow(3, 1, 0),
111                 new PseudoRow(4, 4, 4), new PseudoRow(4, 4, 7)));
112     }
113 
114     @Override
115     public String getCompatibilityVersion() {
116         return "4";
117     }
118 
119     @Override
120     public Comparator<PseudoRow> makeObject() {
121         final ComparatorChain<PseudoRow> chain = new ComparatorChain<>(new ColumnComparator(0));
122         chain.addComparator(new ColumnComparator(1), true); // reverse the second column
123         chain.addComparator(new ColumnComparator(2), false);
124         return chain;
125     }
126 
127     @Test
128     public void testBadListComparatorChain() {
129         final List<Comparator<Integer>> list = new LinkedList<>();
130         final ComparatorChain<Integer> chain = new ComparatorChain<>(list);
131         final Integer i1 = 4;
132         final Integer i2 = 6;
133 
134         assertThrows(UnsupportedOperationException.class, () -> chain.compare(i1, i2));
135     }
136 
137     @Test
138     public void testBadNoopComparatorChain() {
139         final ComparatorChain<Integer> chain = new ComparatorChain<>();
140         final Integer i1 = 4;
141         final Integer i2 = 6;
142 
143         assertThrows(UnsupportedOperationException.class, () -> chain.compare(i1, i2), "An exception should be thrown when a chain contains zero comparators.");
144     }
145 
146     @Test
147     public void testComparatorChainOnMinValuedComparator() {
148         // -1 * Integer.MIN_VALUE is less than 0,
149         // test that ComparatorChain handles this edge case correctly
150         final ComparatorChain<Integer> chain = new ComparatorChain<>();
151         chain.addComparator((a, b) -> {
152             final int result = a.compareTo(b);
153             if (result < 0) {
154                 return Integer.MIN_VALUE;
155             }
156             if (result > 0) {
157                 return Integer.MAX_VALUE;
158             }
159             return 0;
160         }, true);
161 
162         assertTrue(chain.compare(4, 5) > 0);
163         assertTrue(chain.compare(5, 4) < 0);
164         assertEquals(0, chain.compare(4, 4));
165     }
166 
167     @Test
168     public void testListComparatorChain() {
169         final List<Comparator<Integer>> list = new LinkedList<>();
170         list.add(new ComparableComparator<>());
171         final ComparatorChain<Integer> chain = new ComparatorChain<>(list);
172         final Integer i1 = 4;
173         final Integer i2 = 6;
174 
175         final int correctValue = i1.compareTo(i2);
176         assertEquals(chain.compare(i1, i2), correctValue, "Comparison returns the right order");
177     }
178 
179     @Test
180     public void testNoopComparatorChain() {
181         final ComparatorChain<Integer> chain = new ComparatorChain<>();
182         final Integer i1 = 4;
183         final Integer i2 = 6;
184         chain.addComparator(new ComparableComparator<>());
185 
186         final int correctValue = i1.compareTo(i2);
187         assertEquals(chain.compare(i1, i2), correctValue, "Comparison returns the right order");
188     }
189 
190 }