Skip to content

ngoanh2n/commons

Repository files navigation

GitHub forks GitHub stars GitHub watchers

Maven Central javadoc badge-jdk License: MIT

Table of Contents

Declaration

Gradle

Add dependency to build.gradle.

implementation("com.github.ngoanh2n:commons:1.5.1")

Maven

Add dependency to pom.xml.

<dependency>
  <groupId>com.github.ngoanh2n</groupId>
  <artifactId>commons</artifactId>
  <version>1.5.1</version>
</dependency>

Usage

├── build
├── out
│   ├── production
│   |   └── resources
│   |       └── log4j.properties
│   └── test
│       └── resources
│           ├── file.json
│           ├── user.yml
│           └── users.yml
├── src
│   ├── main
│   |   └── resources
│   |       └── log4j.properties
│   └── test
│       └── resources
│           ├── file.json
│           ├── user.yml
│           └── users.yml

Resources

Find and read Java resources.

  • File file = Resources.getFile("file.json")
  • Path path = Resources.getPath("file.json")
  • String content = Resources.getContent("file.json")
  • InputStream is = Resources.getInputStream("file.json")

System Property

  • ngoanh2n.findResourceOnClasspath
    Indicate to find the resource file on classpath (Default to true).
    • true: Get the resource on the classpath
      • {project}/out/test/resources/
      • {project}/out/production/resources/
    • false: Get the resource in root location
      • {project}/src/test/resources/
      • {project}/src/main/resources/

YamlData

Static

Reads Yaml file to Map, List of Map.

  1. Read to Map
    • Map<String, Object> map = YamlData.toMapFromResource("user.yml")
    • Map<String, Object> map = YamlData.toMapFromFile("src/test/resources/user.yml")
  2. Read to List of Maps
    • List<Map<String, Object>> maps = YamlData.toMapsFromResource("user.yml")
    • List<Map<String, Object>> maps = YamlData.toMapsFromFile("src/test/resources/user.yml")

Inheritance

Read Yaml file to Model, List of Models.
Model class must be public and has setter methods.

  1. Read to Model
    # Yaml: user.yml
    username: usr1
    notes:
      - note1
      - note2
    companies:
      - name: Com1
        address: Addr1
      - name: Com2
        address: Addr3
    // Model: User.java
    public class User extends YamlData<User> {
      private String username;
      private List<String> notes;
      private List<Company> companies;
    
      ...GETTERS & SETTERS...
    }
    // Model: Company.java
    public class Company extends YamlData<Company> {
      private String name;
      private String address;
    
      ...GETTERS & SETTERS...
    }
    • Without annotation
      • User user = new User().fromResource("user.yml").toModel()
      • User user = new User().fromFile("src/test/resources/user.yml").toModel()
    • With annotation
      Attach com.github.ngoanh2n.YamlFrom annotation for Model.
      • YamlFrom.resource()
        @YamlFrom(resource = "user.yml")
        public class User extends YamlData<User> {
            ...  
        }
      • YamlFrom.file()
        @YamlFrom(file = "src/test/resources/user.yml")
        public class User extends YamlData<User> {
            ...  
        }
      Overwrite value of com.github.ngoanh2n.YamlFrom annotation by calling fromResource(String) or fromFile(String) method.
  2. Read to List of Models
    # Yaml File: users.yml
    - username: usr1
      password: pwd1
    - username: usr2
      password: pwd2
    // Model: User.java
    public class User extends YamlData<User> {
      private String username;
      private String password;
    
      ...GETTERS & SETTERS...
    }
    • Without annotation
      • List<User> users = new User().fromResource("users.yml").toModels()
      • List<User> users = new User().fromFile("src/test/resources/users.yml").toModels()
    • With annotation
      Attach com.github.ngoanh2n.YamlFrom annotation for Model.
      • YamlFrom.resource()
        @YamlFrom(resource = "users.yml")
        public class User extends YamlData<User> {
            ...  
        }
      • YamlFrom.file()
        @YamlFrom(file = "src/test/resources/users.yml")
        public class User extends YamlData<User> {
            ...  
        }
      Overwrite value of com.github.ngoanh2n.YamlFrom annotation by calling fromResource(String) or fromFile(String) method.

About

Commons for ngoanh2n

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages