001/* A ToIntMapping 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//// ToIntMapMapping
031
032/** A ToIntMapping that is based on a Map. The values in the Map
033 must be instances of Integer. ToIntMapMappings 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 class ToIntMapMapping extends MapMapping implements ToIntMapping {
044    /** Construct a ToIntMapMapping from a given map. The values in the
045     *  must be instances of Integer; otherwise, unpredictable behavior
046     *  may result. Modifications to the argument Map after construction
047     *  of this mapping will be reflected in the mapping. The Map modifications
048     *  must follow the restriction that all added values to the Map
049     *  must be instances of Integer.
050     *  @param map The given map.
051     */
052    public ToIntMapMapping(Map map) {
053        super(map);
054    }
055
056    ///////////////////////////////////////////////////////////////////
057    ////                         public methods                    ////
058
059    /** Return true if the given object is in the domain of this Mapping.
060     *  More precisely, return true if the given object is a valid argument
061     *  to {@link #toInt(Object)}, which means that the object is a
062     *  key in the Map that is associated with this Mapping and the value
063     *  in the Map is an instance of Integer.
064     *  @param object The given object.
065     *  @return True if the given object is in the domain of this Mapping.
066     */
067    @Override
068    public boolean inDomain(Object object) {
069        return _map.containsKey(object) && _map.get(object) instanceof Integer;
070    }
071
072    /** Return the int value that is associated with given object under
073     *  this mapping. For efficiency, no error checking is performed
074     *  on the argument, and consequently, a runtime exception may result as
075     *  noted below. To perform argument validity checking before mapping an
076     *  object, use {@link ptolemy.graph.mapping.Mapping#inDomain(Object)}.
077     *  @param object The given object.
078     *  @return The int value that is associated with given object under
079     *  this mapping.
080     *  @exception RuntimeException If the given object is not an instance
081     *  of {@link java.lang.Integer} or if the given object is not in the
082     *  domain of the mapping.
083     */
084    @Override
085    public int toInt(Object object) {
086        return ((Integer) _map.get(object)).intValue();
087    }
088
089    @Override
090    public Object toObject(Object object) {
091        return _map.get(object);
092    }
093}