View Javadoc

1   /*
2    * Copyright 2007-2011 smartics, Kronseder & Reiner GmbH
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 de.smartics.exceptions.report.sort;
17  
18  import java.util.Comparator;
19  
20  import de.smartics.exceptions.code.DefaultCodeComparator;
21  import de.smartics.exceptions.code.NumberCode;
22  import de.smartics.exceptions.code.NumberCodeComparator;
23  import de.smartics.exceptions.core.Code;
24  import de.smartics.exceptions.report.sort.CodeUtils.CodeContainer;
25  
26  /**
27   * Compares exception code information instances. The instances are ordered by
28   * their codes. This implementation takes care if the code stored is a simple
29   * {@link Code} or a {@link NumberCode}.
30   *
31   * @todo It may be somewhat tricky to ensure that the compareTo rules are
32   *       enforced. Maybe we should remove this one?
33   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34   * @version $Revision:591 $
35   */
36  public class MixedCodeComparator implements Comparator<CodeContainer>
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    /**
43     * Singleton instance for reuse.
44     */
45    public static final MixedCodeComparator INSTANCE = new MixedCodeComparator();
46  
47    // --- members --------------------------------------------------------------
48  
49    // ****************************** Initializer *******************************
50  
51    // ****************************** Constructors ******************************
52  
53    /**
54     * Default constructor.
55     * <p>
56     * Consider to use {@link #INSTANCE the singleton}.
57     * </p>
58     */
59    public MixedCodeComparator()
60    {
61    }
62  
63    // ****************************** Inner Classes *****************************
64  
65    // ********************************* Methods ********************************
66  
67    // --- init -----------------------------------------------------------------
68  
69    // --- get&set --------------------------------------------------------------
70  
71    // --- business -------------------------------------------------------------
72  
73    /**
74     * {@inheritDoc}
75     *
76     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
77     */
78    public int compare(final CodeContainer o1, final CodeContainer o2)
79    {
80      final Code c1 = o1.getCode();
81      final Code c2 = o2.getCode();
82  
83      if (c1 instanceof NumberCode && c2 instanceof NumberCode)
84      {
85        final NumberCodeComparator<NumberCode> comparator =
86            new NumberCodeComparator<NumberCode>();
87        return comparator.compare((NumberCode) c1, (NumberCode) c2);
88      }
89      else
90      {
91        final DefaultCodeComparator<Code> comparator =
92            new DefaultCodeComparator<Code>();
93        return comparator.compare(c1, c2);
94      }
95    }
96  
97    // --- object basics --------------------------------------------------------
98  
99  }