hydromatic-resource

Compiler-checked wrappers around resource files

License

License

Categories

Categories

Maven Build Tools Net
GroupId

GroupId

net.hydromatic
ArtifactId

ArtifactId

hydromatic-resource-maven-plugin
Last Version

Last Version

0.6
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

hydromatic-resource
Compiler-checked wrappers around resource files
Project URL

Project URL

http://github.com/julianhyde/hydromatic-resource
Project Organization

Project Organization

Julian Hyde
Source Code Management

Source Code Management

http://github.com/julianhyde/hydromatic-resource/tree/master

Download hydromatic-resource-maven-plugin

How to add to project

<plugin>
    <groupId>net.hydromatic</groupId>
    <artifactId>hydromatic-resource-maven-plugin</artifactId>
    <version>0.6</version>
</plugin>

Dependencies

compile (2)

Group / Artifact Type Version
org.apache.maven : maven-plugin-api jar 3.2.1
org.apache.maven : maven-core jar 3.2.1

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

Build Status

hydromatic-resource

Compiler-checked wrappers around resource and property files.

An example

This example shows how you can create a wrapper around some resources and properties. Suppose you have a properties file birthday.properties:

com.example.likes.cake=true
com.example.favorite.cake.flavor=Chocolate

and a resource bundle with resources in English in com/example/BirthdayResource_en_US.properties:

HappyBirthday=Happy birthday, {0}! You don't look {1,number}.
TooOld={0}, you're too old for cake!

and resources in French in com/example/BirthdayResource_fr_FR.properties:

HappyBirthday=Bon anniversaire, {0}! {1,number}, quel bon âge.

Write the following interface as a wrapper:

interface Birthday {
  @BaseMessage("Happy birthday, {0}! You don't look {1,number}.")
  Inst happyBirthday(String name, int age);
  
  @BaseMessage("{0} is too old.")
  ExInst<RuntimeException> tooOld(String name);

  @BaseMessage("Have some {0} cake.")
  Inst haveSomeCake(String flavor);

  @Default("false")
  @Resource("com.example.likes.cake")
  BoolProp likesCake();

  @Resource("com.example.favorite.cake.flavor")
  StringProp cakeFlavor(); 

  @Default("10")
  IntProp maximumAge(); 
}

Now you can materialize the wrapper based on the resource bundle and properties file:

Birthday birthday = Resources.create("com.example.BirthdayResource",
  "birthday.properties", Birthday.class);

and use it in a program:

void celebrate(String name, int age) {
  if (age > birthday.maximumAge().get()) {
    throw birthday.tooOld(name).ex();
  }
  System.out.println(birthday.happyBirthday(name, age).str());
  if (birthday.likesCake()) {
    System.out.println(birthday.haveSomeCake(birthday.cakeFlavor()).str());
  }
}

Note that some resources and properties do not occur in the files. The @BaseMessage and @Default annotations supply default values. In the case of resources, Java's resource mechanism inherits resources from more general locales: for instance, US English ('en_US') inherits from English ('en').

The wrapper converts properties to the right type, substitutes parameters, validates that localized resources have the same number and type of parameters as the base message, and creates exceptions for error conditions.

Resources inherit the JVM's locale, but you can override for the current thread:

Resources.setThreadLocale(locale);

and even on the resource instance:

throw birthday.tooOld("Fred").localize(locale).ex();

Get hydromatic-resource

From Maven

Get hydromatic-resource from Maven Central:

<dependency>
  <groupId>net.hydromatic</groupId>
  <artifactId>hydromatic-resource-maven-plugin</artifactId>
  <version>0.6</version>
</dependency>

Download and build

You need Java (1.6 or higher; 9 preferred), git and maven (3.2.1 or later).

$ git clone git://github.com/julianhyde/hydromatic-resource.git
$ cd hydromatic-resource
$ mvn package

Make a release

Using JDK 1.7, follow instructions in hydromatic-parent.

More information

Versions

Version
0.6
0.5.1
0.5
0.4