Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.ui.impl; |
| 19 | |
| 20 | import org.onosproject.ui.UiTopoOverlay; |
| 21 | |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
| 24 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 26 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 27 | /** |
| 28 | * A cache of {@link org.onosproject.ui.UiTopoOverlay}'s that were registered |
| 29 | * at the time the UI connection was established. |
| 30 | */ |
| 31 | public class TopoOverlayCache { |
| 32 | |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 33 | private static final String EMPTY = ""; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 34 | private static final UiTopoOverlay NONE = new NullOverlay(); |
| 35 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 36 | private final Map<String, UiTopoOverlay> overlays = new HashMap<>(); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 37 | private UiTopoOverlay current = NONE; |
| 38 | |
| 39 | public TopoOverlayCache() { |
| 40 | overlays.put(null, NONE); |
| 41 | } |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Adds a topology overlay to the cache. |
| 45 | * |
| 46 | * @param overlay a topology overlay |
| 47 | */ |
| 48 | public void add(UiTopoOverlay overlay) { |
| 49 | overlays.put(overlay.id(), overlay); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Invoked when the cache is no longer needed. |
| 54 | */ |
| 55 | public void destroy() { |
| 56 | overlays.clear(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Switching currently selected overlay. |
| 61 | * |
| 62 | * @param deact identity of overlay to deactivate |
| 63 | * @param act identity of overlay to activate |
| 64 | */ |
| 65 | public void switchOverlay(String deact, String act) { |
| 66 | UiTopoOverlay toDeactivate = getOverlay(deact); |
| 67 | UiTopoOverlay toActivate = getOverlay(act); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 68 | |
| 69 | toDeactivate.deactivate(); |
| 70 | current = toActivate; |
| 71 | current.activate(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | private UiTopoOverlay getOverlay(String id) { |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 75 | return isNullOrEmpty(id) ? NONE : overlays.get(id); |
| 76 | } |
| 77 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Returns the current overlay instance. |
| 80 | * Note that this method always returns a reference; when there is no |
| 81 | * overlay selected the "NULL" overlay instance is returned. |
| 82 | * |
| 83 | * @return the current overlay |
| 84 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 85 | public UiTopoOverlay currentOverlay() { |
| 86 | return current; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 90 | * Returns the number of overlays in the cache. Remember that this |
| 91 | * includes the "NULL" overlay, representing "no overlay selected". |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 92 | * |
| 93 | * @return number of overlays |
| 94 | */ |
| 95 | public int size() { |
| 96 | return overlays.size(); |
| 97 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 98 | |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Returns true if the identifier of the currently active overlay |
| 101 | * matches the given parameter. |
| 102 | * |
| 103 | * @param overlayId overlay identifier |
| 104 | * @return true if this matches the ID of currently active overlay |
| 105 | */ |
| 106 | public boolean isActive(String overlayId) { |
| 107 | return currentOverlay().id().equals(overlayId); |
| 108 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 109 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 110 | // overlay instance representing "no overlay selected" |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 111 | private static class NullOverlay extends UiTopoOverlay { |
| 112 | public NullOverlay() { |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 113 | super(EMPTY); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 114 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 115 | } |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 116 | } |