Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 33 | private static final UiTopoOverlay NONE = new NullOverlay(); |
| 34 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 35 | private final Map<String, UiTopoOverlay> overlays = new HashMap<>(); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 36 | private UiTopoOverlay current = NONE; |
| 37 | |
| 38 | public TopoOverlayCache() { |
| 39 | overlays.put(null, NONE); |
| 40 | } |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Adds a topology overlay to the cache. |
| 44 | * |
| 45 | * @param overlay a topology overlay |
| 46 | */ |
| 47 | public void add(UiTopoOverlay overlay) { |
| 48 | overlays.put(overlay.id(), overlay); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Invoked when the cache is no longer needed. |
| 53 | */ |
| 54 | public void destroy() { |
| 55 | overlays.clear(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Switching currently selected overlay. |
| 60 | * |
| 61 | * @param deact identity of overlay to deactivate |
| 62 | * @param act identity of overlay to activate |
| 63 | */ |
| 64 | public void switchOverlay(String deact, String act) { |
| 65 | UiTopoOverlay toDeactivate = getOverlay(deact); |
| 66 | UiTopoOverlay toActivate = getOverlay(act); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 67 | |
| 68 | toDeactivate.deactivate(); |
| 69 | current = toActivate; |
| 70 | current.activate(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private UiTopoOverlay getOverlay(String id) { |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 74 | return isNullOrEmpty(id) ? NONE : overlays.get(id); |
| 75 | } |
| 76 | |
| 77 | public UiTopoOverlay currentOverlay() { |
| 78 | return current; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the number of overlays in the cache. |
| 83 | * |
| 84 | * @return number of overlays |
| 85 | */ |
| 86 | public int size() { |
| 87 | return overlays.size(); |
| 88 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 89 | |
| 90 | |
| 91 | |
| 92 | private static class NullOverlay extends UiTopoOverlay { |
| 93 | public NullOverlay() { |
| 94 | super(null); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void init() { |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void activate() { |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void deactivate() { |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void destroy() { |
| 111 | } |
| 112 | } |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 113 | } |