001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.chain.web;
018
019import java.util.Map;
020import java.util.Objects;
021
022/**
023 * {@code Map.Entry} implementation that can be constructed to either be read-only
024 * or not.
025 *
026 * @version $Revision$ $Date$
027 *
028 * @param <T> type of the value
029 */
030public class MapEntry<T> implements Map.Entry<String, T> {
031
032    /**
033     * The entry key.
034     */
035    private final String key;
036
037    /**
038     * The entry value.
039     */
040    private T value;
041
042    /**
043     * Whether the entry can be modified.
044     */
045    private final boolean modifiable;
046
047    /**
048     * Creates a map entry that can either allow modifications or not.
049     *
050     * @param key The entry key
051     * @param value The entry value
052     * @param modifiable Whether the entry should allow modification or not
053     */
054    public MapEntry(String key, T value, boolean modifiable) {
055        this.key = key;
056        this.value = value;
057        this.modifiable = modifiable;
058    }
059
060    /**
061     * Gets the entry key.
062     *
063     * @return The entry key
064     */
065    @Override
066    public String getKey() {
067        return key;
068    }
069
070    /**
071     * Gets the entry value.
072     *
073     * @return The entry key
074     */
075    @Override
076    public T getValue() {
077        return value;
078    }
079
080    /**
081     * Sets the entry value if the entry can be modified.
082     *
083     * @param val The new value
084     *
085     * @return The old entry value
086     *
087     * @throws UnsupportedOperationException If the entry cannot be modified
088     */
089    @Override
090    public T setValue(T val) {
091        if (modifiable) {
092            T oldVal = this.value;
093            this.value = val;
094            return oldVal;
095        } else {
096            throw new UnsupportedOperationException();
097        }
098    }
099
100    /**
101     * Determines if this entry is equal to the passed object.
102     *
103     * @param o The object to test
104     *
105     * @return True if equal, else false
106     */
107    @Override
108    public boolean equals(Object o) {
109        if (this == o) {
110            return true;
111        }
112        if (o == null || getClass() != o.getClass()) {
113            return false;
114        }
115        final MapEntry<?> other = (MapEntry<?>) o;
116        return Objects.equals(key, other.key) && Objects.equals(value, other.value);
117    }
118
119    /**
120     * Returns the hashcode for this entry.
121     *
122     * @return The and'ed hashcode of the key and value
123     */
124    @Override
125    public int hashCode() {
126        return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue());
127    }
128}