Key.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 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
  12.  * the License. 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
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.util;

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

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

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

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

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

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

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

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

  110.     /*******************************************************************************************************************
  111.      *
  112.      * Returns all the keys registered in the system.
  113.      *
  114.      * @return    a mutable and sorted set of keys.
  115.      * @since     3.2-ALPHA-2
  116.      *
  117.      ******************************************************************************************************************/
  118.     @Nonnull
  119.     public static Set<Key<?>> allKeys()
  120.       {
  121.         return new TreeSet<>(INSTANCES.values());
  122.       }

  123.     /*******************************************************************************************************************
  124.      *
  125.      * Create a new instance with the given name.
  126.      *
  127.      * @param   name        the name
  128.      * @deprecated use {@link #of(String, Class)}
  129.      *
  130.      ******************************************************************************************************************/
  131.     public Key (@Nonnull final StringValue name)
  132.       {
  133.         this(name.stringValue());
  134.       }

  135.     /*******************************************************************************************************************
  136.      *
  137.      * {@inheritDoc}
  138.      *
  139.      ******************************************************************************************************************/
  140.     @Override @Nonnull
  141.     public String stringValue()
  142.       {
  143.         return name;
  144.       }

  145.     /*******************************************************************************************************************
  146.      *
  147.      * {@inheritDoc}
  148.      *
  149.      ******************************************************************************************************************/
  150.     @Override
  151.     public int compareTo (@Nonnull final Key<?> other)
  152.       {
  153.         final Comparator<Key<?>> byType = Comparator.comparing(k -> k.getType().getName());
  154.         final Comparator<Key<?>> byName = Comparator.comparing(Key::getName);
  155.         return byName.thenComparing(byType).compare(this, other);
  156.       }
  157.   }