IdFactory.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 java.util.concurrent.atomic.AtomicInteger;

  29. /***************************************************************************************************************************************************************
  30.  *
  31.  * A factory for creating a new, unique {@code Id} for an object.
  32.  *
  33.  * @author  Fabrizio Giudici
  34.  * @it.tidalwave.javadoc.stable
  35.  *
  36.  **************************************************************************************************************************************************************/
  37. @FunctionalInterface
  38. public interface IdFactory
  39.   {
  40.     public static final Class<IdFactory> _IdFactory_ = IdFactory.class;

  41.     /** A default implementation that uses UUID.
  42.      *  @since 3.2-ALPHA-19 */
  43.     public static final IdFactory DEFAULT = Id::ofUuid;

  44.     /***********************************************************************************************************************************************************
  45.      * Creates a new mock factory, useful for testing, that returns mock UUIDs based on a sequential counter.
  46.      *
  47.      * @return  the new factory
  48.      * @since 3.2-ALPHA-23
  49.      **********************************************************************************************************************************************************/
  50.     @Nonnull
  51.     public static IdFactory createMock()
  52.       {
  53.         final var sequence = new AtomicInteger(0);
  54.         return () -> Id.of(String.format("%08x-0000-0000-0000-000000000000", sequence.getAndIncrement()));
  55.       }

  56.     /***********************************************************************************************************************************************************
  57.      * Creates a new id.
  58.      *
  59.      * @return  the new id
  60.      **********************************************************************************************************************************************************/
  61.     @Nonnull
  62.     public Id createId();

  63.     /***********************************************************************************************************************************************************
  64.      * Creates a new id for an object of the given class.
  65.      *
  66.      * @param   objectClass  the class of the object for which the {@code Id} is created
  67.      * @return               the new id
  68.      **********************************************************************************************************************************************************/
  69.     @Nonnull
  70.     public default Id createId (@Nonnull Class<?> objectClass)
  71.       {
  72.         return createId();
  73.       }

  74.     /***********************************************************************************************************************************************************
  75.      * Creates a new id for the given object of the given class. This method allows to explicitly pass a {@code Class}
  76.      * for cases in which the {@code object} implements multiple interfaces and one wants to specify the referenced one.
  77.      *
  78.      * @param   object       the object for which the {@code Id}
  79.      * @param   objectClass  the class of the object for which the {@code Id} is created
  80.      * @return               the new id
  81.      **********************************************************************************************************************************************************/
  82.     @Nonnull
  83.     public default Id createId (@Nonnull Class<?> objectClass, @Nonnull Object object)
  84.       {
  85.         return createId(objectClass);
  86.       }
  87.   }