Skip to content
Supports to interact with properties file
Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.travis.yml
CHANGELOG.md
README.md
pom.xml

README.md

Build Status Dependency Status Coverage Status Maven Central Javadocs

Synopsis

This project support for interact with properties file

Code Example

1. Get all classes're mapped to keys in properties file

	try {
		Map<Object, Class<?>> classes = new ClassFetcher.Builder()
			.context(getClass())
			.file(PROPERTIES_FILE_PATH)
			.reader(new BaseFileReader()) //or Base64FileReader
			.build()
			.fetch();
	} catch (PropertiesFileException e) {
		e.printStackTrace();
	}

2. Get class mapped to key in properites file

	try {
		Class<User> = new ClassFetcher.Builder()
			.context(getClass())
			.file(PROPERTIES_FILE_PATH)
			.reader(new BaseFileReader()) //or Base64FileReader
			.build()
			.fetch("key name");
	} catch (PropertiesFileException e) {
		e.printStackTrace();
	}

3. Create a factory and use this factory for creating a new instance

Let's say you have a properties file that save pairs of interface and implementation class, example:

	com.tvd12.interface.IClassA=com.tvd12.implementation.ClassAImpl
	com.tvd12.interface.IClassB=com.tvd12.implementation.ClassBImpl

and you want to create an instance of interface, you can do: Step 1: Init a factory, example:

	InstanceFactory.defaultInstance().initialize("array of properties files");

Step 2: Use the factory to create interface's instance from implementation class, example:

	IClassA instance = InstanceFactory.defaultInstance().getPrototype(IClassA.class);

Step 3: (Optional) If you want to create singleton instance of interface, you can do, example:

	IClassA instance = InstanceFactory.defaultInstance().getSingleton(IClassA.class);

4. Map properties file to object

Let's say you have a properties file "room-config.properties":

capacity = 1000 id = 1001 maxRoomVariablesAllowed = 30 maxSpectators = 50

and you want to map it to RoomConfig object, you can do:

	RoomConfig room = new PropertiesMapper()
		.file("room-config.properties")
		.clazz(ExampleRoom.class)
		.reader(new BaseFileReader())
		.map();

and if you encode properties file with base64 you can do:

	RoomConfig room = new PropertiesMapper()
		.file("room-config.properties")
		.clazz(ExampleRoom.class)
		.reader(new Base64FileReader())
		.map();

4. Convert an object to a map

Sometimes you want to convert an object to a map, you can do:

	PropertiesBean map = new PropertiesBean(ClassA.class);

or

	PropertiesBean map = new PropertiesBean(new ClassA());

5. Use annotation to map properties file to object or convert an object to a map

Sometimes you have properties files with list of key and value, but key name is not same with field name, to map them, you can do:

@PropertyWrapper
public class RoomConfig {
    @Setter @Getter
    protected int variablesCount;
    @Setter @Getter
    @Property("public")
    protected boolean isPublic;
    // your code

Motivation

Because sometimes we want to make loose coupling source code, we want to use factory design pattern, we want to map a properties file to object, convert an object to map, so I make this project for that mean

Installation

	<dependency>
		<groupId>com.tvd12</groupId>
		<artifactId>properties-file</artifactId>
		<version>1.0.8</version>
	</dependency>

API Reference

http://www.javadoc.io/doc/com.tvd12/properties-file

Tests

mvn test

Contributors

None

License

  • Apache License, Version 2.0
You can’t perform that action at this time.