com.google.gerrit.server.PropertyMap Maven / Gradle / Ivy
// Copyright (C) 2020 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.server;
import com.google.common.collect.ImmutableMap;
import java.util.Optional;
/**
 * Immutable map that holds a collection of random objects allowing for a type-safe retrieval.
 *
 * Intended to be used in {@link CurrentUser} when the object is constructed during login and
 * holds per-request state. This functionality allows plugins/extensions to contribute specific data
 * to {@link CurrentUser} that is unknown to Gerrit core.
 */
public class PropertyMap {
  /** Empty instance to be referenced once per JVM. */
  public static final PropertyMap EMPTY = builder().build();
  /**
   * Typed key for {@link PropertyMap}. This class intentionally does not implement {@link
   * Object#equals(Object)} and {@link Object#hashCode()} so that the same instance has to be used
   * to retrieve a stored value.
   *
   * 
We require the exact same key instance because {@link PropertyMap} is implemented in a
   * type-safe fashion by using Java generics to guarantee the return type. The generic type can't
   * be recovered at runtime, so there is no way to just use the type's full name as key - we'd have
   * to pass additional arguments. At the same time, this is in-line with how we'd want callers to
   * use {@link PropertyMap}: Instantiate a static, per-JVM key that is reused when setting and
   * getting values.
   */
  public static class Key {}
  public static  Key key() {
    return new Key<>();
  }
  public static class Builder {
    private ImmutableMap.Builder