Class GenericField<E>

java.lang.Object
com.singularsys.extensions.field.AbstractComparativeField
com.singularsys.extensions.field.implementations.GenericField<E>
Type Parameters:
E - Basic type field uses, e.g. Double or Complex.
All Implemented Interfaces:
FieldI, IntegerConvertor, Serializable
Direct Known Subclasses:
BigDecimalField, BigIntegerField, BigModulusField, ExtDocsTest.StringField3, FixedPointField, GenericPowerField, IntegerField, ModulusField

public abstract class GenericField<E> extends AbstractComparativeField implements IntegerConvertor
Abstract-generic base class where the type-variable specifies the arguments and return type. Subclasses must implement E cast(Object l) which converts arguments to the type-E, and also methods E addG(E l, E r) etc. The class provides implementations of Object add(Object l,Object r) which calls cast(Object l) on both arguments and then calls addG, if either argument converts to null then null is returned. It has null implementations for the logical operators.

An examples for a Field using Strings:

public class StringField3 extends GenericField<String> {
    private static final long serialVersionUID = 330L;

        @Override
        public String cast(Object l) throws EvaluationException {
                if(l instanceof String) 
                        return ((String) l);
                return null;
        }

        // No need for casting in individual operations
        @Override
        public String addG(String l, String r) throws EvaluationException {
                return l + r;
        }

        // A single method for all comparison operations
        @Override
        public Integer cmpG(String l, String r) throws EvaluationException {
                return l.compareTo(r);
        }

    // other methods just return null
        @Override
        public String subG(String l, String r) { return null; }
        @Override
        public String negG(String l) { return null; }
        @Override
        public String mulG(String l, String r) { return null; }
        @Override
        public String divG(String l, String r) { return null; }
        @Override
        public String modG(String l, String r) { return null; }
        @Override
        public String powG(String l, String r) { return null; }

        // no need to implement comparison operations le(l,r) etc.
        // or logical operations and(l,r), or(l,r), not(l) 
}
Author:
Richard Morris
See Also:
  • Constructor Details

    • GenericField

      public GenericField()
  • Method Details

    • getOne

      public abstract E getOne()
      Description copied from interface: FieldI
      Get the multiplicative identity for this field.
      Specified by:
      getOne in interface FieldI
      Returns:
      object representing one or null if undefined for this field
    • getZero

      public abstract E getZero()
      Description copied from interface: FieldI
      Get the additive identity for this field
      Specified by:
      getZero in interface FieldI
      Returns:
      object representing zero or null if undefined for this field
    • cast

      public abstract E cast(Object l) throws EvaluationException
      Convert the input to type E.
      Parameters:
      l -
      Returns:
      l cast to type E if possible or null if casting is not possible
      Throws:
      EvaluationException
    • addG

      public abstract E addG(E l, E r) throws EvaluationException
      Adds two elements of type E.
      Parameters:
      l - lhs argument
      r - rhs argument
      Returns:
      the sum
      Throws:
      EvaluationException - on error
    • subG

      public abstract E subG(E l, E r) throws EvaluationException
      Subtracts two elements of type E.
      Parameters:
      l - lhs argument
      r - rhs argument
      Returns:
      the difference
      Throws:
      EvaluationException - on error
    • negG

      public abstract E negG(E l) throws EvaluationException
      Negates an elements of type E.
      Parameters:
      l - the argument
      Returns:
      the negation
      Throws:
      EvaluationException - on error
    • mulG

      public abstract E mulG(E l, E r) throws EvaluationException
      Multiplies two elements of type E.
      Parameters:
      l - lhs argument
      r - rhs argument
      Returns:
      the product
      Throws:
      EvaluationException - on error
    • divG

      public abstract E divG(E l, E r) throws EvaluationException
      Divides two elements of type E.
      Parameters:
      l - numerator
      r - denominator
      Returns:
      the division
      Throws:
      EvaluationException - on error
    • modG

      public abstract E modG(E l, E r) throws EvaluationException
      Modulus of two elements of type E.
      Parameters:
      l - lhs argument
      r - rhs argument
      Returns:
      the modulus
      Throws:
      EvaluationException - on error
    • powG

      public abstract E powG(E l, E r) throws EvaluationException
      Raises l to the power of r
      Parameters:
      l - lhs argument
      r - rhs argument
      Returns:
      the power
      Throws:
      EvaluationException - on error
    • cmpG

      public abstract Integer cmpG(E l, E r) throws EvaluationException
      Compare two objects of the same type.
      Parameters:
      l - left argument
      r - right argument
      Returns:
      -1, 0, 1 or null
      Throws:
      EvaluationException
    • add

      public Object add(Object l, Object r) throws EvaluationException
      First calls cast(Object) on both arguments then call the addG(E, E). If the result of either cast are null returns null.
      Specified by:
      add in interface FieldI
      Parameters:
      l - left-hand argument
      r - right-hand argument
      Returns:
      an object representing the result or null if it can not be evaluated.
      Throws:
      EvaluationException - on error
    • sub

      public Object sub(Object l, Object r) throws EvaluationException
      Description copied from interface: FieldI
      Subtract two members of the field.
      Specified by:
      sub in interface FieldI
      Parameters:
      l - left-hand argument
      r - right-hand argument
      Returns:
      an object representing the result or null if it can not be evaluated.
      Throws:
      EvaluationException - on error
    • neg

      public Object neg(Object l) throws EvaluationException
      Description copied from interface: FieldI
      The negation -x of an element.
      Specified by:
      neg in interface FieldI
      Parameters:
      l - the argument
      Returns:
      an object representing the result or null if it can not be evaluated.
      Throws:
      EvaluationException - on error
    • mul

      public Object mul(Object l, Object r) throws EvaluationException
      Description copied from interface: FieldI
      Multiplies two members of the field.
      Specified by:
      mul in interface FieldI
      Parameters:
      l - left-hand argument
      r - right-hand argument
      Returns:
      an object representing the result or null if it can not be evaluated.
      Throws:
      EvaluationException - on error
    • div

      public Object div(Object l, Object r) throws EvaluationException
      Description copied from interface: FieldI
      Divides two members of the field.
      Specified by:
      div in interface FieldI
      Parameters:
      l - left-hand argument
      r - right-hand argument
      Returns:
      an object representing the result or null if it can not be evaluated.
      Throws:
      EvaluationException - on error
    • mod

      public Object mod(Object l, Object r) throws EvaluationException
      Description copied from interface: FieldI
      The modulus of two members of the field.
      Specified by:
      mod in interface FieldI
      Parameters:
      l - left-hand argument
      r - right-hand argument
      Returns:
      an object representing the result or null if it can not be evaluated.
      Throws:
      EvaluationException - on error
    • pow

      public Object pow(Object l, Object r) throws EvaluationException
      Description copied from interface: FieldI
      The power operator.
      Specified by:
      pow in interface FieldI
      Parameters:
      l - left-hand argument
      r - right-hand argument
      Returns:
      an object representing the result or null if it can not be evaluated.
      Throws:
      EvaluationException - on error
    • cmp

      public Integer cmp(Object l, Object r) throws EvaluationException
      Description copied from class: AbstractComparativeField
      Compare two elements, return -1,0,1 if the comparison can be carried out, null otherwise.
      Specified by:
      cmp in class AbstractComparativeField
      Parameters:
      l -
      r -
      Returns:
      -1, 0, 1 if l < r, l==r or l > r respectively, or null if comparison cannot be carried out or
      Throws:
      EvaluationException
    • and

      public Boolean and(Object l, Object r)
      Default implementation returns null.
      Specified by:
      and in interface FieldI
      Parameters:
      l - left-hand argument
      r - right-hand argument
      Returns:
      an object representing the result or null if it can not be evaluated.
    • or

      public Boolean or(Object l, Object r)
      Default implementation returns null.
      Specified by:
      or in interface FieldI
      Parameters:
      l - left-hand argument
      r - right-hand argument
      Returns:
      an object representing the result or null if it can not be evaluated.
    • not

      public Boolean not(Object l)
      Default implementation returns null.
      Specified by:
      not in interface FieldI
      Parameters:
      l - argument
      Returns:
      an object representing the result or null if it can not be evaluated.
    • convertToIntE

      public abstract Integer convertToIntE(E l) throws EvaluationException
      If possible convert the argument to an Integer to enable integral powers. If the argument cannot be converted null should be returned and the powG(E,E) method will be called.
      Parameters:
      l - argument to convert.
      Returns:
      an integer or null if argument cannot be converted
      Throws:
      EvaluationException
    • convertToInt

      public Integer convertToInt(Object l) throws EvaluationException
      Description copied from interface: IntegerConvertor
      Attempt to convert argument to an integer
      Specified by:
      convertToInt in interface IntegerConvertor
      Parameters:
      l - value to convert
      Returns:
      corresponding integer or null if it cannot be converted
      Throws:
      EvaluationException - on error
    • convertFromInt

      public abstract E convertFromInt(Integer l) throws EvaluationException
      Description copied from interface: IntegerConvertor
      Attempt to convert argument from an integer
      Specified by:
      convertFromInt in interface IntegerConvertor
      Parameters:
      l - value to convert
      Returns:
      corresponding value in this field or null if it cannot be converted
      Throws:
      EvaluationException - probably never