Fluent API Design in OSGL Library

Gelin Luo@

18 Years software developer

Author of 

Technical Director of Thinking Studio

About ME

What is OSGL

Open Source General Libary

osgl-tool

osgl-version

osgl-aaa

excel-reader

genie

osgl-http

osgl-mvc

osgl-logging

osgl-cache

osgl-storage

StringUtils.split(a, b)
S.split(a).by(b).get()
StringUtils.split(a, b)
StringUtils.replace(a, b, c)
StringUtils.replace(a, b, c)
S.replace(b).with(c).in(a)
StringUtils.startsWith(a, b);
StringUtils.contains(a, b);
StringUtils.countMatches(a, b);
StringUtils.wrap(a, b);
...
StringUtils.startsWith(a, b);
StringUtils.contains(a, b);
StringUtils.countMatches(a, b);
StringUtils.wrap(a, b);
...
S.is(a).startsWith(b);
S.is(a).contains(b);
S.count(b).in(a);
S.wrap(a).with(b);
...
File file = File.createTempFile("osgl", ".tmp");
byte[] ba = {1, 2, 3};
IO.write(ba).to(file);
byte[] ba0 = IO.read(file).toByteArray();
eq(ba, ba0);
String s = "60";
eq((byte) 60, $.convert(s).to(Byte.class));
eq(255, $.convert("FF").hint(16).toInteger());
Date date = $.convert("06 Apr 2018").hint("dd MMM yyyy").toDate();
eq("2018-04-06", $.convert(date).hint("yyyy-MM-dd").toString());
eq(5, $.convert(null).defaultTo(5).toInteger());
eq(2, $.convert("2").defaultTo(5).toInteger());
URL url = ImgTest.class.getResource("/img/img2.jpg");
InputStream is = IO.inputStream(url);
source(is)
        .resize(640, 480)
        .pipeline()
        .crop(50, 50, -50, -50)
        .pipeline()
        .watermark("HELLO OSGL")
        .writeTo("/tmp/img2_pipeline.jpg");

Tips

  • Pay attention to multiple parameter methods
  • Use stage class
  • Use chained call

Downside

Downside

  • Temporary objects
    • Can JVM optimization help?
  • Library size
<dependency>
  <groupId>org.osgl</groupId>
  <artifactId>osgl-tool</artifactId>
  <version>1.10.0</version>
</dependency>

GET OSGL Tool Library

Q&A

Fluent API design in OSGL library

By Gelin Luo

Fluent API design in OSGL library

Introduce Fluent API design in OSGL tool library

  • 472