You have a queue of methods defined in the order in which they must be executed.
class Builder {
Queue<Function<String, Builder>> methods
= new LinkedList<>(List.of(this::method1, this::method2, this::method3));
}
You have a queue of arguments formed from a raw array.
Queue<String> argsQueue = new LinkedList<>(Arrays.asList(args));
Given that the method query is always longer than the queue of arguments, the algorithm is
Builder builder = new ExampleClass.Builder();
while (!argsQueue.isEmpty()) {
builder.getMethods().poll().apply(argsQueue.poll());
}
ExampleClass instance = builder.build();
It shouldn't necessarily be queues.
When you add/remove a method, you would need to take care only of methods
.
The whole (dummy) example:
class ExampleClass {
public static void main(String[] args) {
Queue<String> argsQueue = new LinkedList<>(Arrays.asList(args));
Builder builder = new ExampleClass.Builder();
while (!(argsQueue.isEmpty() || builder.getMethods().isEmpty())) {
builder.getMethods().poll().apply(argsQueue.poll());
}
ExampleClass instance = builder.build();
}
public static class Builder {
private final Queue<Function<String, Builder>> methods = new LinkedList<>(List.of(this::method1, this::method2, this::method3));
public Queue<Function<String, Builder>> getMethods() {
return methods;
}
public Builder method1(String s) {
System.out.println("method1 " + s);
return this;
}
public Builder method2(String s) {
System.out.println("method2 " + s);
return this;
}
public Builder method3(String s) {
System.out.println("method3 " + s);
return this;
}
public ExampleClass build() {
System.out.println("building...");
return new ExampleClass();
}
}
}