Package: EntitySelectedEvent
EntitySelectedEvent
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
getEntity(Class) |
|
|
|
|
|
||||||||||||||||||||
of(FileEntity, Class, Supplier) |
|
|
|
|
|
||||||||||||||||||||
of(MimeType, Class, Supplier) |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
Coverage
1: package it.tidalwave.dam.ui.message;
2:
3: import jakarta.annotation.Nonnull;
4: import java.util.Optional;
5: import java.util.function.Supplier;
6: import it.tidalwave.dam.model.Entity;
7: import it.tidalwave.dam.model.MimeType;
8: import it.tidalwave.dam.model.io.FileEntity;
9: import lombok.Getter;
10: import lombok.RequiredArgsConstructor;
11: import static lombok.AccessLevel.PROTECTED;
12:
13: /***************************************************************************************************************************************************************
14: *
15: * A message that notifies that an {@link Entity} has been selected.
16: *
17: * @stereotype Message
18: * @author Fabrizio Giudici
19: *
20: **************************************************************************************************************************************************************/
21: @RequiredArgsConstructor(access = PROTECTED) @Getter
22: public class EntitySelectedEvent
23: {
24: @Getter @Nonnull
25: private final MimeType mimeType;
26:
27: @Getter @Nonnull
28: private final Class<? extends Entity> entityType;
29:
30: @Nonnull
31: private final Supplier<? extends Entity> entitySupplier;
32:
33: /***********************************************************************************************************************************************************
34: * {@return a new message for the given entity type of the given {@link MimeType}}.
35: * @param <T> the static type of the entity
36: * @param entityType the type of the entity
37: * @param mimeType the MIME type
38: * @param entitySupplier a {@link Supplier} of the entity (for lazy instantiation)
39: **********************************************************************************************************************************************************/
40: @Nonnull
41: public static <T extends Entity> EntitySelectedEvent of (@Nonnull final MimeType mimeType,
42: @Nonnull final Class<T> entityType,
43: @Nonnull final Supplier<T> entitySupplier)
44: {
45: return new EntitySelectedEvent(mimeType, entityType, entitySupplier);
46: }
47:
48: /***********************************************************************************************************************************************************
49: * {@return a new message for the given entity type with the MIME type from the given {@link FileEntity}}.
50: * @param <T> the static type of the entity
51: * @param entityType the type of the entity
52: * @param file the file
53: * @param entitySupplier a {@link Supplier} of the entity (for lazy instantiation)
54: **********************************************************************************************************************************************************/
55: @Nonnull
56: public static <T extends Entity> EntitySelectedEvent of (@Nonnull final FileEntity file,
57: @Nonnull final Class<T> entityType,
58: @Nonnull final Supplier<T> entitySupplier)
59: {
60: return of(file.getMimeType().orElseThrow(), entityType, entitySupplier);
61: }
62:
63: /***********************************************************************************************************************************************************
64: * {@return the {@link Entity} in this message of the given type}, or an empty {@link Optional} if the type doesn't match.
65: * @param <T> the static type of the entity
66: * @param entityType the type of the entity
67: **********************************************************************************************************************************************************/
68: @Nonnull
69: public final <T extends Entity> Optional<T> getEntity (@Nonnull final Class<T> entityType)
70: {
71:• return getEntityType().isAssignableFrom(entityType) ? Optional.of(entityType.cast(entitySupplier.get())) : Optional.empty();
72: }
73:
74: /***********************************************************************************************************************************************************
75: * {@inheritDoc}
76: **********************************************************************************************************************************************************/
77: @Override @Nonnull
78: public String toString()
79: {
80: return String.format("%s(%s)", getClass().getSimpleName(), mimeType);
81: }
82: }