View Javadoc
1   /*
2    * Copyright 2018 Andrew Rucker Jones.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.opencsv.bean.comparator;
17  
18  import org.apache.commons.collections4.comparators.ComparableComparator;
19  import org.apache.commons.collections4.comparators.ComparatorChain;
20  import org.apache.commons.collections4.comparators.FixedOrderComparator;
21  import org.apache.commons.collections4.comparators.NullComparator;
22  
23  import java.io.Serializable;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.Comparator;
27  import java.util.List;
28  
29  /**
30   * This {@link java.util.Comparator} takes an array of literals that define an
31   * order.
32   * Anything not included in the array is placed after anything in the array and
33   * is then sorted according to its natural order.
34   *
35   * @param <T> The type to be sorted
36   *
37   * @since 4.3
38   * @author Andrew Rucker Jones
39   * @deprecated This exact behavior can be had using comparators from Apache
40   * Commons Collections, which opencsv includes as a dependency. The following
41   * code gives the same result:
42   * {@code
43   * List<T> predefinedList = Arrays.<T>asList(predefinedOrder);
44   * FixedOrderComparator<T> fixedComparator = new FixedOrderComparator<>(predefinedList);
45   * fixedComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.AFTER);
46   * Comparator<T> c = new ComparatorChain<>(Arrays.<Comparator<T>>asList(
47   *     fixedComparator,
48   *     new NullComparator<>(false),
49   *     new ComparableComparator<>()));
50   * }
51   */
52  @Deprecated
53  public class LiteralComparator<T extends Comparable<T>> implements Comparator<T>, Serializable {
54      private static final long serialVersionUID = 1L;
55      private Comparator<T> c;
56  
57      /**
58       * Constructor.
59       *
60       * @param predefinedOrder Objects that define the order of comparison
61       */
62      public LiteralComparator(T[] predefinedOrder) {
63          List<T> predefinedList = predefinedOrder == null ? Collections.<T>emptyList() : Arrays.<T>asList(predefinedOrder);
64          FixedOrderComparator<T> fixedComparator = new FixedOrderComparator<>(predefinedList);
65          fixedComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.AFTER);
66          c = new ComparatorChain<>(Arrays.<Comparator<T>>asList(
67                  fixedComparator,
68                  new NullComparator<>(false),
69                  new ComparableComparator<>()));
70      }
71  
72      @Override
73      public int compare(T o1, T o2) {
74          return c.compare(o1, o2);
75      }
76  }