001/*
002 *  Copyright 2019 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.data;
017
018import java.util.Objects;
019
020/**
021 * Class containing a reference
022 */
023public class Reference
024{
025    /** The value identifier */
026    public static final String VALUE_IDENTIFIER = "value";
027    /** The type identifier */
028    public static final String TYPE_IDENTIFIER = "type";
029    
030    /** The default type */
031    public static final String DEFAULT_TYPE = "__external";
032    
033    private String _value;
034    private String _type;
035    
036    /**
037     * Constructs a reference
038     * @param value the value of the reference
039     */
040    public Reference(String value)
041    {
042        this(value, null);
043    }
044    
045    /**
046     * Constructs a reference
047     * @param value the value of the reference
048     * @param type the type of the reference. For example, the type of an URL is '__external'
049     */
050    public Reference(String value, String type)
051    {
052        _type = type != null ? type : DEFAULT_TYPE;
053        _value = value;
054    }
055
056    /**
057     * Retrieves the value
058     * @return the value
059     */
060    public String getValue()
061    {
062        return _value;
063    }
064
065    /**
066     * Retrieves the type
067     * @return the type
068     */
069    public String getType()
070    {
071        return _type;
072    }
073
074    @Override
075    public int hashCode()
076    {
077        return Objects.hash(_type, _value);
078    }
079
080    @Override
081    public boolean equals(Object obj)
082    {
083        if (this == obj)
084        {
085            return true;
086        }
087        if (!(obj instanceof Reference))
088        {
089            return false;
090        }
091        Reference other = (Reference) obj;
092        return Objects.equals(_type, other._type) && Objects.equals(_value, other._value);
093    }
094}