Package Torello.Java.Additional
Class EffectivelyFinal<A>
- java.lang.Object
-
- Torello.Java.Additional.EffectivelyFinal<A>
-
- Type Parameters:
A
- Any type. ClassEffectivelyFinal
serves as a wrapper for this type.
- All Implemented Interfaces:
java.lang.Cloneable
public class EffectivelyFinal<A> extends java.lang.Object implements java.lang.Cloneable
Another extremely simple class that can help avoidEffectively 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 usingfinal, 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: classCounter
was also created with the same reasoning behind it.
This is a "Before & After" example of using this class:
Example:
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // This will compile without errors, and work properly: // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** EffectivelyFinal<Boolean> hasErrors = new EffectivelyFinal<>(false); // someStringStream is an instance of java.util.stream.Stream<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); eFinalInt.f++; // 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.
Hi-Lited Source-Code:- View Here: Torello/Java/Additional/EffectivelyFinal.java
- Open New Browser-Tab: Torello/Java/Additional/EffectivelyFinal.java
File Size: 2,987 Bytes Line Count: 87 '\n' Characters Found
-
-
Constructor Summary
Constructors Constructor Description EffectivelyFinal()
Creates an instance of this class, and initializes the fieldthis.f
to null.EffectivelyFinal(A f)
Creates an instance of this class, and initializes the field.
-
-
-
Constructor Detail
-
EffectivelyFinal
public EffectivelyFinal(A f)
Creates an instance of this class, and initializes the field.- Parameters:
f
- Initializes this class' only field,this.f
, with the value provided by parameter'f'
-
EffectivelyFinal
public EffectivelyFinal()
Creates an instance of this class, and initializes the fieldthis.f
to null.
-
-
Method Detail
-
toString
public java.lang.String toString()
Invokes the standardjava.lang.Object
methodtoString()
on the fieldthis.f
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- Returns the
String
produced by the contents'toString()
- Code:
- Exact Method Body:
return f.toString();
-
hashCode
public int hashCode()
Invokes the standardjava.lang.Object
methodhashCode()
on the fieldthis.f
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- Returns the hash code produced by the contents'
hashCode()
. - Code:
- Exact Method Body:
return f.hashCode();
-
clone
public EffectivelyFinal<A> clone()
This creates a clone of the wrapper class.
NOTE: The field, itself, is not cloned. AnotherEffectivelyFinal
wrapper is built, using the same internal field. Both the clone and'this'
will share a pointer-reference to the same field.- Overrides:
clone
in classjava.lang.Object
- Returns:
- An
EffectivelyFinal<A>
clone. - Code:
- Exact Method Body:
return new EffectivelyFinal<>(f);
-
equals
public boolean equals(java.lang.Object other)
This equality test checks whether both'this'
and'other'
share identical references. Equality between the contained / wrapped field'f'
is not checked.
Ifthis.f
andother.f
hold different references, this method will returnFALSE
even if both of these references have Object-Equality.- Overrides:
equals
in classjava.lang.Object
- Returns:
- Returns
TRUE
if and only if'other'
is an instance of'EffectivelyFinal'
andthis.f == other.f
. This method checks for pointer-equality, rather than object-equality. - Code:
- Exact Method Body:
if (! (other instanceof EffectivelyFinal)) return false; @SuppressWarnings("rawtypes") EffectivelyFinal o = (EffectivelyFinal) other; return this.f == o.f;
-
-