Fri, 24 Oct 2014 15:02:28 +0200
8054367: More references for endpoints
Summary: fix also reviewed by Iaroslav.Savytskyi@oracle.com, Alexander.Fomin@oracle.com
Reviewed-by: mullan, skoivu
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2.runtime; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.HashSet; |
aoqi@0 | 29 | import java.util.IdentityHashMap; |
aoqi@0 | 30 | import java.util.Map; |
aoqi@0 | 31 | import java.util.Set; |
aoqi@0 | 32 | |
aoqi@0 | 33 | /** |
aoqi@0 | 34 | * Bi-directional map between elements, inner peers, |
aoqi@0 | 35 | * and outer peers. |
aoqi@0 | 36 | * |
aoqi@0 | 37 | * <p> |
aoqi@0 | 38 | * TODO: this should be rewritten for efficiency. |
aoqi@0 | 39 | * |
aoqi@0 | 40 | * @since 2.0 |
aoqi@0 | 41 | * |
aoqi@0 | 42 | * @author |
aoqi@0 | 43 | * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
aoqi@0 | 44 | */ |
aoqi@0 | 45 | public final class AssociationMap<XmlNode> { |
aoqi@0 | 46 | final static class Entry<XmlNode> { |
aoqi@0 | 47 | /** XML element. */ |
aoqi@0 | 48 | private XmlNode element; |
aoqi@0 | 49 | /** inner peer, or null. */ |
aoqi@0 | 50 | private Object inner; |
aoqi@0 | 51 | /** outer peer, or null. */ |
aoqi@0 | 52 | private Object outer; |
aoqi@0 | 53 | |
aoqi@0 | 54 | public XmlNode element() { |
aoqi@0 | 55 | return element; |
aoqi@0 | 56 | } |
aoqi@0 | 57 | public Object inner() { |
aoqi@0 | 58 | return inner; |
aoqi@0 | 59 | } |
aoqi@0 | 60 | public Object outer() { |
aoqi@0 | 61 | return outer; |
aoqi@0 | 62 | } |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | private final Map<XmlNode,Entry<XmlNode>> byElement = new IdentityHashMap<XmlNode,Entry<XmlNode>>(); |
aoqi@0 | 66 | private final Map<Object,Entry<XmlNode>> byPeer = new IdentityHashMap<Object,Entry<XmlNode>>(); |
aoqi@0 | 67 | private final Set<XmlNode> usedNodes = new HashSet<XmlNode>(); |
aoqi@0 | 68 | |
aoqi@0 | 69 | /** Records the new element<->inner peer association. */ |
aoqi@0 | 70 | public void addInner( XmlNode element, Object inner ) { |
aoqi@0 | 71 | Entry<XmlNode> e = byElement.get(element); |
aoqi@0 | 72 | if(e!=null) { |
aoqi@0 | 73 | if(e.inner!=null) |
aoqi@0 | 74 | byPeer.remove(e.inner); |
aoqi@0 | 75 | e.inner = inner; |
aoqi@0 | 76 | } else { |
aoqi@0 | 77 | e = new Entry<XmlNode>(); |
aoqi@0 | 78 | e.element = element; |
aoqi@0 | 79 | e.inner = inner; |
aoqi@0 | 80 | } |
aoqi@0 | 81 | |
aoqi@0 | 82 | byElement.put(element,e); |
aoqi@0 | 83 | |
aoqi@0 | 84 | Entry<XmlNode> old = byPeer.put(inner,e); |
aoqi@0 | 85 | if(old!=null) { |
aoqi@0 | 86 | if(old.outer!=null) |
aoqi@0 | 87 | byPeer.remove(old.outer); |
aoqi@0 | 88 | if(old.element!=null) |
aoqi@0 | 89 | byElement.remove(old.element); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | /** Records the new element<->outer peer association. */ |
aoqi@0 | 94 | public void addOuter( XmlNode element, Object outer ) { |
aoqi@0 | 95 | Entry<XmlNode> e = byElement.get(element); |
aoqi@0 | 96 | if(e!=null) { |
aoqi@0 | 97 | if(e.outer!=null) |
aoqi@0 | 98 | byPeer.remove(e.outer); |
aoqi@0 | 99 | e.outer = outer; |
aoqi@0 | 100 | } else { |
aoqi@0 | 101 | e = new Entry<XmlNode>(); |
aoqi@0 | 102 | e.element = element; |
aoqi@0 | 103 | e.outer = outer; |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | byElement.put(element,e); |
aoqi@0 | 107 | |
aoqi@0 | 108 | Entry<XmlNode> old = byPeer.put(outer,e); |
aoqi@0 | 109 | if(old!=null) { |
aoqi@0 | 110 | old.outer=null; |
aoqi@0 | 111 | |
aoqi@0 | 112 | if(old.inner==null) |
aoqi@0 | 113 | // remove this entry |
aoqi@0 | 114 | byElement.remove(old.element); |
aoqi@0 | 115 | } |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | public void addUsed( XmlNode n ) { |
aoqi@0 | 119 | usedNodes.add(n); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | public Entry<XmlNode> byElement( Object e ) { |
aoqi@0 | 123 | return byElement.get(e); |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | public Entry<XmlNode> byPeer( Object o ) { |
aoqi@0 | 127 | return byPeer.get(o); |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | public Object getInnerPeer( XmlNode element ) { |
aoqi@0 | 131 | Entry e = byElement(element); |
aoqi@0 | 132 | if(e==null) return null; |
aoqi@0 | 133 | else return e.inner; |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | public Object getOuterPeer( XmlNode element ) { |
aoqi@0 | 137 | Entry e = byElement(element); |
aoqi@0 | 138 | if(e==null) return null; |
aoqi@0 | 139 | else return e.outer; |
aoqi@0 | 140 | } |
aoqi@0 | 141 | } |