Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Foo {
- private Map<String, Object> data = new HashMap<>();
- String build() {
- StringBuilder builder = new StringBuilder();
- builder.append("{");
- Iterator<Map.Entry<String, Object>> it = data.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry entry = it.next();
- builder.append(String.format(
- ""%s": "%s"", entry.getKey(), entry.getValue()
- ));
- if (it.hasNext()) {
- builder.append(", ");
- }
- }
- builder.append("}");
- return builder.toString();
- }
- Foo a(Object value) {
- data.put("a", value);
- return this;
- }
- Foo a() {
- return a(null);
- }
- Foo b(Object value) {
- data.put("b", value);
- return this;
- }
- Foo c(Object value) {
- data.put("c", value);
- return this;
- }
- }
- public static void main(String[] args) {
- System.out.println("text: " + new Foo().a(null).b(1).c("123").build());
- // text: {"a": "null", "b": "1", "c": "123"}
- System.out.println("text: " + new Foo().build());
- // text: {}
- System.out.println("text: " + new Foo().a().build());
- // text: {"a": "null"}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement