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