public final class C10NFilters extends Object
| Constructor and Description |
|---|
C10NFilters() |
| Modifier and Type | Method and Description |
|---|---|
static <T> C10NFilterProvider<T> |
cachedFilterProvider(C10NFilterProvider<T> filterProvider)
Decorates the specified filter provider with a simple static cache.
|
static <E extends Enum<?>> |
enumMapping(Class<E> enumClass,
Class<?> c10nMappedInterface)
Create an Enum-to-methods mapping filter to ease Enum translation using C10N.
|
static <T> C10NFilterProvider<T> |
staticFilterProvider(C10NFilter<T> filter)
Filter provider that always returns the specified instance
|
public static <E extends Enum<?>> C10NFilterProvider<E> enumMapping(Class<E> enumClass, Class<?> c10nMappedInterface)
Create an Enum-to-methods mapping filter to ease Enum translation using C10N.
Consider the following Enum type with 3 values:
enum Status{
Open, Closed, Pending
}
In order to localise each of the values, first create a c10n interface with a method for each of the Status values
@C10NMessages
public interface StatusMsg {
@En("open")
@Ja("未着手")
String open();
@En("closed! beer time!")
@Ja("完了")
String closed();
@En("pending ...")
@Ja("進行中")
String pending();
}
Then, in your c10n configuration add an enum-filter binding for the Status type
void configure(){
bindFilter(C10NFilters.enumMapping(Status.class, StatusMsg.class), Status.class);
}
Now, every time c10n encounters Status as a method argument in a c10n-interface type, the it will be replaced with the appropriate localised version of the Enum value:
@C10NMessages
public interface Messages{
@En("Localised status is: {0}")
@Ja("状態は{0}")
String showStatus(Status status);
}
Invoking showStatus(Status.Closed) will render
as "Localised status is: closed! beer time!".
You can restrict filter application only to method arguments annotated with a given
annotation, or one of the given annotations from a list, by using annotatedWith(Class) method
when binding. For example:
void configure(){
bindFilter(new IntFormattingFilter(), int.class)
.annotatedWith(Precise.class);
}
The above declaration will make sure int arguments are only passed through the
IntFormattingFilter whenever the method argument is marked with the @Precise
annotation. Other int arguments will not have the filter applied.
Enum values are mapped to c10n-interface methods in the following order:
status_open()closed()Note: method mapping is case-insensitive.
Note: mapped methods cannot take any arguments. Methods with arguments will be excluded from mapping.
Warning: if mapping for one or more values is not found, a runtime exception will be thrown.
E - Enum typeenumClass - Enum type to create mapping forc10nMappedInterface - a c10n-interface containing mapped methodspublic static <T> C10NFilterProvider<T> staticFilterProvider(C10NFilter<T> filter)
Filter provider that always returns the specified instance
T - Filter argument typefilter - filter instance to return from the generated provider(not-null)public static <T> C10NFilterProvider<T> cachedFilterProvider(C10NFilterProvider<T> filterProvider)
Decorates the specified filter provider with a simple static cache.
Only the first call will result in an execution of C10NFilterProvider.get() method.
The following calls will always return a cached instance of the first call.
T - Filter argument typefilterProvider - filter provider to decorate with caching (not-null)Copyright © 2013. All Rights Reserved.