All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.extensions.registration.PrivateInternals_DynamicMapImpl Maven / Gradle / Ivy

There is a newer version: 3.11.0-rc4
Show newest version
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.extensions.registration;

import static java.util.Objects.requireNonNull;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.annotations.Export;
import com.google.inject.Key;
import com.google.inject.Provider;

/** DO NOT USE */
public class PrivateInternals_DynamicMapImpl extends DynamicMap {
  PrivateInternals_DynamicMapImpl() {}

  /**
   * Store one new element into the map.
   *
   * @param pluginName unique name of the plugin providing the export.
   * @param exportName name the plugin has exported the item as.
   * @param item the item to add to the collection. Must not be null.
   * @return handle to remove the item at a later point in time.
   */
  public RegistrationHandle put(String pluginName, String exportName, Provider item) {
    requireNonNull(item);
    final NamePair key = new NamePair(pluginName, exportName);
    items.put(key, item);
    return () -> items.remove(key, item);
  }

  /**
   * Store one new element that may be hot-replaceable in the future.
   *
   * @param pluginName unique name of the plugin providing the export.
   * @param key unique description from the item's Guice binding. This can be later obtained from
   *     the registration handle to facilitate matching with the new equivalent instance during a
   *     hot reload. The key must use an {@link Export} annotation.
   * @param item the item to add to the collection right now. Must not be null.
   * @return a handle that can remove this item later, or hot-swap the item without it ever leaving
   *     the collection.
   */
  @CanIgnoreReturnValue
  public ReloadableRegistrationHandle put(String pluginName, Key key, Provider item) {
    requireNonNull(item);
    String exportName = ((Export) key.getAnnotation()).value();
    NamePair np = new NamePair(pluginName, exportName);
    items.put(np, item);
    return new ReloadableHandle(np, key, item);
  }

  private class ReloadableHandle implements ReloadableRegistrationHandle {
    private final NamePair np;
    private final Key key;
    private final Provider item;

    ReloadableHandle(NamePair np, Key key, Provider item) {
      this.np = np;
      this.key = key;
      this.item = item;
    }

    @Override
    public void remove() {
      items.remove(np, item);
    }

    @Override
    public Key getKey() {
      return key;
    }

    @Nullable
    @Override
    public ReloadableHandle replace(Key newKey, Provider newItem) {
      if (items.replace(np, item, newItem)) {
        return new ReloadableHandle(np, newKey, newItem);
      }
      return null;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy