Giter Club home page Giter Club logo

enhancedproperties's Introduction

Tests javadoc Maven Central

License: MIT

EnhancedProperties

EnhancedProperties is an improved way to read *.properties-files in Java. It handles reading (and soon writing) files for you, so you don't need to worry about your Input/Output-Streams, closing files etc. There are even methods to parse the data into the most common datatypes. This library is especially useful, if you have a large amount of properties.

Use with Maven

<dependency>
  <groupId>de.felixsfd</groupId>
  <artifactId>enhancedproperties</artifactId>
  <version>1.1.0</version>
</dependency>

Basic example

The old way

For reading a double value from a local properties file, you'd do something like this:

Properties properties = new Properties();
try (FileInputStream fis = new FileInputStream(new File("my.properties"))) {
  properties.load(fis);
}

String strValue = properties.getProperty("someNumber", "0.0");
double doubleValue;
try {
  doubleValue = Double.parseDouble(strValue);
} catch (NumberFormatException e) {
  doubleValue = 0.0;
}

This might be nice an quick in some cases, but if you have lots of properties and read them very often, this can be quite annoying.

Quick and dirty

If you don't have many different keys in you file, you can create an anonymous class of EnhancedPropertiesInFile, EnhancedPropertiesInResources or EnhancedPropertiesInFileOrResources. This is not the recommended way, but it should work, too.

EnhancedPropertiesInFile propertiesInFile = new EnhancedPropertiesInFile("my.properties") {};
double doubleValue = propertiesInFile.getDouble("someNumber", 0.0);

The beautiful way

This is the recommended way of using EnhancedProperties.

Create your Properties-Class

To read the properties for your application, you'll need to create a subclass of EnhancedPropertiesInFile, EnhancedPropertiesInResources or EnhancedPropertiesInFileOrResources. In this example, we need EnhancedPropertiesInFile.

public class MyProperties extends EnhancedPropertiesInFile {
  protected MyProperties() throws IOException {
    super("my.properties");
  }

  public double getDoubleValue(double defaultValue) {
    return super.getDouble("doubleValue", defaultValue);
  }
}

Access the property

Since the MyProperties-class contains the file-path and handles parsing the double as well, we can now access the value like this:

MyProperties myProperties = new MyProperties();
double doubleValue = myProperties.getDoubleValue(0.0);

Example: Read file from resources

To read a file from your application's resources, you can follow the instructions of the "basic example". Just change the superclass for MyProperties from EnhancedPropertiesInFile to EnhancedPropertiesInResources.

public class MyProperties extends EnhancedPropertiesInResources {
  protected MyProperties() throws IOException {
    super("my.properties");
  }

  public double getDoubleValue(double defaultValue) {
    return super.getDouble("doubleValue", defaultValue);
  }
}

Example: Use two properties file overwriting each other

For complex use-cases, you can read files from file-system and the application's resources at the same time. If a key is present in both files, the value from the preferred file will be used. This is useful, if you ship default properties within the resources of the application but you wan't to override some of the values.

In this example, we assume, that the default properties are stored in the resources-root directory and the custom properties in /home/user/application/my.properties. The values of the file in the file-system should be preferred over the values in the resources.

@ReadRule(preferredLocation = ReadRule.Location.FILE)
public class MyProperties extends EnhancedPropertiesInResources {
  protected MyProperties() throws IOException {
    super("/home/user/application/my.properties", "my_default.properties");
  }

  public double getDoubleValue(double defaultValue) {
    return super.getDouble("doubleValue", defaultValue);
  }
}

If you want the properties in the resources to be the preferred file, just change the annotation to @ReadRule(preferredLocation = ReadRule.Location.RESOURCES).

enhancedproperties's People

Contributors

felixsfd avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar

enhancedproperties's Issues

Add possibility to write properties-files

Right now, the properties are read-only. It should be possible (and easy) to save changes.

  • add interface EnhancedWriteableProperties which extends EnhancedProperties with:
    • default setters (int, double, String,...)
    • save() method
  • add abstract class EnhancedWriteablePropertiesImpl which implements the new interface and extends EnhancedPropertiesImpl
  • make EnhancedPropertiesInFile and EnhancedPropertiesInFileOrResources extend the new class
  • add event listeners to listen for changed properties

Listen to external file changes

It should be possible to change files externally and reload the EnhancedProperties automatically

Additionally, you could call an event listener whenever the file was reloaded from disk

Support arrays and lists

There should be default getters and setters for arrays and lists.

The getters should have 3 parameters:

  • Key
  • Split by (A String which is used to split the value from the properties-file)
  • Optional: Default value (for example an empty array)

Example:
To read a list from a value like this, you would use ",":

myArray=one,two,three

EnhancedPropertiesInFileOrResources should not throw an exception if the file in the file-system does not exist

Version used: 1.0.0

Description of the bug:
The application is implementing EnhancedPropertiesInFileOrResources with the annotation @ReadRule(preferredLocation = ReadRule.Location.FILE). The file in resources exists, but the one in the file-system doesn't.

To Reproduce:
Steps to reproduce the behavior:
(it could be useful to include code examples and the contents of your *.properties-files)

Expected behavior
The file in the file-system should be optional

System information:

  • OS: Windows 10
  • Java Version: 1.8.0_102
  • EnhancedProperties Version: 1.0.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.