Skip to main content

Quick start

It's very easy to use Auto-Pipeline, just follow the steps.

Step1. add dependency​

auto-pipeline has published to maven central, click here to find the latest version. current the latest version is 0.3.0

pom.xml
<dependency>
<groupId>com.foldright.auto-pipeline</groupId>
<artifactId>auto-pipeline-processor</artifactId>
<version>0.3.0</version>
<scope>provided</scope>
</dependency>
note the dependency scope!

The auto-pipeline annotation processor will generate the pipeline classes for the interface.

Annotation processor dependency should be "provided" scope, because it's only needed at compile time.

Step2. Using @AutoPipeline​

Very simple and easy.

Define Your interface​

Let's define your business interface called ConfigSource.

The ConfigSource has the get() method, input a string as key and output a string as the value. like this:

ConfigSource.java
public interface ConfigSource {
String get(String key);
}

Annotation the interface​

If we want ConfigSource has some extensibility, we can apply the chain of responsibility pattern to it for extensibility.

We can simply add @AutoPipelin to ConfigSource to make ConfigSource more extensible:

ConfigSource.java
@AutoPipeline // add @AutoPipeline here!
public interface ConfigSource {
String get(String key);
}

And then we compile our code, auto-pipeline-processor will auto generate pipeline java files for ConfigSource into subpackage pipeline:

  • ConfigSourceHandler.java the responsibility interface we want to implement for extensibility
  • ConfigSourcePipeline.java the chain
  • ConfigSourceHandlerContext.java
  • AbstractConfigSourceHandlerContext.java
  • DefaultConfigSourceHandlerContext.java

Oh yeah! We add @AutoPipeline and we get 5 files for free. The 5 files make up our ConfigSource pipeline!

Step3. write your business code​

After generating java files, we can now add our business code to the implements.

we can implement MapConfigSourceHandler and SystemConfigSourceHandler (they are all in the ConfigSource handler example):

MapConfigSourceHandler.java
public class MapConfigSourceHandler implements ConfigSourceHandler {
private final Map<String, String> map;

public MapConfigSourceHandler(Map<String, String> map) {
this.map = map;
}

@Override
public String get(String key, ConfigSourceHandlerContext context) {
String value = map.get(key);
if (StringUtils.isNotBlank(value)) {
return value;
}
return context.get(key);
}
}
SystemConfigSourceHandler.java
public class SystemConfigSourceHandler implements ConfigSourceHandler {
public static final SystemConfigSourceHandler INSTANCE = new SystemConfigSourceHandler();

@Override
public String get(String key, ConfigSourceHandlerContext context) {
String value = System.getProperty(key);
if (StringUtils.isNotBlank(value)) {
return value;
}
return context.get(key);
}
}

Step4. using your pipeline​

Now you can create the ConfigSourcePipeline by composing ConfigSourceHandlers.

And using the pipeline as the ConfigSource:

Main.java
public class Main {
public static void main(String[] args) {
Map<String, String> mapConfig = new HashMap<String, String>();
mapConfig.put("hello", "world");
ConfigSourceHandler mapConfigSourceHandler = new MapConfigSourceHandler(mapConfig);

ConfigSource pipeline = new ConfigSourcePipeline()
.addLast(mapConfigSourceHandler)
.addLast(SystemConfigSourceHandler.INSTANCE);
}
}
the ConfigSourcePipeline!

In fact, the ConfigSourcePipeline.java implements ConfigSource.

So we can use ConfigSourcePipeline as ConfigSource.

Now, we can use the pipeline.get(...) to invoke the chain! 🎉

Main.java
public class Main {
public static void main(String[] args) {
// ...

pipeline.get("hello");
// get value "world"
// from mapConfig / mapConfigSourceHandler

pipeline.get("java.specification.version");
// get value "1.8"
// from system properties / SystemConfigSourceHandler
}
}

That's all, the @AutoPipeline help you generate the Pipeline Design Pattern Code! It's never been easier!

What's next?​