recid.java

/*
** Module   : recid.java
** Abstract : Progress 4GL compatible record ID
**
** Copyright (c) 2005-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- --------------------------Description-------------------------------
** 001 SVL 20090225   @41371 Created initial version. Progress 4GL compatible
**                           record ID data type.
** 002 ECF 20090521   @42419 Added duplicate() method.
** 003 CA  20130121          Added missing c'tor.
**                           Added c'tor which builds a new instance from a
**                           BaseDataType value, if possible. This should be
**                           used only when constructing an instance using the
**                           value returned by a DYNAMIC-FUNCTION call; 4GL
**                           does some special casting in this case, which is
**                           not implemented yet.
** 004 GES 20130322          Fixed the BDT c'tor.
** 005 CA  20131001          Eliminated custom group/dec separators when format is used. In this
**                           case, they must default to comma (,) for group and dot (.) for
**                           decimal separator.
** 006 OM  20150220          Added missing instantiateUnknown() method.
** 007 ECF 20160113          Added missing instantiateDefault() method.
** 008 CA  20151221          Added missing instantiateDefaultExtent().
** 009 GES 20171207          Removed forced version of assign().
** 010 IAS 20201007          Added Type enum
** 011 CA  20230215          'duplicate()' method returns the real type instead of BDT.
*/
/*
** 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.util;

import com.goldencode.p2j.util.BaseDataType.*;

/**
 * A class that represents a Progress 4GL compatible record ID. It is the same
 * as {@link integer} except default format.
 */
public class recid
extends integer
{
   /** Default format string for this data type. */
   private static String defFmt = ">>>>>>9";
   
   /**
    * Default constructor which creates an instance representing the <code>unknown value</code>.
    */
   public recid()
   {
      super();
   }
   
   /**
    * This is a special c'tor which should be used only when converting the
    * value returned by a function or method with polymorphic return type into the
    * expected type (i.e. DYNAMIC-FUNCTION()).  In such cases, the 4GL does some
    * automatic type conversion (see {@link #assign(BaseDataType)}).
    * 
    * @param    value
    *           The value to be used for this instance.
    */
   public recid(BaseDataType value)
   {
      if (value == null || value.isUnknown())
      {
         setUnknown();
      }
      else
      {
         assign(value);
      }
   }
   
   /**
    * Constructs an instance after casting the parameter into the internal
    * representation of this class.  If the value is larger than
    * <code>Integer.MAX_VALUE</code> or smaller than
    * <code>Integer.MIN_VALUE</code> then some loss of magnitude will
    * occur (the resulting value will be set to the maximum or minimum
    * value respectively).
    * <p>
    * It is not possible to yield an <code>unknown value</code> from this
    * constructor without calling the method {@link #setUnknown} separate.
    *
    * @param   value
    *          The value to be used for this instance.
    */
   public recid(long value)
   {
      super(value);
   }
   
   /**
    * Constructs an instance after casting the parameter into the internal
    * representation of this class.  If the value is larger than
    * <code>Integer.MAX_VALUE</code> or smaller than
    * <code>Integer.MIN_VALUE</code> then some loss of magnitude will
    * occur (the resulting value will be set to the maximum or minimum
    * value respectively).
    * <p>
    * If the parameter's represents the <code>unknown value</code>, this
    * instance will also represent the <code>unknown value</code>.
    *
    * @param   value
    *          The value to be used for this instance.
    */
   public recid(integer value)
   {
      super(value);
   }
   
   /**
    * Constructs an instance after copying the parameter's data into the
    * internal representation of this class.  No loss of precision will occur.
    * <p>
    * If the parameter's represents the <code>unknown value</code>, this
    * instance will also represent the <code>unknown value</code>.
    *
    * @param   value
    *          The value to be used for this instance.
    */
   public recid(recid value)
   {
      super(value);
   }
   
   /**
    * Constructs an instance after converting the string representation
    * of a number into an integer.  Due to rounding, a loss of precision
    * may occur in this conversion (all digits to the right of the decimal
    * point will be truncated).  In addition, if the number is larger than
    * can be stored in a Java <code>int</code>, loss of magnitude will
    * occur.
    *
    * @param   value
    *          The value to be used for this instance.
    */
   public recid(String value)
   {
      super(value);
   }
   
   /**
    * Get the type
    * @return type
    */
   public Type getType()
   {
      return Type.RECID;
   }

   /**
    * Does the same as standard <code>clone()</code> method but returns an instance of
    * <code>BaseDataType</code> and doesn't throw the <code>CloneNotSupportedException</code>.
    *
    * @return   A clone of this instance.
    */
   @Override
   public recid duplicate()
   {
      return new recid(this);
   }
   
   /**
    * Returns the default format string for this given data type.
    *
    * @return   The default format string for this data.
    */
   @Override
   public String obtainDefaultFormat()
   {
      return defFmt;
   }
   
   /**
    * Dynamically builds the default format string for this given data type.
    *
    * @return   The default format string for this data type.
    */
   @Override
   public String buildDefaultFormat()
   {
      return defFmt;
   }
   
   /**
    * Creates a new instance of {@code recid} type that represents the <code>unknown value</code>.
    *
    * @return  A {@code recid} instance that represents the <code>unknown value</code>.
    */
   @Override
   public BaseDataType instantiateUnknown()
   {
      return new recid();
   }
   
   /**
    * Creates a new instance of the same type that represents the  default initialized value.
    *
    * @return  An instance that represents the default value.
    */
   @Override
   public BaseDataType instantiateDefault()
   {
      return new recid();
   }
   
   /**
    * Get the default initialization for an extent variable of this type.
    * 
    * @return   See above.
    */
   @Override
   protected BaseDataType instantiateDefaultExtent()
   {
      return instantiateDefault();
   }
}