Bernhard Colby
Bernhard Colby

Reputation: 319

Alias-like structure in Java

I have simplified my problem. For example, in the following code snippet, carsList is empty at the beginning and listEmpty is naturally true. However, after the first line, I have added new item to the list and printed results. The value of listEmpty Boolean has not changed when carsList.isEmpty returned as expected false.

Boolean listEmpty = carsList.isEmpty(); //carsList.isEmpty() returns true

carsLists.add(car1);

System.out.println("ListEmpty: " + listEmpty + " CarsList.isEmpty:" + carsList.isEmpty); // ListEmpty: true CarsList.isEmpty: false

I need an alias-like structure which always points the latest result of a method.

alias = carsList.isEmpty(); //carsList.isEmpty() returns true

carsLists.add(car1);

System.out.println("Alias: " + alias + " CarsList.isEmpty:" + carsList.isEmpty); // Alias: false CarsList.isEmpty: false

When the result of a method changes, the value of the alias-like structure should change accordingly.

Edit

The selected solution is not what i was looking for but it was the closest. The other solutions were also perfect and can be used in other cases.

Upvotes: 1

Views: 134

Answers (6)

William Greenly
William Greenly

Reputation: 3989

You can do this with the Reactive extensions for Java which intrinsically uses the observer pattern:

https://github.com/ReactiveX/RxJava

In the example above you can implement these as follow:

Observable oCarList = Observable.fromIterable(carList);
oCarList.subscribe(
  new Consumer<Car>() {
      @Override
      void accept(Car car) {
        //Method that executes when a car is added
        System.out.println(car.toString());
      }
  }
)

Upvotes: 0

Stefan Warminski
Stefan Warminski

Reputation: 1835

With Java 8 you can use the Function interface:

carsList.add(car1);
Function<List<?>, Boolean> empty = List::isEmpty;

System.out.println("ListEmpty: " + empty.apply(carsList) + " CarsList.isEmpty:" + carsList.isEmpty());

Upvotes: 1

Ivan Gammel
Ivan Gammel

Reputation: 662

You can use a lambda returning a Supplier instance. It will work the same as passing by reference the latest result of method invocation:

// note the final keyword
final List<...> list = ...
Supplier<Boolean> isEmpty = () -> list.isEmpty();
// now you can return it and use somewhere else
return isEmpty; 
...
list.add(...);
...
System.out.println("Is list empty? " + isEmpty.get());

Upvotes: 1

Socowi
Socowi

Reputation: 27245

Call the method every time. isEmpty() usually is super fast,
more precisely O(1).

If you need to call a lot of functions to access the list in the first place, "alias" the list instead:

List<> myList = superLong().function().nesting().getList();
myList.isEmpty();
myList.isEmpty();
...

instead of

superLong().function().nesting().getList().isEmpty();
superLong().function().nesting().getList().isEmpty();
...

Upvotes: 2

Rahul
Rahul

Reputation: 16355

private Boolean isListEmpty(){
   return carsList.isEmpty()
}

Instead of accessing the boolean variable, use this method to check for the boolean condition like

System.out.println("Alias: " + isListEmpty() + " CarsList.isEmpty:" + carsList.isEmpty); 

Upvotes: 1

iPhantomGuy
iPhantomGuy

Reputation: 250

Then you probably want to use the Observer pattern. Her is how you use it: https://www.tutorialspoint.com/design_pattern/observer_pattern.htm

Upvotes: 0

Related Questions