AbstractMessageAwareFiller.java

  1. package fr.sii.ogham.core.filler;

  2. import java.util.Map;

  3. import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilderHelper;
  4. import fr.sii.ogham.core.exception.filler.FillMessageException;
  5. import fr.sii.ogham.core.message.Message;

  6. /**
  7.  * Base class to help to fill a particular {@link Message} type.
  8.  *
  9.  * @author AurĂ©lien Baudet
  10.  *
  11.  * @param <M>
  12.  *            the type of the message that the filler is able to handle
  13.  */
  14. public abstract class AbstractMessageAwareFiller<M> implements MessageFiller {
  15.     protected final Map<String, ConfigurationValueBuilderHelper<?, ?>> defaultValues;
  16.     private final Class<M> messageType;

  17.     /**
  18.      * The list of properties is indexed by an alias that is known by the
  19.      * implementation. For example, if the keys is defined like this:
  20.      *
  21.      * <pre>
  22.      * Map&lt;String, List&lt;String&gt;&gt; keys = new HashMap&lt;&gt;();
  23.      * keys.put("to", valueBuilder.properties("ogham.email.to.default-value"));
  24.      * keys.put("from", valueBuilder.properties("ogham.email.from.default-value", "mail.smtp.from"));
  25.      * </pre>
  26.      *
  27.      * The implementation can then retrieve real property value using map key
  28.      * (alias):
  29.      *
  30.      * <pre>
  31.      * getProperty("from");
  32.      * // will return either the value of "ogham.email.from.default-value" or
  33.      * // "mail.smtp.from"
  34.      * </pre>
  35.      *
  36.      * @param defaultValues
  37.      *            a list of property keys indexed by an alias
  38.      * @param messageType
  39.      *            the class of the message that this implementation can handle
  40.      */
  41.     protected AbstractMessageAwareFiller(Map<String, ConfigurationValueBuilderHelper<?, ?>> defaultValues, Class<M> messageType) {
  42.         super();
  43.         this.defaultValues = defaultValues;
  44.         this.messageType = messageType;
  45.     }

  46.     @SuppressWarnings("unchecked")
  47.     @Override
  48.     public void fill(Message message) throws FillMessageException {
  49.         if (messageType.isAssignableFrom(message.getClass())) {
  50.             fill((M) message);
  51.         }
  52.     }

  53.     protected abstract void fill(M message);

  54.     /**
  55.      * Return whether the given property alias has at least one property key
  56.      * that is available for resolution, i.e., the value for the given key is
  57.      * not {@code null}.
  58.      *
  59.      * @param alias
  60.      *            the property alias to resolve
  61.      * @return true if property exists, false otherwise
  62.      */
  63.     protected boolean containsProperty(String alias) {
  64.         ConfigurationValueBuilderHelper<?, ?> valueBuilder = defaultValues.get(alias);
  65.         return valueBuilder != null && valueBuilder.getValue() != null;
  66.     }

  67.     /**
  68.      * Returns the value of first property represented by the provided alias
  69.      * that has a value (not {@code null}).
  70.      *
  71.      * @param alias
  72.      *            the property alias to resolve
  73.      * @param valueClass
  74.      *            the class of the resulting value
  75.      * @param <T>
  76.      *            the type of the resulting value
  77.      * @return the property value or null
  78.      */
  79.     @SuppressWarnings("unchecked")
  80.     protected <T> T getProperty(String alias, Class<T> valueClass) {
  81.         ConfigurationValueBuilderHelper<?, T> valueBuilder = (ConfigurationValueBuilderHelper<?, T>) defaultValues.get(alias);
  82.         return valueBuilder == null ? null : valueBuilder.getValue();
  83.     }
  84. }