Skip to content

Instantly share code, notes, and snippets.

@t-botz
Last active December 19, 2015 11:38
Show Gist options
  • Save t-botz/5948761 to your computer and use it in GitHub Desktop.
Save t-botz/5948761 to your computer and use it in GitHub Desktop.
Java properties & Lambdas
public class Person {
//The syntax obj#field, uses the new operator # which represents a direct access to the field (without getter/setter)
//Default public getter/setter
private String firstName : ();
//Explicitly defined getter/setter using lambda, exact equivalent as the default syntax ().By Default visibility is public
private String lastName : (()-> this#lastName, (lastName)-> this#lastName = lastName);
//Immutable field. When final the syntax takes only one lambdas, the getter.
private final int age : (()-> this#lastName); // similar as ()
//Ability to modify the visibility through method references. (Visibility inference)
private int id = 0 :(()-> this#id, this::setId);
private void setId(int id) {
this.id = id;
}
/**********************
* Need more thinking :
**********************/
//Indexed properties (defined in the javabeans spec)
private int[] array = new int[2] : (
( index ) -> { this#array[index] },
( index, value ) -> { this#array[index] = value }
);
//Properties not backed by a field
private final void length : (() -> this#array.length);
//Properties can be applied to static fields :
private static final int DEFAULT_NUMBER_OF_LEGS = 4 : ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment