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  import org.apache.commons.lang3.ArrayUtils;
26  
27  /**
28   * Formats messages.
29   */
30  public final class Messages {
31  
32      /**
33       * Map from message code to MessageFormat object for the message.
34       */
35      private static final ConcurrentMap<String, MessageFormat> messageMap = new ConcurrentHashMap<>();
36      private static final ResourceBundle RESOURCES = new CombinedResources("org.apache.commons.vfs2.Resources");
37  
38      /**
39       * Locates a message by its code.
40       */
41      private static MessageFormat findMessage(final String code) throws MissingResourceException {
42          // Check if the message is cached
43          return messageMap.computeIfAbsent(code, k -> new MessageFormat(RESOURCES.getString(k)));
44      }
45  
46      /**
47       * Formats a message.
48       *
49       * @param code The message code.
50       * @return The formatted message.
51       */
52      public static String getString(final String code) {
53          return getString(code, ArrayUtils.EMPTY_OBJECT_ARRAY);
54      }
55  
56      /**
57       * Formats a message.
58       *
59       * @param code The message code.
60       * @param param The message parameter.
61       * @return The formatted message.
62       * @deprecated Will be removed in 3.0 in favor of {@link #getString(String, Object[])} When removed, calls to this
63       *             method will automatically recompile to {@link #getString(String, Object[])}
64       */
65      @Deprecated
66      public static String getString(final String code, final Object param) {
67          return getString(code, new Object[] { param });
68      }
69  
70      /**
71       * Formats a message.
72       *
73       * @param code The message code.
74       * @param params The message parameters.
75       * @return The formatted message.
76       */
77      public static String getString(final String code, final Object... params) {
78          try {
79              if (code == null) {
80                  return null;
81              }
82              return findMessage(code).format(params);
83          } catch (final MissingResourceException mre) {
84              return "Unknown message with code \"" + code + "\".";
85          }
86      }
87  
88      private Messages() {
89          // no instances.
90      }
91  }