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 :: Examples :: DCI :: Marshal XStream</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-util</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">/*
 * *********************************************************************************************************************
 *
 * TheseFoolishThings: Miscellaneous utilities
 * http://tidalwave.it/projects/thesefoolishthings
 *
 * Copyright (C) 2009 - 2021 by 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.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
 * git clone https://github.com/tidalwave-it/thesefoolishthings-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.util;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.Comparator;
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;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @since   1.11.0
 * @stereotype flyweight
 *
 **********************************************************************************************************************/
<span class="pc bpc" id="L50" title="13 of 26 branches missed.">@Immutable @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @EqualsAndHashCode @ToString</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="L56"> @VisibleForTesting static final ConcurrentHashMap&lt;Key&lt;?&gt;, Key&lt;?&gt;&gt; INSTANCES = new ConcurrentHashMap&lt;&gt;();</span> <span class="fc" id="L58"> @Getter @Nonnull</span> private final String name; <span class="fc" id="L61"> @Getter @Nonnull</span> private final 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 (@Nonnull final String name) <span class="fc" id="L74"> {</span> <span class="fc" id="L75"> this.name = name;</span> <span class="fc" id="L76"> type = (Class&lt;T&gt;)ReflectionUtils.getTypeArguments(Key.class, getClass()).get(0);</span> <span class="fc" id="L77"> }</span> /******************************************************************************************************************* * * Creates an instance with the given name and type. If an identical key already exists, that existing instance is * returned. It is allowed to have two keys with the same name and different types (e.g. {@code Key.of(&quot;foo&quot;, * String.class)} and {@code Key.of(&quot;foo&quot;, Integer.class)}: they are considered as two distinct keys. This feature * allows to treat the same data both as typed and type-agnostic at the same time; for instance, a collection of * properties could be read from a configuration file in type-agnostic way (if there is no type information in * the file) and later managed as typed only when needed. * * @param &lt;T&gt; the static type * @param name the name * @param type the dynamic type * @return the key * @since 3.2-ALPHA-2 * ******************************************************************************************************************/ @Nonnull public static &lt;T&gt; Key&lt;T&gt; of (@Nonnull final String name, @Nonnull final Class&lt;T&gt; type) { <span class="fc" id="L98"> final Key&lt;T&gt; newKey = new Key&lt;&gt;(name, type);</span> <span class="fc" id="L99"> final Key&lt;T&gt; key = (Key&lt;T&gt;)INSTANCES.putIfAbsent(newKey, newKey);</span> <span class="fc bfc" id="L100" title="All 2 branches covered."> return key != null ? key : newKey;</span> } /******************************************************************************************************************* * * Creates an instance with the given name. Type is considere as unknown (and assumed as {@link Object}. Please see * {@link #of(String, Class)} for more information. * * @param name the name * @return the key * @since 3.2-ALPHA-4 * ******************************************************************************************************************/ @Nonnull public static Key&lt;Object&gt; of (@Nonnull final String name) { <span class="nc" id="L116"> return of(name, Object.class);</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="L130"> return new TreeSet&lt;&gt;(INSTANCES.values());</span> } /******************************************************************************************************************* * * Create a new instance with the given name. * * @param name the name * @deprecated use {@link #of(String, Class)} * ******************************************************************************************************************/ public Key (@Nonnull final StringValue name) { <span class="nc" id="L143"> this(name.stringValue());</span> <span class="nc" id="L144"> }</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public String stringValue() { <span class="nc" id="L154"> return name;</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override public int compareTo (@Nonnull final Key&lt;?&gt; other) { <span class="fc" id="L165"> final Comparator&lt;Key&lt;?&gt;&gt; byType = Comparator.comparing(k -&gt; k.getType().getName());</span> <span class="fc" id="L166"> final Comparator&lt;Key&lt;?&gt;&gt; byName = Comparator.comparing(Key::getName);</span> <span class="fc" id="L167"> return byName.thenComparing(byType).compare(this, other);</span> } } </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>