All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.toolkit.util.resourcebundle.MessageBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.toolkit.util.resourcebundle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import com.alibaba.toolkit.util.StringUtil;

/**
 * 通过资源束创建消息的工具类, 支持所有原子类型, 方便使用.
 * 

* 使用方法: *

*

 * String message = new MessageBuilder(bundle, key).append(param1).append(param2).toString();
 * 
*

*

*

* 在构造此类时, 可以提供一个quiet参数. 如果此参数为true, 并且resource * bundle找不到, 则不会抛出MissingResourceException, 而是返回一个默认的字符串. *

* * @author Michael Zhou * @version $Id: MessageBuilder.java,v 1.1 2003/07/03 07:26:35 baobao Exp $ */ public class MessageBuilder { protected final List params = new ArrayList(5); protected final ResourceBundle bundle; protected final Object key; /** * 创建一个MessageBuilder. * * @param bundleName 资源束 * @param key 键值 * @throws MissingResourceException 指定bundle未找到, 或创建bundle错误 */ public MessageBuilder(String bundleName, Object key) { this(ResourceBundleFactory.getBundle(bundleName), key); } /** * 创建一个MessageBuilder. * * @param bundle 资源束 * @param key 键值 */ public MessageBuilder(ResourceBundle bundle, Object key) { this.bundle = bundle; this.key = key; } /** * 增加一个参数. * * @param param 参数 * @return MessageBuilder自身 */ public MessageBuilder append(Object param) { params.add(param); return this; } /** * 增加一个参数. * * @param param 参数 * @return MessageBuilder自身 */ public MessageBuilder append(boolean param) { params.add(new Boolean(param)); return this; } /** * 增加一个参数. * * @param param 参数 * @return MessageBuilder自身 */ public MessageBuilder append(char param) { params.add(new Character(param)); return this; } /** * 增加一个参数. * * @param param 参数 * @return MessageBuilder自身 */ public MessageBuilder append(double param) { params.add(new Double(param)); return this; } /** * 增加一个参数. * * @param param 参数 * @return MessageBuilder自身 */ public MessageBuilder append(float param) { params.add(new Float(param)); return this; } /** * 增加一个参数. * * @param param 参数 * @return MessageBuilder自身 */ public MessageBuilder append(int param) { params.add(new Integer(param)); return this; } /** * 增加一个参数. * * @param param 参数 * @return MessageBuilder自身 */ public MessageBuilder append(long param) { params.add(new Long(param)); return this; } /** * 增加多个参数. * * @param params 参数表 * @return MessageBuilder自身 */ public MessageBuilder append(Object[] params) { if (params != null) { this.params.addAll(Arrays.asList(params)); } return this; } /** * 取得消息字符串. * * @return 消息字符串 */ @Override public String toString() { return getMessage(); } /** * 从资源束中取得消息字符串. * * @return 消息字符串 * @throws MissingResourceException 指定resource key未找到 */ protected String getMessage() { return StringUtil.getMessage(bundle, key, params.toArray()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy