Skip to content

API Examples (Static)

Thomas Deutsch edited this page Mar 18, 2018 · 1 revision

[FluentAPI] Static Usage Examples

Example01

Minimalistic fluent example using a anonymous static data provider We use a very simplistic ClassBuilder implementation called StringBufferEnumBuilder And to get something we export the result on the console through the ReportingEnumExporter

Fluent.builder()
    .dataProvider(() -> {
      EnumData model = new EnumData();
      model.getData().add(NameTypeValue.of("RED"));
      model.getData().add(NameTypeValue.of("GREEN"));
      model.getData().add(NameTypeValue.of("BLUE"));
      return model;
    })
    .classBuilder(new StringBufferEnumBuilder().withName("org.my.data.Colors"))
    .enumExporter(new ReportingEnumExporter(SYSTEM_OUT))
    .build();

Output (Console):

package org.my.data;

public enum Colors {
  RED,
  GREEN,
  BLUE;
}

Example02

Fluent example using a anonymous static data provider We now have three fields where we want to save the real color data as an integer We define datatypes (int.class) and names of course (r-g-b) We also use the same ClassBuilder implementation like in Example01 but this time we call a factory method and export on the console through the ReportingEnumExporter

Fluent.builder()
  .dataProvider(() -> {
    EnumData model = new EnumData();
    model.setFieldClasses(int.class, int.class, int.class);   //define datatypes
    model.setFieldNames("r", "g", "b");   //define names for the field variables
    model.getData().add(NameTypeValue.of("RED", new Object[]{255, 0, 0}));
    model.getData().add(NameTypeValue.of("GREEN", new Object[]{0, 255, 0}));
    model.getData().add(NameTypeValue.of("BLUE", new Object[]{0, 0, 255}));
    return model;
  })
  .classBuilder(new StringBufferEnumBuilder().withName("org.my.data.Colors"))
  .classBuilder(ClassBuilderFactory.getEnumClassBuilder().withName("org.my.data.Colors"))
  .enumExporter(new ReportingEnumExporter(SYSTEM_OUT))
  .build();

Output (Console):

package org.my.data;

public enum Colors {
  RED(255, 0, 0),
  GREEN(0, 255, 0),
  BLUE(0, 0, 255);

private final int r;
private final int g;
private final int b;

Colors(int r, int g, int b) {
  this.r = r;
  this.g = g;
  this.b = b;
}

public int getR() {
  return this.r;
}
public int getG() {
  return this.g;
}
public int getB() {
  return this.b;
}

}
Clone this wiki locally