001 // Copyright 2004, 2005 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
015 package org.apache.hivemind.impl;
016
017 import java.util.AbstractMap;
018 import java.util.Map;
019 import java.util.Set;
020
021 /**
022 * Implements a {@link java.util.Map} as a proxy to an actual map of elements, provided by an
023 * extension point. The proxy is unmodifiable and will work with the extension point to generate the
024 * real map of elements in a just-in-time manner.
025 *
026 * @author Knut Wannheden
027 * @since 1.1
028 */
029 public final class ElementsInnerProxyMap extends AbstractMap
030 {
031 private Map _inner;
032
033 private ConfigurationPointImpl _point;
034
035 private ElementsProxyMap _outer;
036
037 ElementsInnerProxyMap(ConfigurationPointImpl point, ElementsProxyMap outer)
038 {
039 _point = point;
040 _outer = outer;
041
042 _outer.setInner(this);
043 }
044
045 public Set entrySet()
046 {
047 return inner().entrySet();
048 }
049
050 private synchronized Map inner()
051 {
052 if (_inner == null)
053 {
054 _inner = _point.constructMapElements();
055
056 // Replace ourselves in the outer proxy with the actual list.
057 _outer.setInner(_inner);
058 }
059
060 return _inner;
061 }
062
063 public boolean equals(Object o)
064 {
065 if (this == o)
066 return true;
067
068 if (o == null)
069 return false;
070
071 return inner().equals(o);
072 }
073
074 public int hashCode()
075 {
076 return inner().hashCode();
077 }
078
079 public synchronized String toString()
080 {
081 if (_inner != null)
082 return _inner.toString();
083
084 return "<Element Map Proxy for " + _point.getExtensionPointId() + ">";
085 }
086
087 }