View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.directory.modules.multiview.business.record.column;
35  
36  import java.io.Serializable;
37  import java.util.Comparator;
38  import java.util.Date;
39  
40  import org.apache.commons.lang3.math.NumberUtils;
41  
42  /**
43   * Comparator for RecordColumnCell objects
44   */
45  public class RecordColumnCellComparator implements Comparator<RecordColumnCell>, Serializable
46  {
47      // Generated UID
48      private static final long serialVersionUID = -7519611978697011989L;
49  
50      // Variables
51      private final String _strSortKey;
52  
53      /**
54       * Constructor
55       * 
56       * @param strSortKey
57       *            The key used to sort the RecordColumnCell
58       */
59      public RecordColumnCellComparator( String strSortKey )
60      {
61          _strSortKey = strSortKey;
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public int compare( RecordColumnCell recordColumnCellOne, RecordColumnCell recordColumnCellTwo )
69      {
70          int nComparisonResult = NumberUtils.INTEGER_ZERO;
71  
72          if ( recordColumnCellOne == null )
73          {
74              if ( recordColumnCellTwo != null )
75              {
76                  nComparisonResult = NumberUtils.INTEGER_MINUS_ONE;
77              }
78          }
79          else
80          {
81              if ( recordColumnCellTwo == null )
82              {
83                  nComparisonResult = NumberUtils.INTEGER_ONE;
84              }
85              else
86              {
87                  Object objectOne = recordColumnCellOne.getRecordColumnCellValueByName( _strSortKey );
88                  Object objectTwo = recordColumnCellTwo.getRecordColumnCellValueByName( _strSortKey );
89  
90                  nComparisonResult = compareRecordColumnCellObject( objectOne, objectTwo );
91              }
92          }
93  
94          return nComparisonResult;
95      }
96  
97      /**
98       * Compare two RecordColumnCell object values by converting them as String
99       * 
100      * @param objectOne
101      *            The first object to compare
102      * @param objectTwo
103      *            The second object to compare
104      * @return the result of the comparison of the two given objects
105      */
106     private int compareRecordColumnCellObject( Object objectOne, Object objectTwo )
107     {
108         int nComparisonResult = NumberUtils.INTEGER_ZERO;
109 
110         if ( objectOne == null )
111         {
112             if ( objectTwo != null )
113             {
114                 nComparisonResult = NumberUtils.INTEGER_MINUS_ONE;
115             }
116         }
117         else
118         {
119             if ( objectTwo == null )
120             {
121                 nComparisonResult = NumberUtils.INTEGER_ONE;
122             }
123             else
124             {
125                 nComparisonResult = compareObject( objectOne, objectTwo );
126             }
127         }
128 
129         return nComparisonResult;
130     }
131 
132     /**
133      * Make the comparison between the two given object. If there are both Date a comparison will be make on the two Dates otherwise it their comparison as
134      * String which will be made
135      * 
136      * @param objectOne
137      *            The first object to compare
138      * @param objectTwo
139      *            The second object to compare
140      * @return the result of the comparison between the two given objects
141      */
142     private int compareObject( Object objectOne, Object objectTwo )
143     {
144         int nComparisonResult = NumberUtils.INTEGER_ZERO;
145 
146         if ( objectOne instanceof Date && objectTwo instanceof Date )
147         {
148             Date dateOne = (Date) objectOne;
149             Date dateTwo = (Date) objectTwo;
150 
151             nComparisonResult = dateOne.compareTo( dateTwo );
152         }
153         else
154         {
155             String strObjectOneRepresentation = String.valueOf( objectOne );
156             String strObjectTwoRepresentation = String.valueOf( objectTwo );
157 
158             nComparisonResult = strObjectOneRepresentation.compareTo( strObjectTwoRepresentation );
159         }
160 
161         return nComparisonResult;
162     }
163 }