View Javadoc

1   /*
2    * Copyright 2007-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.exceptions.i18n.message;
17  
18  import java.io.Serializable;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.commons.lang.StringUtils;
23  
24  /**
25   * Provides the messages that are set.
26   */
27  public final class Messages implements Serializable
28  {
29    // ********************************* Fields *********************************
30  
31    // --- constants ------------------------------------------------------------
32  
33    /**
34     * The class version identifier.
35     * <p>
36     * The value of this constant is {@value}.
37     * </p>
38     */
39    private static final long serialVersionUID = 1L;
40  
41    // --- members --------------------------------------------------------------
42  
43    /**
44     * The map of message texts. The value is the rendered message for the given
45     * message type. The message may be <code>null</code>.
46     *
47     * @serial
48     */
49    private final Map<MessageType, String> messageMap;
50  
51    // ****************************** Initializer *******************************
52  
53    // ****************************** Constructors ******************************
54  
55    /**
56     * Default constructor.
57     */
58    private Messages(final Builder builder)
59    {
60      this.messageMap = new HashMap<MessageType, String>(builder.messageMap);
61    }
62  
63    // ****************************** Inner Classes *****************************
64  
65    /**
66     * The builder for message instances.
67     */
68    public static final class Builder
69    {
70      // ******************************** Fields ********************************
71  
72      // --- constants ----------------------------------------------------------
73  
74      // --- members ------------------------------------------------------------
75  
76      /**
77       * The map of message texts. The value is the rendered message for the given
78       * message type.
79       */
80      private final Map<MessageType, String> messageMap =
81          new HashMap<MessageType, String>();
82  
83      // ***************************** Initializer ******************************
84  
85      // ***************************** Constructors *****************************
86  
87      // ***************************** Inner Classes ****************************
88  
89      // ******************************** Methods *******************************
90  
91      // --- init ---------------------------------------------------------------
92  
93      // --- get&set ------------------------------------------------------------
94  
95      // --- business -----------------------------------------------------------
96  
97      /**
98       * Adds the given message as the given type.
99       *
100      * @param type the type of message.
101      * @param message the message to add.
102      * @return a reference to the builder.
103      */
104     public Builder put(final MessageType type, final String message)
105     {
106       messageMap.put(type, message);
107       return this;
108     }
109 
110     /**
111      * Creates the messages instance.
112      *
113      * @return the created instance.
114      */
115     public Messages build()
116     {
117       return new Messages(this);
118     }
119 
120     // --- object basics ------------------------------------------------------
121   }
122 
123   // ********************************* Methods ********************************
124 
125   // --- init -----------------------------------------------------------------
126 
127   // --- get&set --------------------------------------------------------------
128 
129   // --- business -------------------------------------------------------------
130 
131   /**
132    * Checks if there is a message for the given type.
133    *
134    * @param type the type of message being requested.
135    * @return <code>true</code> if there is a non-<code>null</code> message,
136    *         provided, <code>false</code> otherwise.
137    */
138   public boolean hasMessage(final MessageType type)
139   {
140     return null != messageMap.get(type);
141   }
142 
143   /**
144    * Returns the message of the given type.
145    *
146    * @param type the type of message being requested.
147    * @return the message text for the given type. May be <code>null</code>.
148    */
149   public String getMessage(final MessageType type)
150   {
151     return messageMap.get(type);
152   }
153 
154   // --- object basics --------------------------------------------------------
155 
156   @Override
157   public String toString()
158   {
159     final StringBuilder buffer = new StringBuilder(256);
160     for (final MessageType type : MessageType.values())
161     {
162       final String message = getMessage(type);
163       if (message != null)
164       {
165         buffer.append(type).append('=').append(message).append('\n');
166       }
167     }
168     return StringUtils.chop(buffer.toString());
169   }
170 }