de.sayayi.lib.message.adopter.PropertiesAdopter Maven / Gradle / Ivy
Show all versions of message-format Show documentation
/*
* Copyright 2023 Jeroen Gremmen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.sayayi.lib.message.adopter;
import de.sayayi.lib.message.MessageFactory;
import de.sayayi.lib.message.MessageSupport.ConfigurableMessageSupport;
import de.sayayi.lib.message.MessageSupport.MessagePublisher;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
/**
* @author Jeroen Gremmen
* @since 0.8.0
*/
public class PropertiesAdopter extends AbstractMessageAdopter
{
/**
* Create a properties adopter for the given {@code configurableMessageSupport}.
*
* @param configurableMessageSupport configurable message support, not {@code null}
*/
public PropertiesAdopter(@NotNull ConfigurableMessageSupport configurableMessageSupport) {
super(configurableMessageSupport);
}
/**
* Create a properties adopter for the given {@code messageFactory} and {@code publisher}.
*
* @param messageFactory message factory, not {@code null}
* @param publisher message publisher, not {@code null}
*/
public PropertiesAdopter(@NotNull MessageFactory messageFactory,
@NotNull MessagePublisher publisher) {
super(messageFactory, publisher);
}
/**
* Adopt messages from properties.
*
* Each key from the properties object is used as the message code,
* the value is parsed as a message.
*
* @param properties message properties, not {@code null}
*/
@Contract(pure = true)
public void adopt(@NotNull Properties properties)
{
properties.forEach((k,v) -> messagePublisher.addMessage(
messageFactory.parseMessage(k.toString(), v.toString())));
}
/**
* Adopt messages from localized properties.
*
* Each key from the properties object is used as the message code,
* the value is parsed as a message. If the same message code is available in multiple
* property objects, a single message containing multiple localized messages is created for
* each message code.
*
* @param properties map with property messages keyed by locale, not {@code null}
*/
@Contract(pure = true)
public void adopt(@NotNull Map properties)
{
final Map> localizedMessagesByCode = new HashMap<>();
for(Entry entry: properties.entrySet())
{
final Locale locale = entry.getKey();
for(Entry