Skip to content

Package: EntitySupport

EntitySupport

nameinstructionbranchcomplexitylinemethod
EntitySupport()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
EntitySupport(Collection)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
EntitySupport(Object)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
findLoadable(Entity, FileEntity)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
findMimeType(FileEntity)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getFromLoadables(Entity, Function)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getSupportedExtensions(Entity)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getSupportedMimeTypes(Entity)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$findLoadable$3(FileEntity, Loadable)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$findMimeType$2(FileEntity, Loadable)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$getFromLoadables$4(Function, Loadable)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$of$0(FileEntity, Loadable)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$of$1(FileEntity)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(Entity, FileEntity)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * blueMarine III: Semantic DAM
5: * http://tidalwave.it/projects/bluemarine3
6: *
7: * Copyright (C) 2024 - 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/bluemarine3-src
22: * git clone https://github.com/tidalwave-it/bluemarine3-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.dam.model.spi;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.Collection;
30: import java.util.Optional;
31: import java.util.Set;
32: import java.util.function.Function;
33: import it.tidalwave.dam.model.Entity;
34: import it.tidalwave.dam.model.MimeType;
35: import it.tidalwave.dam.model.io.FileEntity;
36: import it.tidalwave.dam.model.role.Loadable;
37: import it.tidalwave.util.As;
38: import lombok.ToString;
39: import lombok.experimental.Delegate;
40: import static it.tidalwave.dam.model.role.Loadable._Loadable_;
41: import static java.util.stream.Collectors.*;
42: import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;
43:
44: /***************************************************************************************************************************************************************
45: *
46: * A convenience support implementation for {@link Entity}.
47: *
48: * @see EntitySupport
49: * @author Fabrizio Giudici
50: *
51: **************************************************************************************************************************************************************/
52: @ToString(exclude = "asDelegate")
53: public abstract class EntitySupport implements As, Entity
54: {
55: /** Support object for implementing {@link As} functions.*/
56: @Delegate @Nonnull
57: private final As asDelegate;
58:
59: /***********************************************************************************************************************************************************
60: * {@return a new instance loaded from the given file}. Each concrete implementation of {@link Entity} will provide its own {@code of(FileEntity)} method
61: * that will pass the proper prototype. This method scans all the {@link Loadable}s related the prototype, let them probe the file and use the first that
62: * positively responds.
63: * @param prototype the prototype
64: * @param file the file
65: **********************************************************************************************************************************************************/
66: @Nonnull
67: public static <T extends Entity> T of (@Nonnull final T prototype, @Nonnull final FileEntity file)
68: {
69: return findLoadable(prototype, file).map(_f(l -> l.load(file))).orElseThrow(() -> new RuntimeException("No loadable for " + file));
70: }
71:
72: /***********************************************************************************************************************************************************
73: * {@return the supported MIME types}.
74: * @param prototype the prototype
75: **********************************************************************************************************************************************************/
76: @Nonnull
77: public static Set<MimeType> getSupportedMimeTypes (@Nonnull final Entity prototype)
78: {
79: return getFromLoadables(prototype, Loadable::getSupportedMimeTypes);
80: }
81:
82: /***********************************************************************************************************************************************************
83: * {@return the supported extensions}.
84: * @param prototype the prototype
85: **********************************************************************************************************************************************************/
86: @Nonnull
87: public static Set<String> getSupportedExtensions (@Nonnull final Entity prototype)
88: {
89: return getFromLoadables(prototype, Loadable::getSupportedExtensions);
90: }
91:
92: /***********************************************************************************************************************************************************
93: * {@return the MIME type for the given file}.
94: * @param file the file
95: **********************************************************************************************************************************************************/
96: @Nonnull
97: protected static Optional<MimeType> findMimeType (@Nonnull final FileEntity file)
98: {
99: return findLoadable(file, file).flatMap(l -> l.getMimeType(file));
100: }
101:
102: /***********************************************************************************************************************************************************
103: * Creates a new instance with no static roles.
104: **********************************************************************************************************************************************************/
105: @SuppressWarnings("this-escape")
106: protected EntitySupport()
107: {
108: asDelegate = As.forObject(this);
109: }
110:
111: /***********************************************************************************************************************************************************
112: * Creates a new instance with the given static role.
113: * @param role the role
114: **********************************************************************************************************************************************************/
115: @SuppressWarnings("this-escape")
116: protected EntitySupport (@Nonnull final Object role)
117: {
118: asDelegate = As.forObject(this, role);
119: }
120:
121: /***********************************************************************************************************************************************************
122: * Creates a new instance with the given static roles.
123: * @param roles the roles
124: **********************************************************************************************************************************************************/
125: @SuppressWarnings("this-escape")
126: protected EntitySupport (@Nonnull final Collection<Object> roles)
127: {
128: asDelegate = As.forObject(this, roles);
129: }
130:
131: /***********************************************************************************************************************************************************
132: * {@return a {@Loadable} associated to the given {@link Entity} and {@link FileEntity}.
133: * @param <T> the type of the entity
134: * @param entity the entity
135: * @param file the file
136: **********************************************************************************************************************************************************/
137: @Nonnull
138: private static <T extends Entity> Optional<Loadable<T>> findLoadable (@Nonnull final T entity, @Nonnull final FileEntity file)
139: {
140: final As.Type<Loadable<T>> loadable = As.type(Loadable.class);
141: return entity.asMany(loadable).stream().filter(l -> l.probe(file)).findFirst();
142: }
143:
144: /***********************************************************************************************************************************************************
145: * {@return the supported extensions}.
146: **********************************************************************************************************************************************************/
147: @Nonnull
148: private static <T> Set<T> getFromLoadables (@Nonnull final Entity prototype, @Nonnull final Function<? super Loadable<?>, ? extends Collection<T>> function)
149: {
150: return prototype.asMany(_Loadable_).stream().flatMap(l -> function.apply(l).stream()).collect(toSet());
151: }
152: }