hydromatic-resource

Compiler-checked wrappers around resource files

Лицензия

Лицензия

Категории

Категории

Maven Компиляция и сборка Сеть
Группа

Группа

net.hydromatic
Идентификатор

Идентификатор

hydromatic-resource-maven-plugin
Последняя версия

Последняя версия

0.6
Дата

Дата

Тип

Тип

maven-plugin
Описание

Описание

hydromatic-resource
Compiler-checked wrappers around resource files
Ссылка на сайт

Ссылка на сайт

http://github.com/julianhyde/hydromatic-resource
Организация-разработчик

Организация-разработчик

Julian Hyde
Система контроля версий

Система контроля версий

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

Скачать hydromatic-resource-maven-plugin

Как подключить последнюю версию

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

Зависимости

compile (2)

Идентификатор библиотеки Тип Версия
org.apache.maven : maven-plugin-api jar 3.2.1
org.apache.maven : maven-core jar 3.2.1

test (1)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.11

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
0.6
0.5.1
0.5
0.4