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          public int getColumn(final int colIndex) {
87              return cols[colIndex];
88          }
89  
90          @Override
91          public String toString() {
92              return "[" + cols[0] + "," + cols[1] + "," + cols[2] + "]";
93          }
94      }
95  
96      public ComparatorChainTest() {
97          super(ComparatorChainTest.class.getSimpleName());
98      }
99  
100 //    public void testCreate() throws Exception {
101 //        writeExternalFormToDisk((java.io.Serializable) makeObject(), "src/test/resources/data/test/ComparatorChain.version4.obj");
102 //    }
103 
104     @Override
105     public List<PseudoRow> getComparableObjectsOrdered() {
106         // this is the correct order assuming a
107         // "0th forward, 1st reverse, 2nd forward" sort
108         return new LinkedList<>(Arrays.asList(new PseudoRow(1, 2, 3), new PseudoRow(2, 3, 5),
109                 new PseudoRow(2, 2, 4), new PseudoRow(2, 2, 8), new PseudoRow(3, 1, 0),
110                 new PseudoRow(4, 4, 4), new PseudoRow(4, 4, 7)));
111     }
112 
113     @Override
114     public String getCompatibilityVersion() {
115         return "4";
116     }
117 
118     @Override
119     public Comparator<PseudoRow> makeObject() {
120         final ComparatorChain<PseudoRow> chain = new ComparatorChain<>(new ColumnComparator(0));
121         chain.addComparator(new ColumnComparator(1), true); // reverse the second column
122         chain.addComparator(new ColumnComparator(2), false);
123         return chain;
124     }
125 
126     @Test
127     public void testBadListComparatorChain() {
128         final List<Comparator<Integer>> list = new LinkedList<>();
129         final ComparatorChain<Integer> chain = new ComparatorChain<>(list);
130         final Integer i1 = 4;
131         final Integer i2 = 6;
132 
133         assertThrows(UnsupportedOperationException.class, () -> chain.compare(i1, i2));
134     }
135 
136     @Test
137     public void testBadNoopComparatorChain() {
138         final ComparatorChain<Integer> chain = new ComparatorChain<>();
139         final Integer i1 = 4;
140         final Integer i2 = 6;
141 
142         assertThrows(UnsupportedOperationException.class, () -> chain.compare(i1, i2), "An exception should be thrown when a chain contains zero comparators.");
143     }
144 
145     @Test
146     public void testComparatorChainOnMinValuedComparator() {
147         // -1 * Integer.MIN_VALUE is less than 0,
148         // test that ComparatorChain handles this edge case correctly
149         final ComparatorChain<Integer> chain = new ComparatorChain<>();
150         chain.addComparator((a, b) -> {
151             final int result = a.compareTo(b);
152             if (result < 0) {
153                 return Integer.MIN_VALUE;
154             }
155             if (result > 0) {
156                 return Integer.MAX_VALUE;
157             }
158             return 0;
159         }, true);
160 
161         assertTrue(chain.compare(4, 5) > 0);
162         assertTrue(chain.compare(5, 4) < 0);
163         assertEquals(0, chain.compare(4, 4));
164     }
165 
166     @Test
167     public void testListComparatorChain() {
168         final List<Comparator<Integer>> list = new LinkedList<>();
169         list.add(new ComparableComparator<>());
170         final ComparatorChain<Integer> chain = new ComparatorChain<>(list);
171         final Integer i1 = 4;
172         final Integer i2 = 6;
173 
174         final int correctValue = i1.compareTo(i2);
175         assertEquals(chain.compare(i1, i2), correctValue, "Comparison returns the right order");
176     }
177 
178     @Test
179     public void testNoopComparatorChain() {
180         final ComparatorChain<Integer> chain = new ComparatorChain<>();
181         final Integer i1 = 4;
182         final Integer i2 = 6;
183         chain.addComparator(new ComparableComparator<>());
184 
185         final int correctValue = i1.compareTo(i2);
186         assertEquals(chain.compare(i1, i2), correctValue, "Comparison returns the right order");
187     }
188 
189 }