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