| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | |
| 9 | package net.sf.saxon.expr.parser; |
| 10 | |
| 11 | import net.sf.saxon.expr.Binding; |
| 12 | |
| 13 | import java.util.IdentityHashMap; |
| 14 | import java.util.Map; |
| 15 | |
| 16 | /** |
| 17 | * A map from old bindings to new bindings, maintained during a copy() operation applied |
| 18 | * to an expression tree. When local variable bindings are copied, the mapping from old to |
| 19 | * new binding can be added to the list; when local variable references are encountered, |
| 20 | * the binding can be switched from the old binding to the new. |
| 21 | * |
| 22 | * Bindings are compared strictly using object identity. Some classes that implement the |
| 23 | * Binding interface have their own equals() method that could return true for two |
| 24 | * bindings that need to be treated as distinct. |
| 25 | */ |
| 26 | public class RebindingMap { |
| 27 | |
| 28 | private Map<Binding, Binding> map = null; // created lazily |
| 29 | |
| 30 | /** |
| 31 | * Add an entry to the binding map |
| 32 | * @param oldBinding the existing binding |
| 33 | * @param newBinding the replacement binding |
| 34 | */ |
| 35 | |
| 36 | public void put(Binding oldBinding, Binding newBinding) { |
| 37 | if (map == null) { |
| 38 | map = new IdentityHashMap<Binding, Binding>(); |
| 39 | } |
| 40 | map.put(oldBinding, newBinding); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the new binding corresponding to an existing binding if there is one |
| 45 | * @param oldBinding the existing binding |
| 46 | * @return the new binding if one exists, or null otherwise |
| 47 | */ |
| 48 | |
| 49 | public Binding get(Binding oldBinding) { |
| 50 | return map == null ? null : map.get(oldBinding); |
| 51 | } |
| 52 | } |
| 53 |