Class AbstractDatatypeHelper

java.lang.Object
org.faktorips.codegen.dthelpers.AbstractDatatypeHelper
All Implemented Interfaces:
BaseDatatypeHelper<JavaCodeFragment>, DatatypeHelper
Direct Known Subclasses:
AbstractPrimitiveDatatypeHelper, AbstractTimeHelper, ArrayOfValueDatatypeHelper, BigDecimalHelper, BooleanHelper, CurrencyHelper, DateHelper, DecimalHelper, DoubleHelper, GenericValueDatatypeHelper, GregorianCalendarAsDateHelper, IntegerHelper, InternationalStringDatatypeHelper, ListOfValueDatatypeHelper, LongHelper, MoneyHelper, StringHelper

public abstract class AbstractDatatypeHelper extends Object implements DatatypeHelper
Abstract base class for datatype helpers.
Author:
Jan Ortmann
  • Constructor Details

    • AbstractDatatypeHelper

      public AbstractDatatypeHelper()
      Constructs a new helper without initializing the datatype.
    • AbstractDatatypeHelper

      public AbstractDatatypeHelper(Datatype datatype)
      Constructs a new helper for the given datatype.
  • Method Details

    • getDatatype

      public Datatype getDatatype()
      Description copied from interface: DatatypeHelper
      Returns the datatype this is a helper for.
      Specified by:
      getDatatype in interface BaseDatatypeHelper<JavaCodeFragment>
      Specified by:
      getDatatype in interface DatatypeHelper
    • setDatatype

      public void setDatatype(Datatype datatype)
      Description copied from interface: DatatypeHelper
      Sets the datatype this is a helper for. Introduced to enable setter based dependency injection, needed for example for Eclipse's extension point mechanism.
      Specified by:
      setDatatype in interface BaseDatatypeHelper<JavaCodeFragment>
      Specified by:
      setDatatype in interface DatatypeHelper
    • valueOfExpression

      protected abstract JavaCodeFragment valueOfExpression(String expression)
      This method is supposed to be overridden by subclasses.

      It is used within the newInstanceFromExpression(String) method. It returns a JavaCodeFragment with sourcecode that creates an instance of the datatype's Java class with the given expression.

      If the expression is null the fragment's sourcecode is either the String "null" or the sourcecode to get an instance of the appropriate null object. Preconditions: Expression may not be null or empty. When evaluated the expression must return a String.

    • newInstanceFromExpression

      public JavaCodeFragment newInstanceFromExpression(String expression)
      Description copied from interface: DatatypeHelper
      Returns a JavaCodeFragment with sourcecode that creates an instance of the datatype's Java class with the given expression. If the expression is null the fragment's sourcecode is either the String "null" or the sourcecode to get an instance of the appropriate null object. When evaluated the expression must return a string.
      Specified by:
      newInstanceFromExpression in interface BaseDatatypeHelper<JavaCodeFragment>
      Specified by:
      newInstanceFromExpression in interface DatatypeHelper
      Parameters:
      expression - A Java source code expression that yields a String. Examples are a constant String like "FOO", a variable like foo or a method call like getÍd().
    • newInstanceFromExpression

      public JavaCodeFragment newInstanceFromExpression(String expression, boolean checkForNull)
      Description copied from interface: DatatypeHelper
      Returns a JavaCodeFragment with sourcecode that creates an instance of the datatype's Java class with the given expression. If the expression is null the fragment's sourcecode is either the String "null" or the sourcecode to get an instance of the appropriate null object. When evaluated the expression must return a string
      Specified by:
      newInstanceFromExpression in interface BaseDatatypeHelper<JavaCodeFragment>
      Specified by:
      newInstanceFromExpression in interface DatatypeHelper
      Parameters:
      expression - A Java source code expression that yields a String. Examples are a constant String like "FOO", a variable like foo or a method call like getÍd().
      checkForNull - true if this helper has to assume that the given expression can yield null or the empty string. Can be used to generate simpler code, if the null check is not necessary.
    • getRangeJavaClassName

      public String getRangeJavaClassName(boolean useTypesafeCollections)
      Description copied from interface: DatatypeHelper
      Returns the qualified Java class name of the range class of the datatype this is a helper for.
      Specified by:
      getRangeJavaClassName in interface DatatypeHelper
    • newRangeInstance

      public JavaCodeFragment newRangeInstance(JavaCodeFragment lowerBoundExp, JavaCodeFragment upperBoundExp, JavaCodeFragment stepExp, JavaCodeFragment containsNullExp, boolean useTypesafeCollections)
      Description copied from interface: DatatypeHelper
      Returns a JavaCodeFragment containing the source code to create a new instance of a type specific range.
      Specified by:
      newRangeInstance in interface DatatypeHelper
      Parameters:
      lowerBoundExp - the lower bound expression of the range. Can be null to indicate that the lower bound is open
      upperBoundExp - the upper bound expression of the range. Can be null to indicate that the upper bound is open.
      stepExp - the minimum increment expression for values within the lower and upper bounds. Can be null to indicate that this is a continuous range.
      containsNullExp - the containsNull expression
      Returns:
      the code fragment to create a new range instance. Can be null to indicate that a range is not supported.
    • newEnumValueSetInstance

      public JavaCodeFragment newEnumValueSetInstance(String[] values, boolean containsNull, boolean useTypesafeCollections)
      Returns a JavaCodeFragment containing the source code to create a new enum value set instance. Code sample
        (IEnumValueSet)new DefaultEnumValueSet<>(
            true,
            GeneratedGender.getGeneratedGender(null),
            GeneratedGender.getGeneratedGender(Integer.valueOf(1)),
            GeneratedGender.getGeneratedGender(Integer.valueOf(2)));
       
      Specified by:
      newEnumValueSetInstance in interface DatatypeHelper
      Parameters:
      values - the values contained in the enum value set code fragment returned by this method
      containsNull - indicates if null is also contained in the returned enum value set
      useTypesafeCollections - indicates if Java 5 typesafe collections and valuetypes shall be used
    • newEnumValueSetInstance

      public JavaCodeFragment newEnumValueSetInstance(JavaCodeFragment valueCollection, JavaCodeFragment containsNullExpression, boolean useTypesafeCollections)
      Description copied from interface: DatatypeHelper
      Returns a JavaCodeFragment containing the source code to create a new enum value set instance.
      Specified by:
      newEnumValueSetInstance in interface DatatypeHelper
      Parameters:
      valueCollection - a JavaCodeFragment is expected that contains an expression of the type java.util.Collection The collection has to contain instances of the datatype of this helper.
      containsNullExpression - a JavaCodeFragment is expected that contains an expression of the type boolean
      useTypesafeCollections - indicates if Java 5 typesafe collections and valuetypes shall be used
    • referenceOrSafeCopyIfNeccessary

      public JavaCodeFragment referenceOrSafeCopyIfNeccessary(String expression)
      Description copied from interface: DatatypeHelper
      If this is a helper for a mutable data type (like GregorianCalendar for example) this method return the code fragment that creates a copy of the given expression. For immutable data types this method returns the expression unmodified.
      Specified by:
      referenceOrSafeCopyIfNeccessary in interface DatatypeHelper
      Parameters:
      expression - An expression (as Java sourcecode)
      Returns:
      s. above
    • newSafeCopy

      protected JavaCodeFragment newSafeCopy(String expression)
      Helpers for immutable datatypes must override this method to create a copy of the value given in the expression.
      Parameters:
      expression - The expression of which you want to get the new safe copy code fragment from
    • getToStringExpression

      public JavaCodeFragment getToStringExpression(String fieldName)
      Description copied from interface: DatatypeHelper
      Returns a JavaCodeFragment containing the code for converting the value (of the given field) to a string representation with respect to its data type. The String must be built so that it can be read using the valueOf-Expression. If the value is null, the toString-code will yield null as a result.

      The default implementation will call the values toString() method or return null . The default implementation for generic (extensible) data types will call the method defined using setToStringMethodName(). Custom DatatypeHelpers may override.

      Specified by:
      getToStringExpression in interface BaseDatatypeHelper<JavaCodeFragment>
      Specified by:
      getToStringExpression in interface DatatypeHelper
      Parameters:
      fieldName - the name of the field in the generated class that should be converted to a string
      Returns:
      a JavaCodeFragment containing the toString() code.
    • nullExpression

      public JavaCodeFragment nullExpression()
      Returns "null". Returns a JavaCodeFragment with sourcecode that is either the String "null" or the sourcecode to get an instance of the appropriate null object.
      Specified by:
      nullExpression in interface BaseDatatypeHelper<JavaCodeFragment>
      Specified by:
      nullExpression in interface DatatypeHelper
    • createCastExpression

      public JavaCodeFragment createCastExpression(String bound)
      Description copied from interface: DatatypeHelper
      Returns a JavaCodeFragment with a new value instance, adding a cast to the value's data type in case the value is empty to avoid problems with generics.
      Specified by:
      createCastExpression in interface DatatypeHelper
      Parameters:
      bound - the String representation of a value of this datatype
      Returns:
      a JavaCodeFragment containing the code to create (and cast) the value