Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.extensions.registration; 16 : 17 : import static java.util.Objects.requireNonNull; 18 : 19 : import com.google.gerrit.common.Nullable; 20 : import com.google.gerrit.extensions.annotations.Export; 21 : import com.google.inject.Key; 22 : import com.google.inject.Provider; 23 : 24 : /** <b>DO NOT USE</b> */ 25 : public class PrivateInternals_DynamicMapImpl<T> extends DynamicMap<T> { 26 152 : PrivateInternals_DynamicMapImpl() {} 27 : 28 : /** 29 : * Store one new element into the map. 30 : * 31 : * @param pluginName unique name of the plugin providing the export. 32 : * @param exportName name the plugin has exported the item as. 33 : * @param item the item to add to the collection. Must not be null. 34 : * @return handle to remove the item at a later point in time. 35 : */ 36 : public RegistrationHandle put(String pluginName, String exportName, Provider<T> item) { 37 3 : requireNonNull(item); 38 3 : final NamePair key = new NamePair(pluginName, exportName); 39 3 : items.put(key, item); 40 3 : return () -> items.remove(key, item); 41 : } 42 : 43 : /** 44 : * Store one new element that may be hot-replaceable in the future. 45 : * 46 : * @param pluginName unique name of the plugin providing the export. 47 : * @param key unique description from the item's Guice binding. This can be later obtained from 48 : * the registration handle to facilitate matching with the new equivalent instance during a 49 : * hot reload. The key must use an {@link Export} annotation. 50 : * @param item the item to add to the collection right now. Must not be null. 51 : * @return a handle that can remove this item later, or hot-swap the item without it ever leaving 52 : * the collection. 53 : */ 54 : public ReloadableRegistrationHandle<T> put(String pluginName, Key<T> key, Provider<T> item) { 55 152 : requireNonNull(item); 56 152 : String exportName = ((Export) key.getAnnotation()).value(); 57 152 : NamePair np = new NamePair(pluginName, exportName); 58 152 : items.put(np, item); 59 152 : return new ReloadableHandle(np, key, item); 60 : } 61 : 62 : private class ReloadableHandle implements ReloadableRegistrationHandle<T> { 63 : private final NamePair np; 64 : private final Key<T> key; 65 : private final Provider<T> item; 66 : 67 152 : ReloadableHandle(NamePair np, Key<T> key, Provider<T> item) { 68 152 : this.np = np; 69 152 : this.key = key; 70 152 : this.item = item; 71 152 : } 72 : 73 : @Override 74 : public void remove() { 75 101 : items.remove(np, item); 76 101 : } 77 : 78 : @Override 79 : public Key<T> getKey() { 80 0 : return key; 81 : } 82 : 83 : @Nullable 84 : @Override 85 : public ReloadableHandle replace(Key<T> newKey, Provider<T> newItem) { 86 0 : if (items.replace(np, item, newItem)) { 87 0 : return new ReloadableHandle(np, newKey, newItem); 88 : } 89 0 : return null; 90 : } 91 : } 92 : }