CSVMessageDescriptor.java

  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. import org.apache.commons.lang3.StringUtils;

  36. /**
  37.  * Describe an error that occurred during the reading of a CSV file.
  38.  */
  39. public class CSVMessageDescriptor implements Comparable<CSVMessageDescriptor>
  40. {
  41.     private CSVMessageLevel _messageLevel;
  42.     private int _nLineNumber;
  43.     private String _strMessageContent;

  44.     /**
  45.      * Default constructor
  46.      */
  47.     public CSVMessageDescriptor( )
  48.     {
  49.     }

  50.     /**
  51.      * Creates a new <i>CSVMessageDescriptor</i> with every attributes initialized
  52.      *
  53.      * @param messageLevel
  54.      *            The level of the message
  55.      * @param nLineNumber
  56.      *            The number of the line associated with the message
  57.      * @param strMessageContent
  58.      *            The content of the message
  59.      */
  60.     public CSVMessageDescriptor( CSVMessageLevel messageLevel, int nLineNumber, String strMessageContent )
  61.     {
  62.         this._messageLevel = messageLevel;
  63.         this._nLineNumber = nLineNumber;
  64.         this._strMessageContent = strMessageContent;
  65.     }

  66.     /**
  67.      * Get the level of the message
  68.      *
  69.      * @return The level of the message
  70.      */
  71.     public CSVMessageLevel getMessageLevel( )
  72.     {
  73.         return _messageLevel;
  74.     }

  75.     /**
  76.      * Set the level of the message
  77.      *
  78.      * @param messageLevel
  79.      *            the level of the message
  80.      */
  81.     public void setMessageLevel( CSVMessageLevel messageLevel )
  82.     {
  83.         this._messageLevel = messageLevel;
  84.     }

  85.     /**
  86.      * Get the number of the line of the CSV file associated with this message.
  87.      *
  88.      * @return The number of the line of the CSV file associated with this message.
  89.      */
  90.     public int getLineNumber( )
  91.     {
  92.         return _nLineNumber;
  93.     }

  94.     /**
  95.      * Set the number of the line of the CSV file associated with this error.
  96.      *
  97.      * @param nLineNumber
  98.      *            The number of the line of the CSV file associated with this error.
  99.      */
  100.     public void setLineNumber( int nLineNumber )
  101.     {
  102.         this._nLineNumber = nLineNumber;
  103.     }

  104.     /**
  105.      * Get the description of the message
  106.      *
  107.      * @return The description of the message
  108.      */
  109.     public String getMessageContent( )
  110.     {
  111.         return _strMessageContent;
  112.     }

  113.     /**
  114.      * Set the description of the message
  115.      *
  116.      * @param strMessageContent
  117.      *            The description of the message
  118.      */
  119.     public void setMessageContent( String strMessageContent )
  120.     {
  121.         this._strMessageContent = strMessageContent;
  122.     }

  123.     /**
  124.      * compare this CSVMessageDescriptor with another.<br>
  125.      * <b>This method returns 0 for objects that are not equals ! Use with care in collections !</b>
  126.      *
  127.      * @param o
  128.      *            Object to compare to
  129.      * @return Returns :<br>
  130.      *         <ul>
  131.      *         <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
  132.      *         {@link CSVMessageLevel#INFO INFO} level and the other one an {@link CSVMessageLevel#ERROR ERROR} level if they have the same line number.</li>
  133.      *         <li>0 if they both have the same line number and level, regardless of their description</li>
  134.      *         <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
  135.      *         {@link CSVMessageLevel#ERROR ERROR} level whereas the other has the {@link CSVMessageLevel#INFO INFO} level if they have the same line
  136.      *         number.</li>
  137.      *         </ul>
  138.      */
  139.     @Override
  140.     public int compareTo( CSVMessageDescriptor o )
  141.     {
  142.         if ( null == o )
  143.         {
  144.             return 1;
  145.         }

  146.         if ( this.getLineNumber( ) == o.getLineNumber( ) )
  147.         {
  148.             if ( this.getMessageLevel( ) == CSVMessageLevel.ERROR )
  149.             {
  150.                 if ( o.getMessageLevel( ) == CSVMessageLevel.ERROR )
  151.                 {
  152.                     return getMessageContent( ).compareTo( o.getMessageContent( ) );
  153.                 }

  154.                 return 1;
  155.             }

  156.             if ( o.getMessageLevel( ) == CSVMessageLevel.INFO )
  157.             {
  158.                 return getMessageContent( ).compareTo( o.getMessageContent( ) );
  159.             }

  160.             return -1;
  161.         }

  162.         if ( this.getLineNumber( ) > o.getLineNumber( ) )
  163.         {
  164.             return 1;
  165.         }

  166.         return -1;
  167.     }

  168.     /**
  169.      * {@inheritDoc}
  170.      */
  171.     @Override
  172.     public boolean equals( Object o )
  173.     {
  174.         if ( !( o instanceof CSVMessageDescriptor ) )
  175.         {
  176.             return false;
  177.         }

  178.         CSVMessageDescriptor other = (CSVMessageDescriptor) o;

  179.         return ( getLineNumber( ) == other.getLineNumber( ) )
  180.                 && ( ( ( getMessageLevel( ) == null ) && ( other.getMessageLevel( ) == null ) ) || getMessageLevel( ).equals( other.getMessageLevel( ) ) )
  181.                 && StringUtils.equals( getMessageContent( ), other.getMessageContent( ) );
  182.     }

  183.     /**
  184.      * {@inheritDoc}
  185.      */
  186.     @Override
  187.     public int hashCode( )
  188.     {
  189.         return ( this.getLineNumber( ) * 1000 ) + this.getMessageLevel( ).hashCode( );
  190.     }
  191. }