001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.util; 018 019import java.text.MessageFormat; 020import java.util.MissingResourceException; 021import java.util.ResourceBundle; 022import java.util.concurrent.ConcurrentHashMap; 023import java.util.concurrent.ConcurrentMap; 024 025import org.apache.commons.lang3.ArrayUtils; 026 027/** 028 * Formats messages. 029 */ 030public final class Messages { 031 032 /** 033 * Map from message code to MessageFormat object for the message. 034 */ 035 private static final ConcurrentMap<String, MessageFormat> MESSAGE_MAP = new ConcurrentHashMap<>(); 036 private static final ResourceBundle RESOURCES = new CombinedResources("org.apache.commons.vfs2.Resources"); 037 038 /** 039 * Locates a message by its code. 040 */ 041 private static MessageFormat findMessage(final String code) throws MissingResourceException { 042 // Check if the message is cached 043 return MESSAGE_MAP.computeIfAbsent(code, k -> new MessageFormat(RESOURCES.getString(k))); 044 } 045 046 /** 047 * Formats a message. 048 * 049 * @param code The message code. 050 * @return The formatted message. 051 */ 052 public static String getString(final String code) { 053 return getString(code, ArrayUtils.EMPTY_OBJECT_ARRAY); 054 } 055 056 /** 057 * Formats a message. 058 * 059 * @param code The message code. 060 * @param param The message parameter. 061 * @return The formatted message. 062 * @deprecated Will be removed in 3.0 in favor of {@link #getString(String, Object[])} When removed, calls to this 063 * method will automatically recompile to {@link #getString(String, Object[])} 064 */ 065 @Deprecated 066 public static String getString(final String code, final Object param) { 067 return getString(code, new Object[] {param}); 068 } 069 070 /** 071 * Formats a message. 072 * 073 * @param code The message code. 074 * @param params The message parameters. 075 * @return The formatted message. 076 */ 077 public static String getString(final String code, final Object... params) { 078 try { 079 if (code == null) { 080 return null; 081 } 082 return findMessage(code).format(params); 083 } catch (final MissingResourceException mre) { 084 return "Unknown message with code \"" + code + "\"."; 085 } 086 } 087 088 private Messages() { 089 // no instances. 090 } 091}