Skip to content

Package: ImplementationFactoryRegistry

ImplementationFactoryRegistry

nameinstructionbranchcomplexitylinemethod
ImplementationFactoryRegistry()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createImageModel(Object)
M: 8 C: 19
70%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 3
60%
M: 0 C: 1
100%
findImplementation(Operation, ImageModel, boolean)
M: 37 C: 64
63%
M: 11 C: 7
39%
M: 7 C: 3
30%
M: 9 C: 13
59%
M: 0 C: 1
100%
getDefault()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getFactories()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$getDefault$0()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
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%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 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/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.op;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Collection;
31: import java.util.ServiceLoader;
32: import java.util.stream.Collectors;
33: import java.util.stream.StreamSupport;
34: import it.tidalwave.image.ImageModel;
35: import lombok.extern.slf4j.Slf4j;
36:
37: /***********************************************************************************************************************
38: *
39: * @author Fabrizio Giudici
40: *
41: **********************************************************************************************************************/
42: @Slf4j
43: public class ImplementationFactoryRegistry
44: {
45: @Nonnull
46: public static ImplementationFactoryRegistry getDefault()
47: {
48: var loader = ServiceLoader.load(ImplementationFactoryRegistry.class);
49: return StreamSupport.stream(loader.spliterator(), false)
50: .findFirst()
51: .orElseThrow(() -> new RuntimeException("Can't found implementation of " + ImplementationFactory.class));
52: }
53:
54: /*******************************************************************************************************************
55: *
56: *
57: ******************************************************************************************************************/
58: @Nonnull
59: public ImageModel createImageModel (@Nonnull final Object image)
60: {
61:• for (final var factory : getFactories())
62: {
63:• if (factory.canConvertFrom(image.getClass()))
64: {
65: return factory.convertFrom(image);
66: }
67: }
68:
69: throw new IllegalArgumentException("Cannot create image model from " + image.getClass());
70: }
71:
72: /*******************************************************************************************************************
73: *
74: * Finds the concrete implementation for a given operation. If canConvert is false, only the first strictly
75: * compatible implementations is returned; otherwise the first implementation that is compatible through a
76: * conversion is returned.
77: *
78: * @param operation operation
79: * @param imageModel the imageModel
80: * @param canConvert is image conversion acceptable?
81: * @return the implementation
82: * @throws UnsupportedOperationException if no implementation has been found
83: *
84: ******************************************************************************************************************/
85: @Nonnull
86: public OperationImplementation findImplementation (@Nonnull final Operation operation,
87: @Nonnull final ImageModel imageModel,
88: final boolean canConvert)
89: {
90: log.trace("findImplementation({}, {}, canConvert: {})", operation, imageModel, canConvert);
91:
92:• final var image = (imageModel != null) ? imageModel.getImage() : null;
93: final Collection<? extends ImplementationFactory> factories = getFactories();
94:
95:• for (final ImplementationFactory factory : factories)
96: {
97: final OperationImplementation implementation = factory.findImplementation(operation);
98:
99:• if (implementation != null)
100: {
101: log.trace(">>>> found implementation from {}", factory);
102:
103:• if (!implementation.canHandle(operation))
104: {
105: log.trace(">>>> but can't handle this specific op, discarded");
106: continue;
107: }
108:
109:• if (image == null) // createOp
110: {
111: return implementation;
112: }
113:
114: log.trace(">>>> image class: {}, factory model class: {}", image.getClass(), factory.getModelClass());
115:
116:• if (factory.getModelClass().isAssignableFrom(image.getClass()))
117: {
118: return implementation;
119: }
120:
121:• if (canConvert
122:• && (factory.canConvertFrom(image.getClass()) ||
123:• imageModel.getFactory().canConvertTo(factory.getModelClass())))
124: {
125: return implementation;
126: }
127:
128: log.trace(">>>> but has been discarded");
129: }
130: }
131:
132: throw new UnsupportedOperationException("Not implemented " + operation + ", imageModel: " + imageModel
133: + " factoryList: " + factories);
134: }
135:
136: /*******************************************************************************************************************
137: *
138: *
139: ******************************************************************************************************************/
140: @Nonnull
141: private static Collection<ImplementationFactory> getFactories()
142: {
143: var loader = ServiceLoader.load(ImplementationFactory.class);
144: return StreamSupport.stream(loader.spliterator(), false).collect(Collectors.toList());
145: }
146: }