2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.Objects;
18 import java.util.Optional;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorData;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ParametrizedConvertor;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Manages various convertors and allows to use them all in one generic way
28 public class ConvertorManager {
29 private static final Logger LOG = LoggerFactory.getLogger(ConvertorManager.class);
30 private static ConvertorManager INSTANCE;
33 INSTANCE = new ConvertorManager();
34 // All convertors are registered here
37 // Actual convertor keys
38 private List<Class<?>> convertorKeys = new ArrayList<>();
39 private List<Class<?>> parametrizedConvertorKeys = new ArrayList<>();
41 // Cache, that holds all registered convertors, but they can have multiple keys,
42 // based on instanceof checks in the convert method
43 private Map<Class<?>, Convertor> convertors = new HashMap<>();
44 private Map<Class<?>, ParametrizedConvertor> parametrizedConvertors = new HashMap<>();
46 private ConvertorManager() {
47 // Hiding implicit constructor
51 * Gets instance of Convertor Manager.
53 * @return the instance
55 public static ConvertorManager getInstance() {
62 * @param convertor the convertor
63 * @return if registration was successful
65 public boolean registerConvertor(final Convertor convertor) {
66 final Class<?> type = convertor.getType();
68 if (convertors.containsKey(type)) {
69 LOG.warn("Convertor for type {} is already registered", type);
73 convertorKeys.add(type);
74 convertors.put(type, convertor);
75 LOG.debug("{} is now converted by {}", type, convertor);
82 * @param convertor the convertor
83 * @return if registration was successful
85 public boolean registerConvertor(final ParametrizedConvertor convertor) {
86 final Class<?> type = convertor.getType();
88 if (parametrizedConvertors.containsKey(type)) {
89 LOG.warn("Convertor for type {} is already registered", type);
93 parametrizedConvertorKeys.add(type);
94 parametrizedConvertors.put(type, convertor);
95 LOG.debug("{} is now converted by {}", type, convertor);
100 * Lookup and use convertor by specified type, then converts source and returns converted result
102 * @param <FROM> the source type
103 * @param <TO> the result type
104 * @param source the source
105 * @return the result (can be empty, if no convertor was found)
107 @SuppressWarnings("unchecked")
108 public <FROM, TO> Optional<TO> convert(final FROM source) {
109 if (Objects.isNull(source)) {
110 LOG.trace("Cannot convert source, because it is null");
111 return Optional.empty();
114 Class<?> type = source.getClass();
116 if (source instanceof Collection) {
117 final Iterator it = ((Collection) source).iterator();
124 if (Objects.isNull(next)) {
125 LOG.trace("Cannot convert {}, because it is empty collection", type);
126 return Optional.empty();
129 type = next.getClass();
132 Convertor convertor = null;
134 if (!convertors.containsKey(type)) {
135 boolean found = false;
137 for (Class<?> key : convertorKeys) {
138 if (key.isAssignableFrom(type)) {
139 convertor = convertors.get(key);
140 convertors.put(type, convertor);
141 LOG.debug("{} is now converted by {}", type, convertor);
148 LOG.error("Convertor for {} not found", type);
149 return Optional.empty();
153 if (Objects.isNull(convertor)) {
154 convertor = convertors.get(type);
157 final Object result = convertor.convert(source);
158 return Optional.of((TO) result);
162 * Lookup and use convertor by specified type, then converts source and returns converted result
164 * @param <FROM> the source type
165 * @param <TO> the result type
166 * @param <DATA> the data type
167 * @param source the source
168 * @param data convertor data
169 * @return the result (can be empty, if no convertor was found)
171 @SuppressWarnings("unchecked")
172 public <FROM, TO, DATA extends ConvertorData> Optional<TO> convert(final FROM source, final DATA data) {
173 if (Objects.isNull(source)) {
174 LOG.trace("Cannot convert source, because it is null");
175 return Optional.empty();
178 Class<?> type = source.getClass();
180 if (source instanceof Collection) {
181 final Iterator it = ((Collection) source).iterator();
188 if (Objects.isNull(next)) {
189 LOG.trace("Cannot convert {}, because it is empty collection", type);
190 return Optional.empty();
193 type = next.getClass();
196 ParametrizedConvertor convertor = null;
198 if (!parametrizedConvertors.containsKey(type)) {
199 boolean found = false;
201 for (Class<?> key : parametrizedConvertorKeys) {
202 if (key.isAssignableFrom(type)) {
203 convertor = parametrizedConvertors.get(key);
204 parametrizedConvertors.put(type, convertor);
205 LOG.debug("{} is now converted by {}", type, convertor);
212 LOG.error("Convertor for {} not found", type);
213 return Optional.empty();
217 if (Objects.isNull(convertor)) {
218 convertor = parametrizedConvertors.get(type);
221 final Object result = convertor.convert(source, data);
222 return Optional.of((TO) result);