Key.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * 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
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util;

  27. import javax.annotation.Nonnull;
  28. import javax.annotation.concurrent.Immutable;
  29. import java.util.Comparator;
  30. import java.util.Set;
  31. import java.util.TreeSet;
  32. import java.util.concurrent.ConcurrentHashMap;
  33. import java.io.Serializable;
  34. import it.tidalwave.util.annotation.VisibleForTesting;
  35. import lombok.AccessLevel;
  36. import lombok.EqualsAndHashCode;
  37. import lombok.Getter;
  38. import lombok.RequiredArgsConstructor;
  39. import lombok.ToString;

  40. /***************************************************************************************************************************************************************
  41.  *
  42.  * @author  Fabrizio Giudici
  43.  * @since   1.11.0
  44.  * @stereotype flyweight
  45.  *
  46.  **************************************************************************************************************************************************************/
  47. @Immutable @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @EqualsAndHashCode @ToString
  48. public class Key<T> implements StringValue, Comparable<Key<?>>, Serializable
  49.   {
  50.     private static final long serialVersionUID = 2817490298518793579L;

  51.     // FIXME: a Set would be enough.
  52.     @VisibleForTesting static final ConcurrentHashMap<Key<?>, Key<?>> INSTANCES = new ConcurrentHashMap<>();

  53.     @Getter @Nonnull
  54.     private final String name;

  55.     @Getter @Nonnull
  56.     private final Class<T> type;

  57.     /***********************************************************************************************************************************************************
  58.      * Create a new instance with the given name.
  59.      *
  60.      * @param   name        the name
  61.      * @deprecated use {@link #of(String, Class)}
  62.      **********************************************************************************************************************************************************/
  63.     @Deprecated
  64.     public Key (@Nonnull final String name)
  65.       {
  66.         this.name = name;
  67.         type = (Class<T>)ReflectionUtils.getTypeArguments(Key.class, getClass()).get(0);
  68.       }

  69.     /***********************************************************************************************************************************************************
  70.      * Creates an instance with the given name and type. If an identical key already exists, that existing instance is
  71.      * returned. It is allowed to have two keys with the same name and different types (e.g. {@code Key.of("foo",
  72.      * String.class)} and {@code Key.of("foo", Integer.class)}): they are considered as two distinct keys. This feature
  73.      * allows to treat the same data both as typed and type-agnostic at the same time; for instance, a collection of
  74.      * properties could be read from a configuration file in type-agnostic way (if there is no type information in
  75.      * the file) and later managed as typed only when needed.
  76.      *
  77.      * @param <T>     the static type
  78.      * @param name    the name
  79.      * @param type    the dynamic type
  80.      * @return        the key
  81.      * @since         3.2-ALPHA-2
  82.      **********************************************************************************************************************************************************/
  83.     @Nonnull @SuppressWarnings("unchecked")
  84.     public static <T> Key<T> of (@Nonnull final String name, @Nonnull final Class<T> type)
  85.       {
  86.         final var newKey = new Key<T>(name, type);
  87.         final var key = (Key<T>)INSTANCES.putIfAbsent(newKey, newKey);
  88.         return key != null ? key : newKey;
  89.       }

  90.     /***********************************************************************************************************************************************************
  91.      * Creates an instance with the given name. Type is considered as unknown (and assumed as {@link Object}). Please
  92.      * see {@link #of(String, Class)} for more information.
  93.      *
  94.      * @param name    the name
  95.      * @return        the key
  96.      * @since         3.2-ALPHA-4
  97.      **********************************************************************************************************************************************************/
  98.     @Nonnull
  99.     public static Key<Object> of (@Nonnull final String name)
  100.       {
  101.         return of(name, Object.class);
  102.       }

  103.     /***********************************************************************************************************************************************************
  104.      * Returns all the keys registered in the system.
  105.      *
  106.      * @return    a mutable and sorted set of keys.
  107.      * @since     3.2-ALPHA-2
  108.      **********************************************************************************************************************************************************/
  109.     @Nonnull
  110.     public static Set<Key<?>> allKeys()
  111.       {
  112.         return new TreeSet<>(INSTANCES.values());
  113.       }

  114.     /***********************************************************************************************************************************************************
  115.      * Create a new instance with the given name.
  116.      *
  117.      * @param   name        the name
  118.      * @deprecated use {@link #of(String, Class)}
  119.      **********************************************************************************************************************************************************/
  120.     public Key (@Nonnull final StringValue name)
  121.       {
  122.         this(name.stringValue());
  123.       }

  124.     /***********************************************************************************************************************************************************
  125.      * {@inheritDoc}
  126.      **********************************************************************************************************************************************************/
  127.     @Override @Nonnull
  128.     public String stringValue()
  129.       {
  130.         return name;
  131.       }

  132.     /***********************************************************************************************************************************************************
  133.      * {@inheritDoc}
  134.      **********************************************************************************************************************************************************/
  135.     @Override
  136.     public int compareTo (@Nonnull final Key<?> other)
  137.       {
  138.         final Comparator<Key<?>> byType = Comparator.comparing(k -> k.getType().getName());
  139.         final Comparator<Key<?>> byName = Comparator.comparing(Key::getName);
  140.         return byName.thenComparing(byType).compare(this, other);
  141.       }
  142.   }