Package: ImplementationFactory
ImplementationFactory
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ImplementationFactory(Class) |
|
|
|
|
|
||||||||||||||||||||
createImageModel(BufferedImage) |
|
|
|
|
|
||||||||||||||||||||
findImplementation(Operation) |
|
|
|
|
|
||||||||||||||||||||
getModelClass() |
|
|
|
|
|
||||||||||||||||||||
registerImplementation(Class, Class) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
||||||||||||||||||||
unregisterImplementation(Class) |
|
|
|
|
|
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.HashMap;
31: import java.util.Map;
32: import java.awt.image.BufferedImage;
33: import it.tidalwave.image.ImageModel;
34: import lombok.Getter;
35: import lombok.RequiredArgsConstructor;
36: import lombok.ToString;
37: import lombok.extern.slf4j.Slf4j;
38:
39: /***********************************************************************************************************************
40: *
41: * @author Fabrizio Giudici
42: *
43: **********************************************************************************************************************/
44:•@RequiredArgsConstructor @ToString(of = "modelClass") @Slf4j
45: public abstract class ImplementationFactory
46: {
47: @Getter @Nonnull
48: private final Class modelClass;
49:
50: private final Map<Class<? extends Operation>, Class<? extends OperationImplementation>> implementationMapping =
51: new HashMap<>();
52:
53: /*******************************************************************************************************************
54: *
55: *
56: ******************************************************************************************************************/
57: public void registerImplementation (@Nonnull final Class<? extends Operation> operationClass,
58: @Nonnull final Class<? extends OperationImplementation> implementationClass)
59: {
60: implementationMapping.put(operationClass, implementationClass);
61: }
62:
63: /*******************************************************************************************************************
64: *
65: *
66: ******************************************************************************************************************/
67: public void unregisterImplementation (@Nonnull final Class<? extends Operation> operationClass)
68: {
69: implementationMapping.remove(operationClass);
70: }
71:
72: /*******************************************************************************************************************
73: *
74: * Finds the concrete implementation for a given operation.
75: *
76: * @param operation operation
77: * @return the implementation (null if not supported)
78: *
79: ******************************************************************************************************************/
80: @Nonnull
81: public OperationImplementation<Operation, Object> findImplementation (@Nonnull final Operation operation)
82: {
83: final var implementationClass =
84: (Class<OperationImplementation<Operation, Object>>)implementationMapping.get(operation.getClass());
85:
86:• if (implementationClass != null)
87: {
88: try
89: {
90: final var implementation = implementationClass.newInstance();
91: // FIXME: drop these setters and pass to the constructor, so the object is truly immutable
92: implementation.setFactory(this);
93: implementation.bind(operation);
94:
95: return implementation;
96: }
97: catch (IllegalAccessException | InstantiationException e)
98: {
99: log.error("findImplementation()", e);
100: }
101: }
102:
103: return null;
104: }
105:
106: /*******************************************************************************************************************
107: *
108: *
109: ******************************************************************************************************************/
110: @Nonnull
111: public ImageModel createImageModel (@Nonnull final BufferedImage image)
112: {
113: throw new UnsupportedOperationException();
114: }
115:
116: /*******************************************************************************************************************
117: *
118: * Return true if we can convert the given imageClass into our specific image
119: * class.
120: *
121: ******************************************************************************************************************/
122: public abstract boolean canConvertFrom (@Nonnull Class imageClass);
123:
124: /*******************************************************************************************************************
125: *
126: * Converts the given image into our specific image representation.
127: *
128: ******************************************************************************************************************/
129: @Nonnull
130: public abstract ImageModel convertFrom (@Nonnull Object image);
131:
132: /*******************************************************************************************************************
133: *
134: *
135: ******************************************************************************************************************/
136: public abstract boolean canConvertTo (@Nonnull Class<?> imageClass);
137:
138: /*******************************************************************************************************************
139: *
140: *
141: ******************************************************************************************************************/
142: @Nonnull
143: public abstract Object convertTo (@Nonnull Object image);
144: }