View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.portal.service.csv;
35  
36  import org.apache.commons.lang3.StringUtils;
37  
38  /**
39   * Describe an error that occurred during the reading of a CSV file.
40   */
41  public class CSVMessageDescriptor implements Comparable<CSVMessageDescriptor>
42  {
43      private CSVMessageLevel _messageLevel;
44      private int _nLineNumber;
45      private String _strMessageContent;
46  
47      /**
48       * Default constructor
49       */
50      public CSVMessageDescriptor( )
51      {
52      }
53  
54      /**
55       * Creates a new <i>CSVMessageDescriptor</i> with every attributes initialized
56       * 
57       * @param messageLevel
58       *            The level of the message
59       * @param nLineNumber
60       *            The number of the line associated with the message
61       * @param strMessageContent
62       *            The content of the message
63       */
64      public CSVMessageDescriptor( CSVMessageLevel messageLevel, int nLineNumber, String strMessageContent )
65      {
66          this._messageLevel = messageLevel;
67          this._nLineNumber = nLineNumber;
68          this._strMessageContent = strMessageContent;
69      }
70  
71      /**
72       * Get the level of the message
73       * 
74       * @return The level of the message
75       */
76      public CSVMessageLevel getMessageLevel( )
77      {
78          return _messageLevel;
79      }
80  
81      /**
82       * Set the level of the message
83       * 
84       * @param messageLevel
85       *            the level of the message
86       */
87      public void setMessageLevel( CSVMessageLevel messageLevel )
88      {
89          this._messageLevel = messageLevel;
90      }
91  
92      /**
93       * Get the number of the line of the CSV file associated with this message.
94       * 
95       * @return The number of the line of the CSV file associated with this message.
96       */
97      public int getLineNumber( )
98      {
99          return _nLineNumber;
100     }
101 
102     /**
103      * Set the number of the line of the CSV file associated with this error.
104      * 
105      * @param nLineNumber
106      *            The number of the line of the CSV file associated with this error.
107      */
108     public void setLineNumber( int nLineNumber )
109     {
110         this._nLineNumber = nLineNumber;
111     }
112 
113     /**
114      * Get the description of the message
115      * 
116      * @return The description of the message
117      */
118     public String getMessageContent( )
119     {
120         return _strMessageContent;
121     }
122 
123     /**
124      * Set the description of the message
125      * 
126      * @param strMessageContent
127      *            The description of the message
128      */
129     public void setMessageContent( String strMessageContent )
130     {
131         this._strMessageContent = strMessageContent;
132     }
133 
134     /**
135      * compare this CSVMessageDescriptor with another.<br>
136      * <b>This method returns 0 for objects that are not equals ! Use with care in collections !</b>
137      * 
138      * @param o
139      *            Object to compare to
140      * @return Returns :<br>
141      *         <ul>
142      *         <li>-1 if the line number of this object is greater than the line number of the other object, or if this object has an
143      *         {@link CSVMessageLevel#INFO INFO} level and the other one an {@link CSVMessageLevel#ERROR ERROR} level if they have the same line number.</li>
144      *         <li>0 if they both have the same line number and level, regardless of their description</li>
145      *         <li>1 if the other object is null, if its line number if greater than the line number of the current object, or if this object has a
146      *         {@link CSVMessageLevel#ERROR ERROR} level whereas the other has the {@link CSVMessageLevel#INFO INFO} level if they have the same line
147      *         number.</li>
148      *         </ul>
149      */
150     @Override
151     public int compareTo( CSVMessageDescriptor o )
152     {
153         if ( null == o )
154         {
155             return 1;
156         }
157 
158         if ( this.getLineNumber( ) == o.getLineNumber( ) )
159         {
160             if ( this.getMessageLevel( ) == CSVMessageLevel.ERROR )
161             {
162                 if ( o.getMessageLevel( ) == CSVMessageLevel.ERROR )
163                 {
164                     return getMessageContent( ).compareTo( o.getMessageContent( ) );
165                 }
166 
167                 return 1;
168             }
169 
170             if ( o.getMessageLevel( ) == CSVMessageLevel.INFO )
171             {
172                 return getMessageContent( ).compareTo( o.getMessageContent( ) );
173             }
174 
175             return -1;
176         }
177 
178         if ( this.getLineNumber( ) > o.getLineNumber( ) )
179         {
180             return 1;
181         }
182 
183         return -1;
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public boolean equals( Object o )
191     {
192         if ( !( o instanceof CSVMessageDescriptor ) )
193         {
194             return false;
195         }
196 
197         CSVMessageDescriptor./../fr/paris/lutece/portal/service/csv/CSVMessageDescriptor.html#CSVMessageDescriptor">CSVMessageDescriptor other = (CSVMessageDescriptor) o;
198 
199         return ( getLineNumber( ) == other.getLineNumber( ) )
200                 && ( ( ( getMessageLevel( ) == null ) && ( other.getMessageLevel( ) == null ) ) || getMessageLevel( ).equals( other.getMessageLevel( ) ) )
201                 && StringUtils.equals( getMessageContent( ), other.getMessageContent( ) );
202     }
203 
204     /**
205      * {@inheritDoc}
206      */
207     @Override
208     public int hashCode( )
209     {
210         return ( this.getLineNumber( ) * 1000 ) + this.getMessageLevel( ).hashCode( );
211     }
212 }