[New] Added I18nMessageVisitor with a ToJson visitor
This commit is contained in:
parent
a3f8c862c0
commit
2a0a59f71b
|
@ -0,0 +1,27 @@
|
||||||
|
package li.strolch.model.i18n;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import li.strolch.utils.I18nMessage;
|
||||||
|
import li.strolch.utils.I18nMessageVisitor;
|
||||||
|
|
||||||
|
public class I18nMessageToJsonVisitor implements I18nMessageVisitor<JsonObject> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonObject visit(I18nMessage message) {
|
||||||
|
JsonObject json = new JsonObject();
|
||||||
|
|
||||||
|
json.addProperty("key", message.getKey());
|
||||||
|
json.addProperty("message", message.getMessage());
|
||||||
|
|
||||||
|
Properties values = message.getValues();
|
||||||
|
if (!values.isEmpty()) {
|
||||||
|
JsonObject valuesJ = new JsonObject();
|
||||||
|
values.stringPropertyNames().forEach(key -> valuesJ.addProperty(key, values.getProperty(key)));
|
||||||
|
json.add("values", valuesJ);
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
|
@ -48,9 +48,9 @@ public class I18nMessage {
|
||||||
return formatMessage();
|
return formatMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public I18nMessage value(String key, String value) {
|
public I18nMessage value(String key, Object value) {
|
||||||
DBC.INTERIM.assertNotEmpty("key must be set!", key);
|
DBC.INTERIM.assertNotEmpty("key must be set!", key);
|
||||||
this.values.setProperty(key, value == null ? "(null)" : value);
|
this.values.setProperty(key, value == null ? "(null)" : value.toString());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +69,10 @@ public class I18nMessage {
|
||||||
return this.message;
|
return this.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> T accept(I18nMessageVisitor<T> visitor) {
|
||||||
|
return visitor.visit(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
package li.strolch.utils;
|
||||||
|
|
||||||
|
public interface I18nMessageVisitor<T> {
|
||||||
|
|
||||||
|
T visit(I18nMessage message);
|
||||||
|
}
|
Loading…
Reference in New Issue