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