Smart Labels - aegisql/conveyor GitHub Wiki

Quick start: Creating Smart Labels

In most cases you will have a limited number of distinct messages, so you can make your label constants.

This is how you can do it using static fields

public class UserBuilderStep {
   public final static SmartLabel<UserBuilder> SET_FIRST 
      = SmartLabel.of("SET_FIRST",UserBuilder::setFirst);
   public final static SmartLabel<UserBuilder> SET_LAST 
      = SmartLabel.of("SET_LAST",UserBuilder::setLast);
   public final static SmartLabel<UserBuilder> SET_YEAR 
      = SmartLabel.of("SET_YEAR",UserBuilder:: setYearOfBirth);
}

You can use Java enums to store static immutable SmartLabels - better way to implement the multiton pattern in Java;

public enum UserBuilderStep implements SmartLabel<UserBuilder> {
	SET_FIRST(SmartLabel.of(UserBuilder::setFirst)),
	SET_LAST(SmartLabel.of(UserBuilder::setLast)),
	SET_YEAR(SmartLabel.of(UserBuilder::setYearOfBirth))
	;
	private final SmartLabel<UserBuilder> inner;
	<T> UserBuilderStep(SmartLabel<UserBuilder> inner) {
		this.inner = inner;
	}
	@Override
	public BiConsumer<UserBuilder, Object> get() {
		return inner.get();
	}
}

and then use

conveyor
   .part()
   .id(1)
   .value("John")
   .label(UserBuilderStep.SET_FIRST))
   .place();

Note that method of(String, BiConsumer) in the first example requires a name. This is an optional parameter, but it makes logging more readable.

Hint: Use 'import static' to avoid Enum type name typing

import static UserBuilderStep.*;
...
conveyor
   .part()
   .id(1)
   .value("John")
   .label(SET_FIRST))
   .place();

Smart Label functional interface

SmartLabel is a Functional interface, extending Java's Supplier. It has some additional default methods, that makes creation of Smart labels extremely flexible, powerful and easy. Default methods API:

  • identity() - returns same instance.
  • labelName(String name) - assigns visible name for the label, by re-defining its toString method.
  • intercept(Class clas, interceptor) - family of methods. Conveyor allows to send any values, however, in most cases, Builder's consumers will be implemented as methods with certain signature. And if you send Integer value to a method that expects String, you will have a ClassCastException. Intercepting allows to combine several different data consumers under one label.
  • andThen( after) - family of methods. Allows to add some action after
  • before( before) - family of methods. Allows to add some action before
⚠️ **GitHub.com Fallback** ⚠️