001// Copyright 2006, 2009, 2011 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.ioc.util; 016 017import org.apache.tapestry5.ioc.MessageFormatter; 018import org.apache.tapestry5.ioc.Messages; 019import org.apache.tapestry5.ioc.internal.util.CollectionFactory; 020import org.apache.tapestry5.ioc.internal.util.MessageFormatterImpl; 021 022import java.util.Locale; 023import java.util.Map; 024 025/** 026 * Abstract implementation of {@link Messages} that doesn't know where values come from (that information is supplied in 027 * a subclass, via the {@link #valueForKey(String)} method). 028 */ 029public abstract class AbstractMessages implements Messages 030{ 031 /** 032 * String key to MF instance. 033 */ 034 private final Map<String, MessageFormatter> cache = CollectionFactory.newConcurrentMap(); 035 036 private final Locale locale; 037 038 protected AbstractMessages(Locale locale) 039 { 040 this.locale = locale; 041 } 042 043 /** 044 * Invoked to provide the value for a particular key. This may be invoked multiple times even for the same key. The 045 * implementation should <em>ignore the case of the key</em>. 046 * 047 * @param key the key to obtain a value for (case insensitive) 048 * @return the value for the key, or null if this instance can not provide the value 049 */ 050 protected abstract String valueForKey(String key); 051 052 053 @Override 054 public boolean contains(String key) 055 { 056 return valueForKey(key) != null; 057 } 058 059 @Override 060 public String get(String key) 061 { 062 if (contains(key)) return valueForKey(key); 063 064 return String.format("[[missing key: %s]]", key); 065 } 066 067 @Override 068 public MessageFormatter getFormatter(String key) 069 { 070 MessageFormatter result = cache.get(key); 071 072 if (result == null) 073 { 074 result = buildMessageFormatter(key); 075 cache.put(key, result); 076 } 077 078 return result; 079 } 080 081 private MessageFormatter buildMessageFormatter(String key) 082 { 083 String format = get(key); 084 085 return new MessageFormatterImpl(format, locale); 086 } 087 088 @Override 089 public String format(String key, Object... args) 090 { 091 return getFormatter(key).format(args); 092 } 093 094}