Class GenericField<E>

  • Type Parameters:
    E - Basic type field uses, e.g. Double or Complex.
    All Implemented Interfaces:
    FieldI, IntegerConvertor, java.io.Serializable
    Direct Known Subclasses:
    BigDecimalField, BigIntegerField, BigModulusField, ExtDocsTest.StringField3, 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. Sub classes 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:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      GenericField()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object add​(java.lang.Object l, java.lang.Object r)
      First calls cast(Object) on both arguments then call the addG(E, E).
      abstract E addG​(E l, E r)
      Adds two elements of type E.
      java.lang.Boolean and​(java.lang.Object l, java.lang.Object r)
      Default implementation returns null.
      abstract E cast​(java.lang.Object l)
      Convert the input to type E.
      java.lang.Integer cmp​(java.lang.Object l, java.lang.Object r)
      Compare two elements, return -1,0,1 if the comparison can be carried out, null otherwise.
      abstract java.lang.Integer cmpG​(E l, E r)
      Compare two objects of the same type.
      abstract E convertFromInt​(java.lang.Integer l)
      Attempt to convert argument from an integer
      java.lang.Integer convertToInt​(java.lang.Object l)
      Attempt to convert argument to an integer
      abstract java.lang.Integer convertToIntE​(E l)
      If possible convert the argument to an Integer to enable integral powers.
      java.lang.Object div​(java.lang.Object l, java.lang.Object r)
      Divides two members of the field.
      abstract E divG​(E l, E r)
      Divides two elements of type E.
      abstract E getOne()
      Get the multiplicative identity for this field.
      abstract E getZero()
      Get the additive identity for this field
      java.lang.Object mod​(java.lang.Object l, java.lang.Object r)
      The modulus of two members of the field.
      abstract E modG​(E l, E r)
      Modulus of two elements of type E.
      java.lang.Object mul​(java.lang.Object l, java.lang.Object r)
      Multiplies two members of the field.
      abstract E mulG​(E l, E r)
      Multiplies two elements of type E.
      java.lang.Object neg​(java.lang.Object l)
      The negation -x of an element.
      abstract E negG​(E l)
      Negates an elements of type E.
      java.lang.Boolean not​(java.lang.Object l)
      Default implementation returns null.
      java.lang.Boolean or​(java.lang.Object l, java.lang.Object r)
      Default implementation returns null.
      java.lang.Object pow​(java.lang.Object l, java.lang.Object r)
      The power operator.
      abstract E powG​(E l, E r)
      Raises l to the power of r
      java.lang.Object sub​(java.lang.Object l, java.lang.Object r)
      Subtract two members of the field.
      abstract E subG​(E l, E r)
      Subtracts two elements of type E.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GenericField

        public GenericField()
    • Method Detail

      • 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​(java.lang.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
      • 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
      • 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
      • cmpG

        public abstract java.lang.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 java.lang.Object add​(java.lang.Object l,
                                    java.lang.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 java.lang.Object sub​(java.lang.Object l,
                                    java.lang.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 java.lang.Object neg​(java.lang.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 java.lang.Object mul​(java.lang.Object l,
                                    java.lang.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 java.lang.Object div​(java.lang.Object l,
                                    java.lang.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 java.lang.Object mod​(java.lang.Object l,
                                    java.lang.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 java.lang.Object pow​(java.lang.Object l,
                                    java.lang.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
      • and

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

        public java.lang.Boolean or​(java.lang.Object l,
                                    java.lang.Object r)
                             throws EvaluationException
        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.
        Throws:
        EvaluationException - on error
      • not

        public java.lang.Boolean not​(java.lang.Object l)
                              throws EvaluationException
        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.
        Throws:
        EvaluationException - on error
      • convertToIntE

        public abstract java.lang.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