View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.internal.util;
18  
19  import java.util.AbstractMap;
20  import java.util.Collections;
21  import java.util.Enumeration;
22  import java.util.LinkedHashSet;
23  import java.util.ResourceBundle;
24  import java.util.Set;
25  
26  /**
27   * リソースバンドルをラップし、<code>Map</code> インターフェイスによって操作するクラスです。
28   * 
29   * @author baba
30   */
31  public class ResourceBundleMap extends AbstractMap<String, Object> {
32  
33  	/** ラップされた <code>ResourceBundle</code> */
34  	private final ResourceBundle resourceBundle;
35  
36  	/** エントリの <code>Set</code> */
37  	private Set<Entry<String, Object>> entrySet;
38  
39  	/**
40  	 * 指定されたリソースバンドルをラップする <code>ResourceBundleMap</code> を作成します。
41  	 * 
42  	 * @param resourceBundle
43  	 *            リソースバンドル
44  	 */
45  	public ResourceBundleMap(final ResourceBundle resourceBundle) {
46  		this.resourceBundle = resourceBundle;
47  	}
48  
49  	/**
50  	 * {@inheritDoc}
51  	 */
52  	@Override
53  	public Object get(final Object key) {
54  		return resourceBundle.getString((String) key);
55  	}
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	@Override
61  	public Set<Entry<String, Object>> entrySet() {
62  		if (this.entrySet == null) {
63  			final Set<Entry<String, Object>> entrySet = new LinkedHashSet<Entry<String, Object>>();
64  			final Enumeration<String> keys = resourceBundle.getKeys();
65  			while (keys.hasMoreElements()) {
66  				final String key = keys.nextElement();
67  				final Object value = resourceBundle.getObject(key);
68  				final Entry<String, Object> entry = new UnmodifiableEntry<String, Object>(
69  						key, value);
70  				entrySet.add(entry);
71  			}
72  			this.entrySet = Collections.unmodifiableSet(entrySet);
73  		}
74  		return entrySet;
75  	}
76  
77  	/**
78  	 * 変更不可能な <code>Entry</code> の実装です。
79  	 * 
80  	 * @param <K>
81  	 *            このエントリが保持するキーの型
82  	 * @param <V>
83  	 *            このエントリが保持する値の型
84  	 * 
85  	 * @author baba
86  	 */
87  	private static class UnmodifiableEntry<K, V> implements Entry<K, V> {
88  
89  		/** キー。 */
90  		private final K key;
91  
92  		/** 値。 */
93  		private final V value;
94  
95  		/**
96  		 * 指定されたキーと値を持つエントリを作成します。
97  		 * 
98  		 * @param key
99  		 *            キー
100 		 * @param value
101 		 *            値
102 		 */
103 		public UnmodifiableEntry(final K key, final V value) {
104 			this.key = key;
105 			this.value = value;
106 		}
107 
108 		/**
109 		 * {@inheritDoc}
110 		 */
111 		public K getKey() {
112 			return key;
113 		}
114 
115 		/**
116 		 * {@inheritDoc}
117 		 */
118 		public V getValue() {
119 			return value;
120 		}
121 
122 		/**
123 		 * {@inheritDoc}
124 		 * <p>
125 		 * 常に {@link UnsupportedOperationException} をスローします。
126 		 * </p>
127 		 */
128 		public V setValue(final Object value) {
129 			throw new UnsupportedOperationException();
130 		}
131 
132 	}
133 
134 }