View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * TheseFoolishThings: Miscellaneous utilities
5    * http://tidalwave.it/projects/thesefoolishthings
6    *
7    * Copyright (C) 2009 - 2025 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  
28  import jakarta.annotation.Nonnull;
29  import java.util.concurrent.atomic.AtomicInteger;
30  
31  /***************************************************************************************************************************************************************
32   *
33   * A factory for creating a new, unique {@code Id} for an object.
34   *
35   * @author  Fabrizio Giudici
36   * @it.tidalwave.javadoc.stable
37   *
38   **************************************************************************************************************************************************************/
39  @FunctionalInterface
40  public interface IdFactory
41    {
42      public static final Class<IdFactory> _IdFactory_ = IdFactory.class;
43  
44      /** A default implementation that uses UUID.
45       *  @since 3.2-ALPHA-19 */
46      public static final IdFactory DEFAULT = Id::ofUuid;
47  
48      /***********************************************************************************************************************************************************
49       * Creates a new mock factory, useful for testing, that returns mock UUIDs based on a sequential counter.
50       *
51       * @return  the new factory
52       * @since 3.2-ALPHA-23
53       **********************************************************************************************************************************************************/
54      @Nonnull
55      public static IdFactory createMock()
56        {
57          final var sequence = new AtomicInteger(0);
58          return () -> Id.of(String.format("%08x-0000-0000-0000-000000000000", sequence.getAndIncrement()));
59        }
60  
61      /***********************************************************************************************************************************************************
62       * Creates a new id.
63       *
64       * @return  the new id
65       **********************************************************************************************************************************************************/
66      @Nonnull
67      public Id createId();
68  
69      /***********************************************************************************************************************************************************
70       * Creates a new id for an object of the given class.
71       *
72       * @param   objectClass  the class of the object for which the {@code Id} is created
73       * @return               the new id
74       **********************************************************************************************************************************************************/
75      @Nonnull
76      public default Id createId (@Nonnull Class<?> objectClass)
77        {
78          return createId();
79        }
80  
81      /***********************************************************************************************************************************************************
82       * Creates a new id for the given object of the given class. This method allows to explicitly pass a {@code Class}
83       * for cases in which the {@code object} implements multiple interfaces and one wants to specify the referenced one.
84       *
85       * @param   object       the object for which the {@code Id}
86       * @param   objectClass  the class of the object for which the {@code Id} is created
87       * @return               the new id
88       **********************************************************************************************************************************************************/
89      @Nonnull
90      public default Id createId (@Nonnull Class<?> objectClass, @Nonnull Object object)
91        {
92          return createId(objectClass);
93        }
94    }