Is there any library that provides a smooth way to build Java format strings?

The syntax of Java format strings may become complex, for example:

"|%1$-10s|%2$-10s|%3$-20s|\n"

Someone has created a smooth DSL to help build these format strings, which seems to be mature (similar to jooq's effect on SQL)

Does such a thing exist?

Solution

You can use fluflo (a fluent API generator) to create such APIs, inspired by the jooq's API and the jooq's underlying API design techniques

Flufluflu provides annotations processed using apt tools to generate fluent APIs from API implementations Annotation simulates a finite state machine This is an example of their Wiki:

@Fluentize(className = "CoreClass",startState = "State0",startMethod = "start")
public abstract class ToBeFluentized implements Cloneable {

    @Transitions({ 
        @Transition(from = "State0",end = true),@Transition(from = "State1",end = true) 
    })
    public void end() {
    }

    protected String with = null;
    protected List<byte[]> b = new LinkedList<>();

    @Transition(from = { "State0","State1" },to = "State0")
    public abstract ToBeFluentized with(
        @AssignTo("with") String a,@AddTo("b") byte[] b
    );

    @Transition(from = "State1",to = "State0")
    public ToBeFluentized z() {
        return this;
    }

    Set<Integer> j = new HashSet<>(); 
    @Transition(from = "State1",to = "State1",name="a")
    public abstract ToBeFluentized z(@AddTo("j") int j);

    @Transition(from = "State0",to = "State1")
    public ToBeFluentized a() {
        return this;
    }

    @Transition(from = "State0",to = "State1")
    public ToBeFluentized b() {
        return this;
    }

    @Transition(from = "State0",to = "State1")
    public ToBeFluentized vari(String... strings) {
        return this;
    }
}

You can then use this:

State0 c = CoreClass.start().a().z();
State0 d = c.b().with("z","z".getBytes());
State0 e = c.b().with("q",new byte[]{0,1});
d.end();
e.end();

Of course, you still need to write an implementation: -)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>