Gaffer icon indicating copy to clipboard operation
Gaffer copied to clipboard

Removal of Builders

Open GCHQDeveloper404 opened this issue 6 years ago • 2 comments

Investigate switching to using builder-style setters to remove the need for inner Builder Classes, except where validation is required before creation e.g. Graph.Builder().build()

GCHQDeveloper404 avatar Mar 12 '18 08:03 GCHQDeveloper404

An example of a current query:

new GetElements().input("vertex1").view(new View.Builder().edge("red").build()).build();

If we added some extra methods it could easily look like this:

GetElements.input("vertex1").e("red")

Another more nested query:

final List<EntitySeed> allV = Arrays.asList(new EntitySeed("seed1"), new EntitySeed("seed2"));

final GetAdjacentIds redEdges = new GetAdjacentIds.Builder()
       .view(new View.Builder()
               .edge("red")
               .build())
       .inOutType(IncludeIncomingOutgoingType.OUTGOING)
       .build();

final GetAdjacentIds blueEdges = new GetAdjacentIds.Builder()
       .view(new View.Builder()
               .edge("blue")
               .build())
       .inOutType(IncludeIncomingOutgoingType.OUTGOING)
       .build();

final While op = new While.Builder<>()
       .input(allV)
       .operation(redEdges)
       .conditional(
               new IsLongerThan(0),
               new While.Builder<>()
                       .operation(blueEdges)
                       .conditional(new CollectionContains(new EntitySeed(3)))
                       .build()
       )
       .build();

And the simplified format:

While.input(allV).repeat(GetAdjacentIds.e("red").out()).until(new NotEmpty(), While.repeat(GetAdjacentIds.e("blue").out()).until(new HasId(3)));

p013570 avatar Jun 26 '18 10:06 p013570

Rather than using the current Builder classes it would be simpler to add builder methods such as: We have already added a few to the User class. Essentially just add some methods in the form:

public class Pojo {
    private String x;
    private String y;

    public Pojo x(final String x) {
        this.x = x;
        return this;
    }

    public Pojo y(final String y) {
        this.y = y;
        return this;
    }
}

The Pojo class can then be initialised like:

Pojo pojo = new Pojo().x("value of x").y("value of y");

m55624 avatar Oct 15 '18 15:10 m55624