Class AutoEquals<T extends HasFields<T> & HasMethods<T> & HasName<T>>

  • Type Parameters:
    T - The extending type
    All Implemented Interfaces:
    Consumer<T>

    public final class AutoEquals<T extends HasFields<T> & HasMethods<T> & HasName<T>>
    extends Object
    implements Consumer<T>
    This control can be applied on a class, enum or similar to auto generate an equals and a hashCode method. The control uses all the fields to determine the salt.

    The control must be instantiated with the File as a parameter. The reason for this is that the generated methods might require new dependencies to be imported.

    To use AutoEquals, follow this example:

         file.add(
             Class.of("Vector2f")
                 .add(Field.of("x", FLOAT_PRIMITIVE)
                     .public_()
                     .set(new NumberValue(0)))
     
                 .add(Field.of("y", FLOAT_PRIMITIVE)
                     .public_()
                     .set(new NumberValue(0)))
     
                 .call(new AutoEquals<>(file))
         );
     

    If one of the methods already exists, it will not be overwritten.

    Author:
    Emil Forslund
    • Constructor Detail

      • AutoEquals

        public AutoEquals​(HasImports<?> importer)
        Instantiates the AutoEquals using something that imports can be added to. This can for an example be a File.
        Parameters:
        importer - the importer
    • Method Detail

      • accept

        public void accept​(T model)
        Adds an equals() and a hashCode() method to the specified model.

        If one of the methods already exists, it will not be overwritten.

        Specified by:
        accept in interface Consumer<T extends HasFields<T> & HasMethods<T> & HasName<T>>
        Parameters:
        model - the model
      • acceptEquals

        protected void acceptEquals​(T model)
        The equals()-part of the accept method.
        Parameters:
        model - the model
      • acceptHashcode

        protected void acceptHashcode​(T model)
        The hashCode()-part of the accept method.
        Parameters:
        model - the model
      • compare

        protected String compare​(Field f)
        Generates code for comparing the specified field in this and another object.
        Parameters:
        f - the field
        Returns:
        the comparing code
      • hash

        protected String hash​(Field f)
        Generates code for hashing the specified field.
        Parameters:
        f - the field
        Returns:
        the hashing code
      • isPrimitive

        protected boolean isPrimitive​(Type type)
        Returns true if the specified type is a primitive type.
        Parameters:
        type - the type
        Returns:
        true if primitive, else false
      • hasMethod

        protected boolean hasMethod​(T model,
                                    String method,
                                    int params)
        Returns if a method with the specified signature exists.
        Parameters:
        model - the model
        method - the method name to look for
        params - the number of parameters in the signature
        Returns:
        true if found, else false