TaggedName.java

/*
** Module   : TaggedName.java
** Abstract : Container for a pair of {object name, object tag}
**
** Copyright (c) 2009-2020, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- --------------------------Description--------------------------------
** 001 NVS 20090428   @42034 Created initial version. 
** 002 CA  20090507   @42118 Added toString() method. Added NPE and type 
**                           protection for the equals() method.
** 003 CA  20090417   @43154 Added getValueAt(int) method.
** 004 SIY 20091116   @44342 Refactored to make further extensions simpler.
** 005 SIY 20091204   @44481 Added constructor for making instances with
**                           capacity greater than provided array.
** 006 SVL 20100211   @44574 Added prettyPrintNoUnknows() function.
** 007 OM  20130420          Added support for prettyPrint()-ing int64 values.
** 008 HC  20170612          Changes related to implementation of new GWT-based Admin client.
** 009 CA  20200122          Javadoc fixes.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.admin;

import java.io.Serializable;

import com.goldencode.p2j.util.*;

/**
 * Provides storage for a list of object names with their respective tags.
 * <p>
 * See {@linkplain TaggedNameHelper} for constructing instances of this class from
 * {@link BaseDataType} types. This split is needed due to the limitations of GWT serializer.
 */
public class TaggedName
implements Serializable, 
           Comparable<TaggedName>
{
   /** Defines the index of the NAME field. */
   protected static final int IDX_NAME = 0;
   
   /** Defines the index of the TAG field. */
   protected static final int IDX_TAG  = 1;
   
   /** Storage for data. */
   private String[] data;

   /** Default constructor */
   public TaggedName()
   {
   }

   /**
    * Basic constructor.
    * 
    * @param    length
    *           Length of internal array.
    * @param    array
    *           Initial values.
    */
   public TaggedName(int length, String... array)
   {
      int sz = (array == null) ? 0 : array.length;
      
      data = new String[Math.max(length, sz)];
      
      if (sz > 0)
         System.arraycopy(array, 0, data, 0, sz);
   }

   /**
    * Constructor from arrays.
    * 
    * @param    array
    *           The data for this row.
    */
   public TaggedName(String... array)
   {
      this(array.length, array);
   }
   
   /**
    * Constructor.
    * 
    * @param    name
    *           The object's name.
    * @param    tag
    *           The object's tag.
    */
   public TaggedName(String name, String tag)
   {
      this(new String[] {name, tag});
   }

   /**
    * Compares this instance of the class with another. Comparison is
    * case-insensitive.
    * 
    * @param    o
    *           Object to compare with.
    * 
    * @return   the usual integers required by Comparable
    */
   public int compareTo(TaggedName o)
   {
      //noinspection NonJREEmulationClassesInClientCode
      return getName().compareToIgnoreCase(o.getName());
   }
   
   /**
    * Compares this instance of the class with another. Comparison is
    * case-insensitive.
    * 
    * @param    o
    *           Object to compare with.
    * 
    * @return   <code>true</code> when both are equal
    */
   public boolean equals(Object o)
   {
      return o != null                 &&
             (o instanceof TaggedName) && 
             getName().equalsIgnoreCase(((TaggedName) o).getName());
   }
   
   /**
    * Calculates the hash code for instances of this class. Hash codes are
    * calculated off the lower case version of the name field.
    * 
    * @return   calculated integer hash code
    */
   public int hashCode()
   {
      return getName().toLowerCase().hashCode();
   }
   
   /**
    * Get a string representation of this object (name of the object).
    * 
    * @return   the object name.
    */
   public String toString()
   {
      return getName();
   }

   /**
    * Get the value for the specified column.
    * 
    * @param    col
    *           Column of interest.
    * 
    * @return   the value for the specified column.
    */
   public Object getValueAt(int col)
   {
      return get(col);
   }

   /**
    * Convenience method for accessing the name.
    * 
    * @param    name
    *           The name to set.
    */
   public void setName(String name)
   {
      data[IDX_NAME] = name;
   }

   /**
    * Convenience method for accessing the name.
    * 
    * @return   the name
    */
   public String getName()
   {
      return data[IDX_NAME];
   }

   /**
    * Convenience method for accessing the tag.
    * 
    * @param    tag
    *           The tag to set.
    */
   public void setTag(String tag)
   {
      data[IDX_TAG] = tag;
   }

   /**
    * Convenience method for accessing the tag.
    * 
    * @return   the tag
    */
   public String getTag()
   {
      return data[IDX_TAG];
   }
   
   /**
    * Set value at specified index.
    * 
    * @param    idx
    *           Value index.
    * @param    value
    *           Value to store.
    */
   public void set(int idx, String value)
   {
      if (idx >= 0 && idx < data.length)
         data[idx] = value;
   }

   /**
    * Get value at specified index.
    * 
    * @param    idx
    *           Value index.
    *           
    * @return   value stored at specified index.
    */
   public String get(int idx)
   {
      if (idx >= 0 && idx < data.length)
         return data[idx];
      
      return null;
   }

   /**
    * Get the length of internal vector.
    * 
    * @return   length of data vector.
    */
   public int length()
   {
      return data.length;
   }

   /**
    * Returns the containing data.
    *
    * @return   See above.
    */
   public String[] getData()
   {
      return data;
   }
}