Quirky, weird and fun pattern for Java

To be honest I have no idea what you can do with the following pattern and if it is good at all. It just a thought I had and it felt like there was something there, so I decided to write it down. Hopefully you can use the following pattern for something.

import java.util.function.Supplier;
import java.util.UUID;


public class Quirky {
  private Supplier<UUID> id;
  private Supplier<String> name;


  public Supplier<UUID> getId() {
    return this.id;
  }

  public Supplier<String> getName() {
    return this.name;
  }

  public void setId(UUID id) {
    this.id = () -> { return id; };
  }

  public void setId(Supplier<UUID> id) {
    this.id = id;
  }

  public void setName(String name) {
    this.name = () -> { return name; };
  }

  public void setName(Supplier<String> name) {
    this.name = name;
  }

}

The idea was to turn the properties into Suppliers and therefore you would get to do some functional style flows. I am not sure yet how it will fit in with other patterns. For example to set the id you would do :

Quirky q = new Quirky();
q.setId(UUID.randomUUID());

Then to retrieve the id you would do :

q.getId().get();

There is the fact you have to do double gets that feel weird. If you wanted to make a random Quirky you could do the following:

Quirky q = new Quirky();
q.setId(() -> { return UUID.randomUUID();});

Then every following call to getId().get() will return a new UUID.

One thing I did want to implement were the double setters . One will wrap a value into a simple supplier but you could also augment that.

public void setName(String name) {
    this.name = () -> { return name.toUpperCase(); };
  }

Now the normal method will always return all caps. There can also be logging and other side effects.

Hopefully you will read this and think of something somewhere to use it. Also I have the feeling somewhere functional programmers woke up and are going to bombard me with Monads, Functors and similar corrigenda.

#code #java