001/* A base class for a Mapping that is based on a Map.
002
003 Copyright (c) 2003-2014 The University of Maryland.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 MARYLAND HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 */
025package ptolemy.graph.mapping;
026
027import java.util.Map;
028
029///////////////////////////////////////////////////////////////////
030//// MapMapping
031
032/** A Mapping that is based on a Map. The domain of the Mapping is the
033 set of keys in the Map. MapMappings are immutable in the
034 sense that the underlying Map cannot be changed (although the keys and
035 values associated with the Map can be changed).
036
037 @since Ptolemy II 4.0
038 @Pt.ProposedRating Red (ssb)
039 @Pt.AcceptedRating Red (ssb)
040 @author Shuvra S. Bhattacharyya
041 @version $Id$
042 */
043public abstract class MapMapping implements Mapping {
044    /** Construct a MapMapping from a given Map.
045     *  Modifications to the argument Map after construction
046     *  of this mapping will be reflected in the Mapping.
047     *  @param map The given map.
048     */
049    public MapMapping(Map map) {
050        _map = map;
051    }
052
053    ///////////////////////////////////////////////////////////////////
054    ////                         public methods                    ////
055
056    /** Return true if the given object is of the same Class and based
057     *  on the same Map as this one.
058     *  @param object The given object.
059     *  @return True if the given object is of the same class and based
060     *  on the same Map as this one.
061     */
062    @Override
063    public boolean equals(Object object) {
064        if (object == null || object.getClass() != getClass()) {
065            return false;
066        }
067
068        return _map.equals(((MapMapping) object)._map);
069    }
070
071    /** Return the hash code of this MapMapping. The hash code is
072     *  simply that of the Map that this Mapping is based on.
073     */
074    @Override
075    public int hashCode() {
076        return _map.hashCode();
077    }
078
079    /** Return true if the given object is a key in the Map that is associated
080     *  with this mapping.
081     *  @param object The given object.
082     *  @return True if the given object is a key in the Map that is associated
083     *  with this mapping.
084     */
085    @Override
086    public boolean inDomain(Object object) {
087        return _map.containsKey(object);
088    }
089
090    /** Return a string representation of this MapMapping. The
091     *  string representation is the class name, followed by a
092     *  delimiting string, followed by a
093     *  string representation of the underlying Map.
094     */
095    @Override
096    public String toString() {
097        return getClass().getName() + "based on the following Map\n"
098                + _map.toString() + "\n";
099    }
100
101    ///////////////////////////////////////////////////////////////////
102    ////                         protected variables               ////
103    /** The Map on which this Mapping is based. */
104    protected Map _map;
105}