Class EffectivelyFinal<A>

  • Type Parameters:
    A - Any type. Class EffectivelyFinal serves as a wrapper for this type.
    All Implemented Interfaces:

    public class EffectivelyFinal<A>
    extends java.lang.Object
    implements java.lang.Cloneable
    Another extremely simple class that can help avoid Effectively Final Java Compiler warnings.

    Lambda-Expression Helper Class

    Java's Function-Pointer syntax (Lambda Exression) can be an invaluable way to create simple methods/functions without having to write the full-blown syntax for a Java Method. Lambda-Expressions, however prohibit variables from changing their value inside the expression This wrapper-class can help alleviate some of the complexities that may crop up from time to time.
    Java's Lambda expressions are somewhat like C Pre-Processor statements. They also do what C-Styled Function Pointers do. However, receiving compiler error's about not using final, or effectively final variables can be a nuisance. The purpose of this class is to allow a user to bypass the Java Compiler's Effectively Final requirement for lambda expressions.

    NOTE: class Counter was also created with the same reasoning behind it.

    This is a "Before & After" example of using this class:

    // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    // This will compile without errors, and work properly:
    // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    EffectivelyFinal<Boolean> hasErrors = new EffectivelyFinal<>(false);
    // someStringStream is an instance of<String> having File Names
    someStringStream.forEach((String fileName) ->
        { if (fileName.contains("Some Incorrect String")) hasErrors.f = true; });
    if (hasErrors.f) throw new IOException("A file name has errors.");
    // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    // This will cause a COMPILE-TIME error:
    // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    boolean hasErrors = false;
    someStringStream.forEach((String fileName) ->
        { if (fileName.contains("Some Incorrect String")) hasErrors = true; });
        // The above line assigns a value to variable 'hasErrors', unfortunately all externally
        // referenced variables in a LAMBDA-EXPRESSION must be declared 'final or effectively-final'
    if (hasErrors) throw new IOException("A file name has errors.");

    Note that the wrapped value is easily accessed as in the example shown below. It is important to remember that Java provides an "Auto-Boxing" and an "Auto-Un-Boxing" feature for all of its Primitive-Wraper Boxed-Types (java.lang.Integer, java.lang.Float, java.lang.Boolean etc...)

    Java Line of Code:
    EffectivelyFinal<Integer> eFinalInt = new EffectivelyFinal<>(0);
    // Auto-Boxing and Auto-Un-Boxing makes this class much more useful.  Note that the wrapped
    // class is the "boxed" Java-Type "Integer" (not a primitive int); however since Java 1.5,
    // the two types are largely interchangeable.  Boxed-Types allow 'null' values, while 
    // Primitive-Types do not.

    • Field Summary

      The Wrapped Instance, Shielded from Compiler Warnings
      Modifier and Type Field
      A f
    • Method Summary

      Methods: class java.lang.Object
      Modifier and Type Method
      boolean equals​(EffectivelyFinal other)
      int hashCode()
      String toString()
      Methods: interface java.lang.Cloneable
      Modifier and Type Method
      EffectivelyFinal<A> clone()
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • f

        public A f
        This variable may be modified inside any Lambda Expression without receiving a compiler error message.
        Exact Field Declaration Expression:
         public A f;
    • Constructor Detail

      • EffectivelyFinal

        🡅  🡇    
        public EffectivelyFinal​(A f)
        Creates an instance of this class, and initializes the field.
        f - Initializes this class' only field, this.f, with the value provided by parameter 'f'
    • Method Detail

      • toString

        🡅  🡇    
        public java.lang.String toString()
        Invokes the standard java.lang.Object method toString() on the field this.f.
        toString in class java.lang.Object
        Returns the String produced by the contents' toString()
        Exact Method Body:
         return f.toString();
      • hashCode

        🡅  🡇    
        public int hashCode()
        Invokes the standard java.lang.Object method hashCode() on the field this.f
        hashCode in class java.lang.Object
        Returns the hash code produced by the contents' hashCode().
        Exact Method Body:
         return f.hashCode();
      • clone

        🡅  🡇    
        public EffectivelyFinal<Aclone()
        This creates a clone of the wrapper class.

        NOTE: The field, itself, is not cloned. Another EffectivelyFinal wrapper is built, using the same internal field. Both the clone and 'this' will share a pointer-reference to the same field.
        clone in class java.lang.Object
        An EffectivelyFinal<A> clone.
        Exact Method Body:
         return new EffectivelyFinal<>(f);
      • equals

        public boolean equals​(EffectivelyFinal other)
        This equality test checks whether both 'this' and 'other' share identical references. Equality between the contained / wrapped field 'f' is not checked.

        If this.f and other.f are, indeed, different references. but whose objects have an 'equals' method which would assert both references were equal, this method would still return FALSE.
        Returns TRUE if and only if this.f == other.f.
        Exact Method Body:
         return this.f == other.f;