Skip to content
landawn edited this page Apr 29, 2018 · 8 revisions

User Guide

AbacusUtil is a general programming library/framework in Java. It's simple, powerful and easy to use with concise APIs. To provide the unmatchable programming experiences and high performance solutions, We think the API design and implementation again and again. First, let's take a quick look:

  • Most daily used methods: range/repeat/concat/stringOf/valueOf/as/...
int[] a = Array.range(1, 3);
int[] b = Array.rangeClosed(3, 7);
int[] c = N.concat(a, b);
String str = N.stringOf(c);
N.println(str); // [1, 2, 3, 4, 5, 6, 7]
int[] d = N.valueOf(int[].class, str);
assertTrue(N.equals(c, d));

List<String> list = N.asList("abc", "1", "2", "3");
Map<String, Integer> map = N.asMap("a", 1, "b", 2, "c", 3);
ImmutableSet<String> set = ImmutableSet.of("abc", "1", "2", "3");
  • Primitive List/Seq:
double avg = IntList.random(10).distinct().average().get();

Map<Integer, String> map = Seq.of("a", "b", "c").toMap(e -> e.hashCode(), e -> String.valueOf(e));

List<String> list = N.asList("a", "b", "c");

// Seq provides the extra most daily used and functional API for Collection.
Seq.of(list).flatMap(e -> N.repeat(e, 3)).forEach(Fn.println());
  • Lambda/Stream/Functional programming...
Stream.of("abc", "123", "ab123").filter(str -> str.startsWith("a")).forEach(N::println);
// abc
// ab123

List<String> strs = N.asList("Hello Java", "I LIKE Functional Programming");

// average length of words in the strings.
Stream.of(strs).flatMapp(str -> str.split(" ")).averageDouble(str -> str.length());
// 5.833333333333333

// count the characters in upper case.
Stream.of(strs).flatMapToChar(str -> CharStream.of(str)).filter(ch -> N.isUpperCase(ch)).count();
// 9
  • SQLBuilder/Executor...
 String sql_insert = insertInto(Account.class, N.asSet("id").sql();
 // sql: INSERT INTO account (gui, first_name, last_name, last_update_time, create_time) VALUES (:gui, :firstName, :lastName, :lastUpdateTime, :createTime)
 long id = sqlExecutor.insert(sql_insert, account);

 // Read
 String sql_getById = select("id", "gui", "firstName", "lastName").from("account").where("id = :id").sql();
 // sql: SELECT id AS "id", gui AS "gui", first_name AS "firstName", last_name AS "lastName" FROM account WHERE id = :id
 Account dbAccount = sqlExecutor.queryForEntity(Account.class, sql_getById, id);

 //Update
 dbAccount.setFirstName("newFirstName");
 String sql_updateFirstNameById = update("account").set("firstName").where("id = :id").sql();
 // sql: UPDATE account SET first_name = :firstName WHERE id = :id
 sqlExecutor.update(sql_updateFirstNameById, dbAccount);

 // Delete
 String sql_deleteByFirstName = deleteFrom("account").where("firstName = :firstName").sql();
 // sql: DELETE FROM account WHERE first_name = :firstName
 sqlExecutor.update(sql_deleteByFirstName, dbAccount);
  • XML/JSONParser...
String xml = xmlParser.serialize(account);
// XML: <account><id>0</id><gui>9d436ceb-a85f-40b2-86aa-20625a7d68b0</gui><firstName>3e37c0e9-96c3-40f3-99a2-bde3a384956c</firstName><lastName>38d19d46-6701-4f36-a112-924376154280</lastName><status>0</status><lastUpdateTime>1437609384633</lastUpdateTime><createTime>1437609384633</createTime></account>
Account account2 = xmlParser.deserialize(Account.class, xml);

String json = jsonParser.serialize(account);
// JSON: {id:0, gui:"edf40293-2e38-4c51-885b-e74638f7b7d2", firstName:"f063cb51-75f6-4810-a8e6-8e55d2db440f", lastName:"70e6f942-8400-4b84-9f17-02594d99ee2c", status:0, lastUpdateTime:1437609422217, createTime:1437609422217}
account2 = jsonParser.deserialize(Account.class, json);
  • Code Generation: (Account.java)...
// prepare class with fields:
public class Account {
    private String firstName;
    private String lastName;
    private Date birthdate;
    private String email;
    private String address;
    private Map<String, List<Date>> attrs;
}

// Then just two lines to generate the mostly beautiful and well-formatted entity class:
File srcDir = new File("./src");
CodeGenerator.writeClassMethod(srcDir, Account.class);