Skip to content

Content of file Key.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Key.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">TheseFoolishThings - Utilities</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.util</a> &gt; <span class="el_source">Key.java</span></div><h1>Key.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * These Foolish Things - Miscellaneous utilities
 * http://thesefoolishthings.tidalwave.it - git clone git@bitbucket.org:tidalwave/thesefoolishthings-src.git
 * %%
 * Copyright (C) 2009 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); 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 &quot;AS IS&quot; 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.
 *
 * *********************************************************************************************************************
 *
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.util;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.io.Serializable;
import it.tidalwave.util.annotation.VisibleForTesting;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @since   1.11.0
 * @stereotype flyweight
 *
 **********************************************************************************************************************/
<span class="pc bpc" id="L51" title="10 of 18 branches missed.">@Immutable @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @EqualsAndHashCode(exclude = &quot;type&quot;) @ToString @Slf4j</span>
public class Key&lt;T&gt; implements StringValue, Comparable&lt;Key&lt;?&gt;&gt;, Serializable
  {
    private static final long serialVersionUID = 2817490298518793579L;

    // FIXME: a Set would be enough.
a Set would be enough.
<span class="fc" id="L57"> @VisibleForTesting static final ConcurrentHashMap&lt;String, Key&lt;?&gt;&gt; INSTANCES = new ConcurrentHashMap&lt;&gt;();</span> <span class="fc" id="L59"> @Getter @Nonnull</span> private final String name; <span class="fc" id="L62"> @Getter @Nonnull</span> private Class&lt;T&gt; type; /******************************************************************************************************************* * * Create a new instance with the given name. * * @param name the name * @deprecated use {@link #of(String, Class)} * ******************************************************************************************************************/ @Deprecated public Key (final @Nonnull String name) <span class="fc" id="L75"> {</span> <span class="fc" id="L76"> this.name = name;</span> <span class="fc" id="L77"> this.type = (Class&lt;T&gt;)ReflectionUtils.getTypeArguments(Key.class, getClass()).get(0);</span> <span class="fc" id="L78"> }</span> /******************************************************************************************************************* * * Creates an instance with the given name and type. If an identical key already exists, that existing instance is * returned. If a key with the same name but different type exists: * * &lt;ul&gt; * &lt;li&gt;if the previous type is {@code Object}, it is replaced by the new type; since key instances are * flyweight, this means that all the references previously obtained will see the change;&lt;/li&gt; * &lt;li&gt;otherwise an exception is thrown.&lt;/li&gt; * &lt;/ul&gt; * * We can call this behaviour &quot;lazy typing&quot; of the key. It allows to &quot;pre-register&quot; a key with a name, but unknown * type (for instance this could be done when reading from a file where there is no type information) and only later * specify the type (when a value associated to that key is going to be processed). For instance, the property * unmarshallers of NorthernWind work this way. * * @param &lt;T&gt; the static type * @param name the name * @param type the dynamic type * @return the key * @throws IllegalArgumentException when trying to create a {@code Key} with the same name of an existing * instance, but a different type. * @since 3.2-ALPHA-2 * ******************************************************************************************************************/ @Nonnull public static &lt;T&gt; Key&lt;T&gt; of (final @Nonnull String name, final @Nonnull Class&lt;T&gt; type) { <span class="fc" id="L108"> final Key&lt;T&gt; newKey = new Key&lt;T&gt;(name, type);</span> <span class="fc" id="L109"> final Key&lt;T&gt; key = (Key&lt;T&gt;)INSTANCES.putIfAbsent(name, newKey);</span> <span class="fc bfc" id="L111" title="All 4 branches covered."> if ((key != null) &amp;&amp; (key.getType() != newKey.getType()))</span> { <span class="fc bfc" id="L113" title="All 2 branches covered."> if (key.getType() != Object.class)</span> { <span class="fc" id="L115"> final String message = String.format(&quot;Can't create %s: previously created with type %s&quot;,</span> <span class="fc" id="L116"> newKey, key.getType().getName());</span> <span class="fc" id="L117"> throw new IllegalArgumentException(message);</span> } <span class="fc" id="L120"> log.debug(&quot;Redefining {} as {}&quot;, key, type);</span> <span class="fc" id="L121"> key.type = type;</span> } <span class="fc bfc" id="L124" title="All 2 branches covered."> return key != null ? key : newKey;</span> } /******************************************************************************************************************* * * Returns a key registered with a given name, if it exists. While the type bound to the key is statically unknown * (thus the result is a {@code Key&lt;Object&gt;}), it can be dynamically retrieved by means of {@link #getType()}. * * @param name the name * @return the key * @since 3.2-ALPHA-3 * ******************************************************************************************************************/ @Nonnull public static Optional&lt;Key&lt;Object&gt;&gt; named (final @Nonnull String name) { <span class="nc" id="L140"> return Optional.ofNullable((Key&lt;Object&gt;)INSTANCES.get(name));</span> } /******************************************************************************************************************* * * Returns all the keys registered in the system. * * @return a mutable and sorted set of keys. * @since 3.2-ALPHA-2 * ******************************************************************************************************************/ @Nonnull public static Set&lt;Key&lt;?&gt;&gt; allKeys() { <span class="fc" id="L154"> return new TreeSet&lt;Key&lt;?&gt;&gt;(INSTANCES.values());</span> } /******************************************************************************************************************* * * Create a new instance with the given name. * * @param name the name * @deprecated use {@link #of(String, Class)} * ******************************************************************************************************************/ public Key (final @Nonnull StringValue name) { <span class="nc" id="L167"> this(name.stringValue());</span> <span class="nc" id="L168"> }</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public String stringValue() { <span class="nc" id="L178"> return name;</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override public int compareTo (final @Nonnull Key&lt;?&gt; other) { <span class="fc" id="L189"> return this.name.compareTo(other.name);</span> // final Comparator&lt;Key&lt;?&gt;&gt; byType = Comparator.comparing(k -&gt; k.getType().getName()); // final Comparator&lt;Key&lt;?&gt;&gt; byName = Comparator.comparing(Key::getName); // return byName.thenComparing(byType).compare(this, other); } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.6.202009150832</span></div></body></html>