ResourceBuddle之疑惑

package com.test.javaSe;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListResourceBundle;
import java.util.Map;
import java.util.Set;

class KeyValue {
	Object key;
	Object value;

	public KeyValue() {
	}

	public KeyValue(Object key, Object value) {
		this.key = key;
		this.value = value;
	}

	public String toString() {
		return this.key + "=" + this.value;
	}
}

/**
 * @deprecated \u8BE5\u5DE5\u5177\u7C7B\u501F\u53E3\u5B9A\u4E49\u4E86\u5411\u8D44\u6E90\u7C7B\u4E2D\u6DFB\u52A0\uFF0C\u5220\u9664\uFF0C\u5224\u65AD\u65F6\u5019\u5305\u542Bkey\uFF0C\u83B7\u53D6\u6240\u6709\u7684key\uFF0C\u83B7\u53D6\u6240\u6709\u7684value\uFF0C\u7B49\u65B9\u6CD5
 * @author Administrator
 */
interface ResourceBundleUtil {

	public List<Object> keyList = new ArrayList<Object>();
	public List<Object> valueList = new ArrayList<Object>();
	public static Map<Object, Object> keyValue = new HashMap<Object, Object>();
	public static Object data[][] = null;

	/**
	 * @deprecated \u8BE5\u65B9\u6CD5\u7528\u4E8E\u5411\u8D44\u6E90\u7C7B\u4E2D\u6DFB\u52A0\u65B0\u7684\u6570\u636E
	 * @param kv
	 */
	public void add(KeyValue kv);

	/**
	 * @deprecated \u8BE5\u65B9\u6CD5\u7528\u4E8E\u5411\u8D44\u6E90\u7C7B\u4E2D\u6DFB\u52A0\u65B0\u7684\u6570\u636E
	 * @param kv
	 */
	public void add(Object key, Object value);

	/**
	 * @deprecated \u5C06map\u4E2D\u7684\u6570\u636E\u8F6C\u6362\u6210Object\u6570\u7EC4
	 * @return \u8FD4\u56DEdate
	 */
	public Object[][] getData();

	/**
	 * @deprecated \u8BE5\u65B9\u6CD5\u7528\u4E8E\u5411\u8D44\u6E90\u7C7B\u4E2D\u5220\u9664\u76F8\u5BF9\u5E94\u7684key\u7684\u6570\u636E
	 * @param key
	 */
	public void delete(Object key);

	/**
	 * @deprecated \u5224\u65AD\u8BE5\u8D44\u6E90\u7C7B\u4E2D\u662F\u5426\u5305\u542B\u67D0key
	 * @param key
	 * @return \u5982\u679C\u5305\u542Bkey\u8FD4\u56DEtrue\u5426\u5219\u8FD4\u56DEfalse
	 */
	public boolean isContains(Object key);

	/**
	 * @deprecated \u5F97\u5230\u6240\u6709\u7684key\u8FD4\u56DE\u7C7B\u578B\u4E3Alist
	 * @return \u6240\u6709\u7684key
	 */
	public ArrayList<Object> getKey();

	/**
	 * @deprecated \u5F97\u5230\u6240\u6709\u7684value \u8FD4\u56DE\u7684\u662Flist
	 * @return \u6240\u6709\u7684value
	 */
	public ArrayList<Object> getValues();

}

/**
 * @deprecated \u8BE5\u5DE5\u5177\u7C7B\u5B9A\u4E49\u4E86\u5411\u8D44\u6E90\u7C7B\u4E2D\u6DFB\u52A0\uFF0C\u5220\u9664\uFF0C\u5224\u65AD\u65F6\u5019\u5305\u542Bkey\uFF0C\u83B7\u53D6\u6240\u6709\u7684key\uFF0C\u83B7\u53D6\u6240\u6709\u7684value\uFF0C\u7B49\u65B9\u6CD5
 * @author Administrator
 * @deprecated \u8BE5\u7C7B\u6709\u70B9\u602A\u5F02\uFF0C\u602A\u5F02\u5728\u4E8E
 * @deprecated(1) \u4E0A\u53E5\u8BDD\u6267\u884C\u540Emap\u5DF2\u7ECF\u4E3A\u7A7A \u4E14\u5F53\u6267\u884C\u8FC7 containsKey()\u65B9\u6CD5\u540E
 *                MessageFormat.format(rbui.getString("ctl"), "-->\u8FD8\u597D\u4E86");
 *                MessageFormat.format(rbui.getString("hjj"), "-->henan"); \u8FD8\u53EF\u4EE5\u6267\u884C
 *                \u5982\u679CcontainsKey()\u6B64\u65B9\u6CD5\u6CA1\u6709\u6267\u884C
 *                \u5C31\u6267\u884CMessageFormat.format(rbui.getString("ctl"), "-->\u8FD8\u597D\u4E86");
 *                MessageFormat.format(rbui.getString("hjj"), "-->henan"); \u5219\u4F1A\u6709\u5F02\u5E38
 * @deprecated(2)\u5982\u679C\u5728\u6CA1\u6709\u6267\u884CcontainsKey()\u65B9\u6CD5\u4E14\u6267\u884CMessageFormat.format\u540E\u5728\u6267\u884Cdelete\u64CD\u4F5C\uFF0C\u518D\u6267\u884C 
 *               MessageFormat.format()
 *               \u867D\u7136Map\u4E2D\u5DF2\u7ECF\u4E3A\u7A7A\uFF0C\u4F46\u662F\u6570\u636E\u6267\u884C\u7684\u7ED3\u679C\u4F9D\u65E7\u662F\u6CA1\u6709\u6267\u884Cdelete\u7684\u7ED3\u679C
 *               \u4E4B\u6240\u4EE5\u53EF\u4EE5\u76F4\u63A5\u4F7F\u7528ResourceBundleUtilImpl\u7684\u5BF9\u8C61\u8C03\u7528getString\u65B9\u6CD5
 *               \u662F\u56E0\u4E3AResourceBundleUtilImpl extends ListResourceBundle extends ResourceBundle
 *               \u800CgetString\u53C8\u662FResourceBuddle\u7684\u65B9\u6CD5 ,\u6240\u4EE5ResourceBundleUtilImpl
 *               \u7684\u5BF9\u8C61\u53EF\u4EE5\u76F4\u63A5\u4F7F\u7528
 * @deprecated(3)\u4E2A\u4EBA\u8BA4\u4E3A\u672C\u7C7B\u7D2F\u6B7Bproperties\u6587\u4EF6\uFF0C\u6BCF\u6B21\u4F7F\u7528\u65F6\u5FC5\u987B\u91CD\u65B0\u52A0\u8F7D\uFF0C\u5728\u4F7F\u7528\u8FC7\u7A0B\u4E2D\u5BF9\u4E4B\u8FDB\u884C\u4FEE\u6539\u4E0D\u51D1\u6548
 */

class ResourceBundleUtilImpl extends ListResourceBundle implements
		ResourceBundleUtil {

	/**
	 * @deprecated \u8BE5\u65B9\u6CD5\u7528\u4E8E\u5411\u8D44\u6E90\u7C7B\u4E2D\u6DFB\u52A0\u65B0\u7684\u6570\u636E
	 * @param kv
	 */
	public void add(KeyValue kv) {
		keyValue.put(kv.key, kv.value);
	}

	/**
	 * @deprecated \u8BE5\u65B9\u6CD5\u7528\u4E8E\u5411\u8D44\u6E90\u7C7B\u4E2D\u5220\u9664\u76F8\u5BF9\u5E94\u7684key\u7684\u6570\u636E
	 * @param key
	 */
	public void delete(Object key) {
		keyValue.remove(key);
	}

	/**
	 * @deprecated \u5224\u65AD\u8BE5\u8D44\u6E90\u7C7B\u4E2D\u662F\u5426\u5305\u542B\u67D0key
	 * @param key
	 * @return \u5982\u679C\u5305\u542Bkey\u8FD4\u56DEtrue\u5426\u5219\u8FD4\u56DEfalse
	 */
	public boolean isContains(Object key) {
		// Set<Object> set = new HashSet<Object>();
		// set = keyValue.keySet();
		// Iterator<Object> iter = set.iterator();
		// Object temp=null;
		// for (; iter.hasNext();) {
		// temp= iter.next();
		// if(temp.equals(key))
		// return true;
		// }
		return keyValue.containsKey(key);
	}

	protected Object[][] getContents() {
		// System.out.println("\u8FDB\u5165getContents()");
		return getData();
	}

	/**
	 * @deprecated \u5F97\u5230\u6240\u6709\u7684value \u8FD4\u56DE\u7684\u662Flist
	 * @return \u6240\u6709\u7684value
	 */
	public ArrayList<Object> getValues() {
		ArrayList<Object> values = new ArrayList<Object>();
		Set<Object> set = new HashSet<Object>();
		set = keyValue.keySet();
		Iterator<Object> iter = set.iterator();
		Object temp = null;
		for (; iter.hasNext();) {
			temp = iter.next();
			values.add(keyValue.get(temp));
		}
		return values;
	}

	/**
	 * @deprecated \u5F97\u5230\u6240\u6709\u7684key\u8FD4\u56DE\u7C7B\u578B\u4E3Alist
	 * @return \u6240\u6709\u7684key
	 */
	public ArrayList<Object> getKey() {
		ArrayList<Object> keys = new ArrayList<Object>();
		Set<Object> set = new HashSet<Object>();
		set = keyValue.keySet();
		Iterator<Object> iter = set.iterator();
		Object temp = null;
		for (; iter.hasNext();) {
			temp = iter.next();
			keys.add(temp);
		}
		return keys;
	}

	/**
	 * @deprecated \u5C06map\u4E2D\u7684\u6570\u636E\u8F6C\u6362\u6210Object\u6570\u7EC4
	 * @return \u8FD4\u56DEdate
	 */
	public Object[][] getData() {
		// System.out.println("\u8FDB\u5165getData()");
		int size = keyValue.size();
		if (size == 0) {
			System.out.println("\u6CA1\u6709\u6570\u636E  \u8FD4\u56DEnull");
			return null;
		}
		// System.out.println("keyValue.size()=" + size);
		Object data[][] = new Object[size][];
		Set<Object> set = new HashSet<Object>();
		set = keyValue.keySet();
		Iterator<Object> iter = set.iterator();
		Object key = null;
		Object value = null;
		int num = 0;
		for (; iter.hasNext();) {
			key = iter.next();
			value = keyValue.get(key);
			data[num] = new Object[2];
			data[num][0] = key;
			data[num][1] = value;
			num++;
		}
		return data;
	}

	/**
	 * @deprecated \u8BE5\u65B9\u6CD5\u7528\u4E8E\u5411\u8D44\u6E90\u7C7B\u4E2D\u6DFB\u52A0\u65B0\u7684\u6570\u636E
	 * @param key
	 * @param value
	 */
	public void add(Object key, Object value) {
		keyValue.put(key, value);
	}

}

public class UseClassReplaceResourceBundle_zh_CN03 {
	public static void main(String[] args) {
		ResourceBundleUtilImpl rbui = new ResourceBundleUtilImpl();
		rbui.add(new KeyValue("ctl", "\u4F60\u8FD8\u597D\u5417?{0}"));
		rbui.add(new KeyValue("hjj", "where are you ?{0}"));
		// System.out.println(rbui.containsKey("ctl"));//true
		// System.out.println(rbui.containsKey("ctllin"));//false
		try {
			String name = MessageFormat.format(rbui.getString("ctl"), "-->\u8FD8\u597D\u4E86");
			String value1 = MessageFormat.format(rbui.getString("hjj"),
					"-->henan");
			System.out.println(name);
			System.out.println(value1);
		} catch (Exception e) {
			System.err.println("\u5173\u952E\u5B57\u4E0D\u518D\u6570\u636E\u4E2D");
		}

		rbui.delete("ctl");
		rbui.delete("hjj");
		try {
			String name = MessageFormat.format(rbui.getString("ctl"), "-->\u8FD8\u597D\u4E86");
			String value1 = MessageFormat.format(rbui.getString("hjj"),
					"-->henan");
			System.out.println(name);
			System.out.println(value1);
		} catch (Exception e) {
			System.err.println("\u5173\u952E\u5B57\u4E0D\u518D\u6570\u636E\u4E2D");
		}
		ResourceBundleUtilImpl obj = new ResourceBundleUtilImpl();
		obj.add("name","ctl{0}");
		obj.add("age","23{0}");
		obj.delete("name");
		obj.delete("age");
		try {
			System.out.println(obj.getString("name"));
			System.out.println(obj.getString("age"));
		} catch (Exception e) {
			System.err.println("\u5173\u952E\u5B57\u4E0D\u518D\u6570\u636E\u4E2D (\u6CA1\u6709\u8C03\u7528containsKey)");
		}
		
		ResourceBundleUtilImpl obj1 = new ResourceBundleUtilImpl();
		obj1.add("name","ctl{0},{1}");
		obj1.add("age","23{0},{1}");
		obj.containsKey("name");
		obj1.delete("name");
		obj1.delete("age");
		try {
			System.out.println(obj.getString("name"));
			System.out.println(obj.getString("age"));
		} catch (Exception e) {
			System.err.println("\u5173\u952E\u5B57\u4E0D\u518D\u6570\u636E\u4E2D  (\u8C03\u7528containsKey)");
		}
	}
}

你可能感兴趣的:(ResourceBuddle之疑惑)