001/*
002 * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.graph;
024
025import java.util.*;
026
027public final class NodeNodeMap extends NodeMap<Node> implements Map<Node, Node> {
028
029    public NodeNodeMap(Graph graph) {
030        super(graph);
031    }
032
033    public NodeNodeMap(NodeNodeMap copyFrom) {
034        super(copyFrom);
035    }
036
037    public Node get(Object key) {
038        return super.get((Node) key);
039    }
040
041    public Node put(Node key, Node value) {
042        Node oldValue = super.get(key);
043        super.set(key, value);
044        return oldValue;
045    }
046
047    public Node remove(Object key) {
048        throw new UnsupportedOperationException("Cannot remove keys from this map");
049    }
050
051    public void putAll(Map<? extends Node, ? extends Node> m) {
052        for (Entry<? extends Node, ? extends Node> entry : m.entrySet()) {
053            put(entry.getKey(), entry.getValue());
054        }
055    }
056
057    public Set<Node> keySet() {
058        HashSet<Node> entries = new HashSet<>();
059        for (int i = 0; i < values.length; ++i) {
060            Object v = values[i];
061            if (v != null) {
062                Node key = getKey(i);
063                if (key != null) {
064                    entries.add(key);
065                }
066            }
067        }
068        /*
069         * The normal contract for entrySet is that modifications of the set are reflected in the
070         * underlying data structure. For simplicity don't allow that but complain if someone tries
071         * to use it that way.
072         */
073        return Collections.unmodifiableSet(entries);
074    }
075
076    public Collection<Node> values() {
077        ArrayList<Node> result = new ArrayList<>(this.size());
078        for (int i = 0; i < values.length; ++i) {
079            Object v = values[i];
080            if (v != null) {
081                result.add((Node) v);
082            }
083        }
084        return result;
085    }
086
087    public Set<Map.Entry<Node, Node>> entrySet() {
088        HashSet<Map.Entry<Node, Node>> entries = new HashSet<>();
089        for (Map.Entry<Node, Node> entry : entries()) {
090            entries.add(entry);
091        }
092        /*
093         * The normal contract for entrySet is that modifications of the set are reflected in the
094         * underlying data structure. For simplicity don't allow that but complain if someone tries
095         * to use it that way.
096         */
097        return Collections.unmodifiableSet(entries);
098    }
099}