001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.util;
029
030import org.opencms.jsp.util.CmsJspContentAccessValueWrapper;
031
032import java.util.Collection;
033import java.util.Collections;
034import java.util.List;
035import java.util.Map;
036import java.util.Set;
037
038/**
039 * Returns the constant Object the map was initialized with for all {@link #get(Object)} calls,
040 * regardless of what the actual key is.<p>
041 *
042 * @since 7.0.2
043 *
044 * @param <K> the type of keys maintained by this map
045 * @param <V> the type of mapped values
046 */
047@SuppressWarnings("unchecked")
048public class CmsConstantMap<K, V> implements Map<K, V> {
049
050    /** Constant Map that always returns {@link Boolean#FALSE}.*/
051    public static final Map<String, Boolean> CONSTANT_BOOLEAN_FALSE_MAP = new CmsConstantMap<String, Boolean>(
052        Boolean.FALSE);
053
054    /** Constant Map that always returns {@link Boolean#TRUE}.*/
055    public static final Map<String, Boolean> CONSTANT_BOOLEAN_TRUE_MAP = new CmsConstantMap<String, Boolean>(
056        Boolean.TRUE);
057
058    /** Constant Map that always returns an empty list. */
059    public static final Map<String, List<CmsJspContentAccessValueWrapper>> CONSTANT_EMPTY_LIST_MAP = new CmsConstantMap<String, List<CmsJspContentAccessValueWrapper>>(
060        Collections.EMPTY_LIST);
061
062    /** Constant Map that always returns an empty list. */
063    public static final Map<String, String> CONSTANT_EMPTY_STRING_MAP = new CmsConstantMap<String, String>("".intern());
064
065    /** The constant Object this map always returns. */
066    private V m_constant;
067
068    /**
069     * Creates a new constant Map.<p>
070     *
071     * @param constant the constant to return for all {@link #get(Object)} calls.<p>
072     */
073    public CmsConstantMap(V constant) {
074
075        m_constant = constant;
076    }
077
078    /**
079     * @see java.util.Map#clear()
080     */
081    public void clear() {
082
083        // NOOP
084    }
085
086    /**
087     * @see java.util.Map#containsKey(java.lang.Object)
088     */
089    public boolean containsKey(Object key) {
090
091        // this Map contains all keys, since all keys return the same value
092        return true;
093    }
094
095    /**
096     * @see java.util.Map#containsValue(java.lang.Object)
097     */
098    public boolean containsValue(Object value) {
099
100        return m_constant.equals(value);
101    }
102
103    /**
104     * @see java.util.Map#entrySet()
105     */
106    public Set<Map.Entry<K, V>> entrySet() {
107
108        return Collections.emptySet();
109    }
110
111    /**
112     * @see java.lang.Object#equals(java.lang.Object)
113     */
114    @Override
115    public boolean equals(Object obj) {
116
117        if (obj == this) {
118            return true;
119        }
120        if (obj instanceof CmsConstantMap) {
121            return m_constant.equals(((CmsConstantMap<?, ?>)obj).m_constant);
122        }
123        return false;
124    }
125
126    /**
127     * @see java.util.Map#get(java.lang.Object)
128     */
129    public V get(Object key) {
130
131        return m_constant;
132    }
133
134    /**
135     * @see java.lang.Object#hashCode()
136     */
137    @Override
138    public int hashCode() {
139
140        return m_constant.hashCode();
141    }
142
143    /**
144     * @see java.util.Map#isEmpty()
145     */
146    public boolean isEmpty() {
147
148        return false;
149    }
150
151    /**
152     * @see java.util.Map#keySet()
153     */
154    public Set<K> keySet() {
155
156        return Collections.emptySet();
157    }
158
159    /**
160     * @see java.util.Map#put(java.lang.Object, java.lang.Object)
161     */
162    public V put(Object arg0, Object arg1) {
163
164        return null;
165    }
166
167    /**
168     * @see java.util.Map#putAll(java.util.Map)
169     */
170    public void putAll(Map<? extends K, ? extends V> arg0) {
171
172        // NOOP
173    }
174
175    /**
176     * @see java.util.Map#remove(java.lang.Object)
177     */
178    public V remove(Object key) {
179
180        return m_constant;
181    }
182
183    /**
184     * @see java.util.Map#size()
185     */
186    public int size() {
187
188        return 0;
189    }
190
191    /**
192     * @see java.util.Map#values()
193     */
194    public Collection<V> values() {
195
196        return Collections.singletonList(m_constant);
197    }
198}