001package org.apache.turbine.util.uri;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Objects;
023
024import org.apache.commons.lang3.StringUtils;
025
026/**
027 * Helper Class to keep a key and a value together in
028 * one object. Used for URI Parameters
029 *
030 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
031 * @version $Id$
032 */
033
034public class URIParam
035{
036    /** Key */
037    private String key = null;
038
039    /** Value */
040    private Object value = null;
041
042    /**
043     * Creates a new Object from Key and Value
044     *
045     * @param key A String with the Param Name.
046     * @param value An Object with the Value.
047     *
048     */
049    public URIParam(String key, Object value)
050    {
051        this.key = key;
052        this.value = value;
053    }
054
055    /**
056     * Returns the key.
057     *
058     * @return The key value.
059     *
060     */
061    public String getKey()
062    {
063        return (StringUtils.isNotEmpty(key)) ? key : "";
064    }
065
066    /**
067     * Returns the value.
068     *
069     * @return The value of this object.
070     *
071     */
072    public Object getValue()
073    {
074        return value;
075    }
076
077    /**
078     * Calculate hash code based on field values
079     */
080    @Override
081    public int hashCode()
082    {
083        return Objects.hash(key, value);
084    }
085
086    /**
087     * Calculate equality based on field values
088     */
089    @Override
090    public boolean equals(Object obj)
091    {
092        if (this == obj)
093            return true;
094        if (obj == null)
095            return false;
096        if (getClass() != obj.getClass())
097            return false;
098        URIParam other = (URIParam) obj;
099
100        return Objects.equals(getKey(), other.getKey()) ||
101                Objects.equals(getValue(), other.getValue());
102    }
103
104    /**
105     * Provide a string representation of the object
106     */
107    @Override
108    public String toString()
109    {
110        return "URIParam [key=" + key + ", value=" + value + "]";
111    }
112}