Skip to content

Aliases

Rajab Davudov edited this page Jun 20, 2019 · 4 revisions

Aliases

It is possible to define an alias to another key or to an expression. It is useful to define optional keys to values and those keys can be used only for value retrieval or inside expressions.

Car c = new Car() ;
c.set(Car.PRODUCER, "Ford") ;
c.set(Car.YEAR_OF_PRODUCTION, 2019) ;

c.setAlias("manufacturer", "producer") ;
System.out.println(car.get("manufacturer")) ;
Ford

Not that setting aliases only creates a link to actual value. If you put another value with same alias name then it will be overwritten.

c.set("manufacturer", "Toyota") ;
System.out.println(car.get("manufacturer")) ;
Toyota

Alias Expressions

It is also possible to define an alias as an expression. This time expression will be evaluated each time it is being called.

Car c = new Car() ;
c.set(Car.PRODUCER, "Toyota") ;
c.set(Car.YEAR_OF_PRODUCTION, 2019) ;

c.setAlias("is_new", "year_of_production > 2018") ;
System.out.println(c.get("is_new"));
true

When we change production year and test again.

c.set(Car.YEAR_OF_PRODUCTION, 2018) ;
System.out.println(c.get("is_new"));
false