View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.util;
18  
19  import java.text.MessageFormat;
20  import java.util.MissingResourceException;
21  import java.util.ResourceBundle;
22  import java.util.concurrent.ConcurrentHashMap;
23  import java.util.concurrent.ConcurrentMap;
24  
25  /**
26   * Formats messages.
27   */
28  public final class Messages {
29  
30      /**
31       * Map from message code to MessageFormat object for the message.
32       */
33      private static ConcurrentMap<String, MessageFormat> messageMap = new ConcurrentHashMap<>();
34      private static final ResourceBundle RESOURCES = new CombinedResources("org.apache.commons.vfs2.Resources");
35  
36      private Messages() {
37      }
38  
39      /**
40       * Formats a message.
41       *
42       * @param code The message code.
43       * @return The formatted message.
44       */
45      public static String getString(final String code) {
46          return getString(code, new Object[0]);
47      }
48  
49      /**
50       * Formats a message.
51       *
52       * @param code The message code.
53       * @param param The message parameter.
54       * @return The formatted message.
55       * @deprecated Will be removed in 3.0 in favor of {@link #getString(String, Object[])} When removed, calls to this
56       *             method will automatically recompile to {@link #getString(String, Object[])}
57       */
58      @Deprecated
59      public static String getString(final String code, final Object param) {
60          return getString(code, new Object[] { param });
61      }
62  
63      /**
64       * Formats a message.
65       *
66       * @param code The message code.
67       * @param params The message parameters.
68       * @return The formatted message.
69       */
70      public static String getString(final String code, final Object... params) {
71          try {
72              if (code == null) {
73                  return null;
74              }
75  
76              final MessageFormat msg = findMessage(code);
77              return msg.format(params);
78          } catch (final MissingResourceException mre) {
79              return "Unknown message with code \"" + code + "\".";
80          }
81      }
82  
83      /**
84       * Locates a message by its code.
85       */
86      private static MessageFormat findMessage(final String code) throws MissingResourceException {
87          // Check if the message is cached
88          MessageFormat msg = messageMap.get(code);
89          if (msg != null) {
90              return msg;
91          }
92  
93          final String msgText = RESOURCES.getString(code);
94          msg = new MessageFormat(msgText);
95          messageMap.putIfAbsent(code, msg);
96          return messageMap.get(code);
97      }
98  }