View Javadoc

1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.internal.util;
17  
18  import java.util.AbstractMap;
19  import java.util.Collections;
20  import java.util.Enumeration;
21  import java.util.LinkedHashSet;
22  import java.util.ResourceBundle;
23  import java.util.Set;
24  
25  /**
26   * リソースバンドルをラップし、<code>Map</code> インターフェイスによって操作するクラスです。
27   * 
28   * @author baba
29   */
30  public class ResourceBundleMap extends AbstractMap<String, Object> {
31  
32  	/** ラップされた <code>ResourceBundle</code> */
33  	private final ResourceBundle resourceBundle;
34  
35  	/** エントリの <code>Set</code> */
36  	private Set<Entry<String, Object>> entrySet;
37  
38  	/**
39  	 * 指定されたリソースバンドルをラップする <code>ResourceBundleMap</code> を作成します。
40  	 * 
41  	 * @param resourceBundle
42  	 *            リソースバンドル
43  	 */
44  	public ResourceBundleMap(final ResourceBundle resourceBundle) {
45  		this.resourceBundle = resourceBundle;
46  	}
47  
48  	/**
49  	 * {@inheritDoc}
50  	 */
51  	@Override
52  	public Object get(final Object key) {
53  		return resourceBundle.getString((String) key);
54  	}
55  
56  	/**
57  	 * {@inheritDoc}
58  	 */
59  	@Override
60  	public Set<Entry<String, Object>> entrySet() {
61  		if (this.entrySet == null) {
62  			final Set<Entry<String, Object>> entrySet = new LinkedHashSet<Entry<String, Object>>();
63  			final Enumeration<String> keys = resourceBundle.getKeys();
64  			while (keys.hasMoreElements()) {
65  				final String key = keys.nextElement();
66  				final Object value = resourceBundle.getObject(key);
67  				final Entry<String, Object> entry = new UnmodifiableEntry<String, Object>(
68  						key, value);
69  				entrySet.add(entry);
70  			}
71  			this.entrySet = Collections.unmodifiableSet(entrySet);
72  		}
73  		return entrySet;
74  	}
75  
76  	/**
77  	 * 変更不可能な <code>Entry</code> の実装です。
78  	 * 
79  	 * @param <K>
80  	 *            このエントリが保持するキーの型
81  	 * @param <V>
82  	 *            このエントリが保持する値の型
83  	 * 
84  	 * @author baba
85  	 */
86  	private static class UnmodifiableEntry<K, V> implements Entry<K, V> {
87  
88  		/** キー。 */
89  		private final K key;
90  
91  		/** 値。 */
92  		private final V value;
93  
94  		/**
95  		 * 指定されたキーと値を持つエントリを作成します。
96  		 * 
97  		 * @param key
98  		 *            キー
99  		 * @param value
100 		 *            値
101 		 */
102 		public UnmodifiableEntry(final K key, final V value) {
103 			this.key = key;
104 			this.value = value;
105 		}
106 
107 		/**
108 		 * {@inheritDoc}
109 		 */
110 		public K getKey() {
111 			return key;
112 		}
113 
114 		/**
115 		 * {@inheritDoc}
116 		 */
117 		public V getValue() {
118 			return value;
119 		}
120 
121 		/**
122 		 * {@inheritDoc}
123 		 * <p>
124 		 * 常に {@link UnsupportedOperationException} をスローします。
125 		 * </p>
126 		 */
127 		public V setValue(final Object value) {
128 			throw new UnsupportedOperationException();
129 		}
130 
131 	}
132 
133 }