ParmType.java

/*
** Module   : ParmType
** Abstract : Parameter types enumerator.
**
** Copyright (c) 2013-2020, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------Description----------------------------------
** 001 EVL 20130304 Created initial version.
** 002 CA  20130313 Added TEXT_VARARGS.
** 003 SVL 20140106 Added fromClass function.
** 004 EVL 20160223 Javadoc fixes to make compatible with Oracle Java 8 for Solaris 10.
** 005 CA  20181039 Added getter for the parameter's type.
** 006 CA  20181221 Removed LARGE_CHAR_OBJECT, as it can't be used.
** 007 SVL 20190614 Support for CLOB and BLOB data types.
** 008 CA  20190717 Added COM-HANDLE and OBJECT type.
** 009 CA  20191213 Added jobject type (FWD extension).
** 010 ECF 20200906 Added missing generic type.
*/
/*
** 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.convert;

import com.goldencode.p2j.persist.*;
import com.goldencode.p2j.ui.*;
import com.goldencode.p2j.util.*;

/**
 * Encodes the possible parameter types in Progress. This includes a code for each
 * individual wrapper type.  But it also includes codes for the common parent types
 * for use in cases where the parameter can be one of multiple related types (e.g.
 * <code>NUMBERTYPE</code> would indicate that an <code>INTEGER</code>,
 * <code>INT64</code> or <code>DECIMAL</code> could all be used as a parameter).
 */ 
public enum ParmType
{
   CHAR(character.class) 
      { @Override public String toString() { return "character"; } },
   LC(longchar.class)
      { @Override public String toString() { return "longchar"; } },
   CLOB(clob.class)
      { @Override public String toString() { return "clob"; } },
   BLOB(blob.class)
      { @Override public String toString() { return "blob"; } },
   TEXT(Text.class)
      { @Override public String toString() { return "Text"; } },
   TEXT_VARARGS(Text[].class)
      { @Override public String toString() { return "Text[]"; } },
   INT(integer.class)
      { @Override public String toString() { return "integer"; } },
   DEC(decimal.class)
      { @Override public String toString() { return "decimal"; } },
   INT64(int64.class)
      { @Override public String toString() { return "int64"; } },
   NUM(NumberType.class)
      { @Override public String toString() { return "NumberType"; } },
   NUM_VARARGS(NumberType[].class)
      { @Override public String toString() { return "NumberType[]"; } },
   LOG(logical.class)
      { @Override public String toString() { return "logical"; } },
   DATE(date.class)
      { @Override public String toString() { return "date"; } },
   DT(datetime.class)
      { @Override public String toString() { return "datetime"; } },
   DTTZ(datetimetz.class)
      { @Override public String toString() { return "datetimetz"; } },
   COMHANDLE(comhandle.class)
      { @Override public String toString() { return "comhandle"; } },
   HANDLE(handle.class)
      { @Override public String toString() { return "handle"; } },
   OBJECT(object.class)
      { @Override public String toString() { return "object"; } },
   JOBJECT(jobject.class)
      { @Override public String toString() { return "jobject"; } },
   WID(GenericWidget.class)
      { @Override public String toString() { return "GenericWidget"; } },
   RECID(recid.class)
      { @Override public String toString() { return "recid"; } },
   RECORD(DataModelObject.class)
      { @Override public String toString() { return "record"; } },
   ROWID(rowid.class)
      { @Override public String toString() { return "rowid"; } },
   ROWID_ARRAY(rowid[].class)
      { @Override public String toString() { return "rowid[]"; } },
   MEM(memptr.class)
      { @Override public String toString() { return "memptr"; } },
   RAW(raw.class)
      { @Override public String toString() { return "raw"; } },
   BIN(BinaryData.class)
      { @Override public String toString() { return "BinaryData"; } },
   BDT(BaseDataType.class)
      { @Override public String toString() { return "BaseDataType"; } },
   BDT_ARRAY(BaseDataType[].class)
      { @Override public String toString() { return "BaseDataType[]"; } },
   BDT_VARARGS(BaseDataType[].class)
      { @Override public String toString() { return "BaseDataType[]"; } };
   
   /** 
    * The P2J wrapper class for this type (may be an array with the component a P2J wrapper type.
    */
   private Class<?> cls;
   
   /**
    * Build a parameter type by specifying its wrapper class. May be an array class.
    * 
    * @param    cls
    *           The parameter's wrapper class (may be an array class).
    */
   private ParmType(Class<?> cls)
   {
      this.cls = cls;
   }
   
   /**
    * Check if the candidate and expected types match.
    * 
    * @param    candidate
    *           The candidate type. May be null.
    * @param    expected
    *           The expected type. May not be null.
    *
    * @return   <code>true</code> if they match (i.e. the expected is a super-class of candidate)
    */
   public static boolean typesMatch(ParmType candidate, ParmType expected)
   {
      if (candidate == null)
      {
         return false;
      }
      
      Class<?> cls1 = candidate.cls;
      Class<?> cls2 = expected.cls;
      // if one of the classes is unknown or they are (or not) both array, then they do not match
      if (cls1 == null || cls2 == null || cls1.isArray() != cls2.isArray())
      {
         return false;
      }
      
      if (cls1.isArray() && cls2.isArray())
      {
         cls1 = cls1.getComponentType();
         cls2 = cls2.getComponentType();
      }
      
      return cls2.isAssignableFrom(cls1);
   }

   /**
    * Resolve the {@link ParmType constant} from the string representation of the given wrapper
    * type.
    * 
    * @param    ptype
    *           The name of the class of a P2J wrapper type.
    *
    * @return   The enum constant for this wrapper type or null if not found.
    */
   public static ParmType fromString(String ptype)
   {
      for (ParmType pt : ParmType.values())
      {
         if (ptype.equals(pt.toString()))
            return pt;
      }
      
      return null;
   }

   /**
    * Resolve the {@link ParmType constant} from the class representing the given wrapper type.
    *
    * @param    ctype
    *           Class representing a P2J wrapper type.
    *
    * @return   The enum constant for this wrapper type or {@code null} if not found.
    */
   public static ParmType fromClass(Class<?> ctype)
   {
      for (ParmType pt : ParmType.values())
      {
         if (ctype.equals(pt.cls))
         {
            return pt;
         }
      }

      return null;
   }

   /**
    * Check if the type is a varargs type.
    * 
    * @return   <code>true</code> if the type is a vararg.
    */
   public boolean isVarargs()
   {
      return this == BDT_VARARGS || this == NUM_VARARGS || this == TEXT_VARARGS;
   }

   /**
    * Get the parameter's {@link #cls type}.
    * 
    * @return   See above.
    */
   public Class<?> getParamType()
   {
      return cls;
   }
}