1 /*
   2  * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.graph;
  24 
  25 import java.util.ArrayList;
  26 import java.util.Collection;
  27 import java.util.Collections;
  28 import java.util.HashSet;
  29 import java.util.Map;
  30 import java.util.Set;
  31 
  32 public final class NodeNodeMap extends NodeMap<Node> implements Map<Node, Node> {
  33 
  34     public NodeNodeMap(Graph graph) {
  35         super(graph);
  36     }
  37 
  38     public NodeNodeMap(NodeNodeMap copyFrom) {
  39         super(copyFrom);
  40     }
  41 
  42     @Override
  43     public Node get(Object key) {
  44         return super.get((Node) key);
  45     }
  46 
  47     @Override
  48     public Node put(Node key, Node value) {
  49         Node oldValue = super.get(key);
  50         super.set(key, value);
  51         return oldValue;
  52     }
  53 
  54     @Override
  55     public Node remove(Object key) {
  56         throw new UnsupportedOperationException("Cannot remove keys from this map");
  57     }
  58 
  59     @Override
  60     public void putAll(Map<? extends Node, ? extends Node> m) {
  61         for (Entry<? extends Node, ? extends Node> entry : m.entrySet()) {
  62             put(entry.getKey(), entry.getValue());
  63         }
  64     }
  65 
  66     @Override
  67     public Set<Node> keySet() {
  68         HashSet<Node> entries = new HashSet<>();
  69         for (int i = 0; i < values.length; ++i) {
  70             Object v = values[i];
  71             if (v != null) {
  72                 Node key = getKey(i);
  73                 if (key != null) {
  74                     entries.add(key);
  75                 }
  76             }
  77         }
  78         /*
  79          * The normal contract for entrySet is that modifications of the set are reflected in the
  80          * underlying data structure. For simplicity don't allow that but complain if someone tries
  81          * to use it that way.
  82          */
  83         return Collections.unmodifiableSet(entries);
  84     }
  85 
  86     @Override
  87     public Collection<Node> values() {
  88         ArrayList<Node> result = new ArrayList<>(this.size());
  89         for (int i = 0; i < values.length; ++i) {
  90             Object v = values[i];
  91             if (v != null) {
  92                 result.add((Node) v);
  93             }
  94         }
  95         return result;
  96     }
  97 
  98     @Override
  99     public Set<Map.Entry<Node, Node>> entrySet() {
 100         HashSet<Map.Entry<Node, Node>> entries = new HashSet<>();
 101         for (Map.Entry<Node, Node> entry : entries()) {
 102             entries.add(entry);
 103         }
 104         /*
 105          * The normal contract for entrySet is that modifications of the set are reflected in the
 106          * underlying data structure. For simplicity don't allow that but complain if someone tries
 107          * to use it that way.
 108          */
 109         return Collections.unmodifiableSet(entries);
 110     }
 111 }