LCOV - code coverage report
Current view: top level - server - PropertyMap.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 12 14 85.7 %
Date: 2022-11-19 15:00:39 Functions: 8 9 88.9 %

          Line data    Source code
       1             : // Copyright (C) 2020 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.server;
      16             : 
      17             : import com.google.common.collect.ImmutableMap;
      18             : import java.util.Optional;
      19             : 
      20             : /**
      21             :  * Immutable map that holds a collection of random objects allowing for a type-safe retrieval.
      22             :  *
      23             :  * <p>Intended to be used in {@link CurrentUser} when the object is constructed during login and
      24             :  * holds per-request state. This functionality allows plugins/extensions to contribute specific data
      25             :  * to {@link CurrentUser} that is unknown to Gerrit core.
      26             :  */
      27             : public class PropertyMap {
      28             :   /** Empty instance to be referenced once per JVM. */
      29         154 :   public static final PropertyMap EMPTY = builder().build();
      30             : 
      31             :   /**
      32             :    * Typed key for {@link PropertyMap}. This class intentionally does not implement {@link
      33             :    * Object#equals(Object)} and {@link Object#hashCode()} so that the same instance has to be used
      34             :    * to retrieve a stored value.
      35             :    *
      36             :    * <p>We require the exact same key instance because {@link PropertyMap} is implemented in a
      37             :    * type-safe fashion by using Java generics to guarantee the return type. The generic type can't
      38             :    * be recovered at runtime, so there is no way to just use the type's full name as key - we'd have
      39             :    * to pass additional arguments. At the same time, this is in-line with how we'd want callers to
      40             :    * use {@link PropertyMap}: Instantiate a static, per-JVM key that is reused when setting and
      41             :    * getting values.
      42             :    */
      43         153 :   public static class Key<T> {}
      44             : 
      45             :   public static <T> Key<T> key() {
      46         153 :     return new Key<>();
      47             :   }
      48             : 
      49             :   public static class Builder {
      50             :     private ImmutableMap.Builder<Object, Object> mutableMap;
      51             : 
      52         154 :     private Builder() {
      53         154 :       this.mutableMap = ImmutableMap.builder();
      54         154 :     }
      55             : 
      56             :     /** Adds the provided {@code value} to the {@link PropertyMap} that is being built. */
      57             :     public <T> Builder put(Key<T> key, T value) {
      58           0 :       mutableMap.put(key, value);
      59           0 :       return this;
      60             :     }
      61             : 
      62             :     /** Builds and returns an immutable {@link PropertyMap}. */
      63             :     public PropertyMap build() {
      64         154 :       return new PropertyMap(mutableMap.build());
      65             :     }
      66             :   }
      67             : 
      68             :   private final ImmutableMap<Object, Object> map;
      69             : 
      70         154 :   private PropertyMap(ImmutableMap<Object, Object> map) {
      71         154 :     this.map = map;
      72         154 :   }
      73             : 
      74             :   /** Returns a new {@link Builder} instance. */
      75             :   public static Builder builder() {
      76         154 :     return new Builder();
      77             :   }
      78             : 
      79             :   /** Returns the requested value wrapped as {@link Optional}. */
      80             :   @SuppressWarnings("unchecked")
      81             :   public <T> Optional<T> get(Key<T> key) {
      82           5 :     return Optional.ofNullable((T) map.get(key));
      83             :   }
      84             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750