property
property is property framework java implementation.
Feature
Gracefully read and update the properties file
After writing properties file attribute is not out of order
Flexible definition encoded information
Use OO way of operating property file
It supports multi-level object reference
Change Log
Quick Start
Environmental dependent
Maven 3.x
Jdk 1.7+
Maven relies introduced
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>property</artifactId>
<version>0.0.4</version>
</dependency>
Getting Case
Read properties
PropertyBs.getInstance("read.properties").get("hello");
read.properties
A file path, hello
value name for the existing property.
Read attribute specifies the default value
final String value = PropertyBs.getInstance("read.properties")
.getOrDefault("hello2", "default");
read.properties
A file path, hello2
the attribute value name does not exist, default
the default value is returned when the property does not exist.
Setting Properties
PropertyBs.getInstance("writeAndFlush.properties").setAndFlush("hello", "world-set");
writeAndFlush.properties
A file path, hello
the attribute information needs to be set.
Method bootstrap class Overview
No. | method | Explanation |
---|---|---|
1 | getInstance(propertyPath) | Get the guide class instance attributes specified file path |
2 | charset(charset) | Specify the file encoding, default UTF-8 |
3 | get(key) | Getting a property value corresponding to the key |
4 | getOrDefault(key, defaultValue) | Getting a property value corresponding to the key, there is no return defaultValue |
5 | set(key, value) | Set value (memory) |
6 | remove(key) | Removal value (memory) |
7 | flush() | Refresh memory disk changes to the current file |
9 | flush(path) | Refresh memory disk changes to a specified file |
10 | set(map) | Set map information to memory |
11 | set(bean) | Setting bean object information into memory |
12 | asMap() | Returns attribute information in memory, as a return to Map |
13 | asBean(bean) | Returns the memory object attribute information bean |
Objects
Brief introduction
We wish to operate property may want to manipulate objects as in line with OO thinking.
Settings
User user = new User();
user.setName("hello");
user.setHobby("hobby");
final long time = 1574147668411L;
user.setBirthday(new Date(time));
PropertyBs propertyBs = PropertyBs.getInstance("setBean.properties")
.set(user);
Assert.assertEquals("hobby", propertyBs.get("myHobby"));
Assert.assertEquals("1574147668411", propertyBs.get("birthday"));
Read value
PropertyBs propertyBs = PropertyBs.getInstance("setBean.properties"
.set("myHobby", "play")
.set("birthday", "1574147668411");
User user = new User();
propertyBs.asBean(user);
Assert.assertEquals("play", user.getHobby());
Assert.assertEquals(1574147668411L, user.getBirthday().getTime());
Object Definition
- User.java
public class User {
private String name;
@PropertyField("myHobby")
private String hobby;
@PropertyField(converter = DateValueConverter.class)
private Date birthday;
}
@PropertyField comment
No. | Attributes | Defaults | Explanation |
---|---|---|---|
1 | value | The current field name | Corresponding to the attribute name property |
2 | converter | The default conversion achieved DefaultValueConverter |
Current conversion processing attributes fields |
Custom conversion classes
- DateValueConverter.java
This is the type of process we focused Date type, its own implementation.
To achieve the following:
public class DateValueConverter implements IValueConverter {
@Override
public Object fieldValue(String value, IFieldValueContext context) {
return new Date(Long.parseLong(value));
}
@Override
public String propertyValue(Object value, IPropertyValueContext context) {
Date date = (Date)value;
return date.getTime()+"";
}
}
set
Explanation
Sometimes a property may be set or array being given here is relatively simple to achieve.
The field values separated by commas directly as an attribute value.
Test Case
UserArrayCollection userArrayCollection = buildUser();
PropertyBs propertyBs = PropertyBs.getInstance("setBeanArrayCollection.properties")
.set(userArrayCollection);
Assert.assertEquals("array,collection", propertyBs.get("alias"));
Assert.assertEquals("array,collection", propertyBs.get("hobbies"));
Object Definition
- UserArrayCollection.java
public class UserArrayCollection {
private List<String> alias;
private String[] hobbies;
}
Only supports String type, do not want to be too complicated.
The latter will consider adding various types of support.
Multi-level objects
Explanation
Sometimes we refer to other objects in an object, such as a target object contained b.
Key abc used here as an attribute in this way, more in line with customary use.
Test Case
Set up
Book book = new Book();
book.name("《海底两万里》").price("12.34");
UserEntry user = new UserEntry();
user.name("海伦").book(book).age("10");
PropertyBs propertyBs = PropertyBs.getInstance("setBeanEntry.properties")
.set(user);
Assert.assertEquals("海伦", propertyBs.get("name"));
Assert.assertEquals("10", propertyBs.get("age"));
Assert.assertEquals("《海底两万里》", propertyBs.get("book.name"));
Assert.assertEquals("12.34", propertyBs.get("book.price"));
Read
Map<String, String> map = new HashMap<>();
map.put("name", "海伦");
map.put("age", "10");
map.put("book.name", "《海底两万里》");
map.put("book.price", "12.34");
UserEntry userEntry = new UserEntry();
PropertyBs.getInstance("setBeanEntry.properties")
.set(map)
.asBean(userEntry);
Assert.assertEquals("UserEntry{name='海伦', age=10, book=Book{name='《海底两万里》', price=12.34}}",
userEntry.toString());
Object Definition
- UserEntry.java
public class UserEntry {
private String name;
private String age;
@PropertyEntry
private Book book;
}
- Book.java
public class Book {
private String name;
private String price;
}
@PropertyEntry Description
@PropertyEntry
Field indicates whether the annotation is used to identify a multi-level objects.
This annotation is only one property, that is value()
, the current field can be used to specify a nickname, and @PropertyField
nicknames is similar.
Follow-up properties
- More built-in type conversion to achieve