Skip to content

Commit

Permalink
V1.6.0: Identifier Scoped option added
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-Bjorn committed Mar 22, 2023
1 parent 67bbf2b commit 7fcca8a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ After that use the methods provided in the builder to build to `TypeProvider`. D
`TypeProviderBuilder#buildProvider` is automatically done through the `Register`.

The identifier can be added with the value can be added through the `#addIdentifier` in the consumer. The key is the
first parameter of the `#addIdentifier` represents the key. The second parameter represents the value of the key.
first parameter of the `#addIdentifier` represents the key. The second parameter represents the value of the key.

```java
register.register(Car.class, (IdentifiersBuilder<Car, Cars> settings) -> settings
Expand All @@ -252,6 +252,9 @@ register.register(Car.class, (IdentifiersBuilder<Car, Cars> settings) -> setting
);
```

It is also possible to scoped value like in C#, this can be added like `#addSupplierIdentifier`. A scoped value is a
value that represents a value that each time will be constructed when the `TypeProvider#getInitProvider` is called.

At last in use the ConstructorResolver with the settings consumer or use the builder like in the example below. Set the
identifiers through the `setIdentifiers` method, in the consumer or the builder. The builder needs
the `ConstructionSettingsBuilder#initClass` to initialize the class. The `ConstructorResolver#initClass` does the class
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<groupId>nl.devoxist</groupId>
<artifactId>TypeRegister</artifactId>
<version>1.5.0</version>
<version>1.6.0</version>
<packaging>jar</packaging>

<name>Type Register</name>
Expand Down Expand Up @@ -188,7 +188,7 @@
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.0</version>
<version>24.0.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
* @version 1.5.0
* @since 1.5.0
*/
public final class TypeKeyProvider<T, I> extends TypeProvider<T, Collection<? extends T>> implements Cloneable {
public final class TypeKeyProvider<T, I> extends TypeProvider<T, Collection<?>> implements Cloneable {
/**
* The {@link Map} that holds the identifiers with their implementations of the type.
*
* @since 1.5.0
*/
private final Map<I, T> identifiersMap;
private final Map<I, TypeProvider<T, ?>> identifiersMap;
/**
* The identifier to get the value {@link #identifiersMap}. NOTE: this can only be applied when this is a clone. The
* {@link #applyIdentifiers(Object[])} sets the value of this field.
Expand All @@ -66,7 +66,7 @@ public final class TypeKeyProvider<T, I> extends TypeProvider<T, Collection<? ex
* @throws ProviderException If there are no identifiers registered.
* @since 1.5.0
*/
public TypeKeyProvider(@NotNull Class<T> typeCls, @NotNull Map<I, T> identifiersMap) {
public TypeKeyProvider(@NotNull Class<T> typeCls, @NotNull Map<I, TypeProvider<T, ?>> identifiersMap) {
super(typeCls, identifiersMap.values());

if (identifiersMap.size() == 0) {
Expand All @@ -84,7 +84,7 @@ public TypeKeyProvider(@NotNull Class<T> typeCls, @NotNull Map<I, T> identifiers
* @since 1.5.0
*/
@Contract(pure = true)
public @NotNull Map<I, T> getIdentifiersMap() {
public @NotNull Map<I, Object> getIdentifiersMap() {
return new HashMap<>(identifiersMap);
}

Expand Down Expand Up @@ -125,14 +125,20 @@ public TypeKeyProvider(@NotNull Class<T> typeCls, @NotNull Map<I, T> identifiers
*/
@Nullable
private T getInstance() {
for (Object o : identifiers) {
T t = identifiersMap.get(o);
for (Object identifier : identifiers) {
TypeProvider<T, ?> item = identifiersMap.get(identifier);

if (t == null) {
if (item == null) {
continue;
}

return t;
T instance = item.getInitProvider();

if (!getType().isInstance(instance)) {
throw new ProviderException("The object that has been registered is not an instance of the type.");
}

return instance;
}

return null;
Expand Down Expand Up @@ -214,13 +220,12 @@ private T getInstance() {
* @see java.lang.Cloneable
* @since 1.5.0
*/
@SuppressWarnings("unchecked")
@Override
public TypeKeyProvider<T, I> clone() {
try {
return (TypeKeyProvider<T, I>) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
throw new AssertionError(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
package nl.devoxist.typeresolver.providers.builders;

import nl.devoxist.typeresolver.exception.RegisterException;
import nl.devoxist.typeresolver.functions.SerializableSupplier;
import nl.devoxist.typeresolver.providers.TypeKeyProvider;
import nl.devoxist.typeresolver.providers.TypeObjectProvider;
import nl.devoxist.typeresolver.providers.TypeProvider;
import nl.devoxist.typeresolver.providers.TypeSupplierProvider;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
* {@link IdentifiersBuilder} is the subclass of {@link TypeProviderBuilder} which will chain-edit the a
Expand All @@ -48,20 +52,37 @@ public final class IdentifiersBuilder<T, I> extends TypeProviderBuilder<T> {
*
* @since 1.5.0
*/
private final Map<I, T> identifiersMap = new HashMap<>();
private final Map<I, TypeProvider<T, ?>> identifiersMap = new HashMap<>();

/**
* Add an identifier with a value to the {@link TypeKeyProvider#getIdentifiersMap()}.
*
* @param identifier The identifier which will be linked to the value.
* @param value The value of the identifier.
* @param value The value of the identifier. This value will be returned when the given identifier is applied
* in {@link TypeKeyProvider}.
*
* @return The builder of the {@link TypeKeyProvider} to chain-edit the {@link TypeKeyProvider}, when the
* options are set call the {@link IdentifiersBuilder#buildProvider(Class)} to get the {@link TypeProvider}.
*/
@Contract("_, _ -> this")
public IdentifiersBuilder<T, I> addIdentifier(I identifier, T value) {
this.identifiersMap.put(identifier, value);
this.identifiersMap.put(identifier, new TypeObjectProvider<>((Class<T>) value.getClass(), value));
return this;
}

/**
* Add an identifier with a value to the {@link TypeKeyProvider#getIdentifiersMap()}.
*
* @param identifier The identifier which will be linked to the value.
* @param value The supplier value of the identifier. Each time the {@link TypeKeyProvider#getInitProvider()}
* and the given identifier is applied, it will return the output of {@link Supplier#get()}.
*
* @return The builder of the {@link TypeKeyProvider} to chain-edit the {@link TypeKeyProvider}, when the
* options are set call the {@link IdentifiersBuilder#buildProvider(Class)} to get the {@link TypeProvider}.
*/
@Contract("_, _ -> this")
public IdentifiersBuilder<T, I> addSupplierIdentifier(I identifier, SerializableSupplier<T> value) {
this.identifiersMap.put(identifier, new TypeSupplierProvider<>(value.getSupplierClass(), value));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ public void checkIfGottenTypeSameType4() {
Assertions.assertEquals(TestClass.class, register.getInitProvider(TestClass.class).getClass());
}

@Test
public void checkIfGottenTypeSameType7() {
Register register = new Register();
register.register(Supplier.class, (Supplier<TestClass>) TestClass::new);

Assertions.assertTrue(register.getInitProvider(Supplier.class) instanceof Supplier<?>);
}

@Test
public void checkIfGottenTypeSameType5() {
Register register = new Register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,46 @@ public void getInitProviderTest2() {
Assertions.assertEquals(carTwoExporter, typeKeyProvider.getInitProvider());
}

@Test
public void getInitProviderTest3() {
IdentifiersBuilder<Exporter, Exporters> identifiersBuilder = new IdentifiersBuilder<>();
identifiersBuilder.addSupplierIdentifier(Exporters.CAR_ONE, CarOneExporter::new);
identifiersBuilder.addSupplierIdentifier(Exporters.CAR_TWO, CarTwoExporter::new);

TypeKeyProvider<Exporter, Exporters> typeKeyProvider = identifiersBuilder.buildProvider(Exporter.class);

typeKeyProvider = typeKeyProvider.applyIdentifiers(Exporters.CAR_ONE);

Assertions.assertEquals(CarOneExporter.class, typeKeyProvider.getInitProvider().getClass());
Assertions.assertNotEquals(typeKeyProvider.getInitProvider(), typeKeyProvider.getInitProvider());

typeKeyProvider = typeKeyProvider.applyIdentifiers(Exporters.CAR_TWO);

Assertions.assertEquals(CarTwoExporter.class, typeKeyProvider.getInitProvider().getClass());
Assertions.assertNotEquals(typeKeyProvider.getInitProvider(), typeKeyProvider.getInitProvider());
}

@Test
public void getInitProviderTest4() {
IdentifiersBuilder<Exporter, Exporters> identifiersBuilder = new IdentifiersBuilder<>();
identifiersBuilder.addSupplierIdentifier(Exporters.CAR_ONE, CarOneExporter::new);
CarTwoExporter carTwoExporter = new CarTwoExporter();
identifiersBuilder.addIdentifier(Exporters.CAR_TWO, carTwoExporter);

TypeKeyProvider<Exporter, Exporters> typeKeyProvider = identifiersBuilder.buildProvider(Exporter.class);

typeKeyProvider = typeKeyProvider.applyIdentifiers(Exporters.CAR_ONE);

Assertions.assertEquals(CarOneExporter.class, typeKeyProvider.getInitProvider().getClass());
Assertions.assertNotEquals(typeKeyProvider.getInitProvider(), typeKeyProvider.getInitProvider());

typeKeyProvider = typeKeyProvider.applyIdentifiers(Exporters.CAR_TWO);

Assertions.assertEquals(carTwoExporter, typeKeyProvider.getInitProvider());
Assertions.assertEquals(typeKeyProvider.getInitProvider(), typeKeyProvider.getInitProvider());
}


@Test
public void getInitProviderFailTest() {
IdentifiersBuilder<Exporter, Class<? extends Exporter>> identifiersBuilder = new IdentifiersBuilder<>();
Expand Down Expand Up @@ -99,6 +139,7 @@ public void getInitProviderFailTest2() {
Assertions.assertThrows(ProviderException.class, typeKeyProvider::getInitProvider);
}


public enum Exporters {
CAR_ONE,
CAR_TWO
Expand Down

0 comments on commit 7fcca8a

Please sign in to comment.