2 * Copyright (c) 2013 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.common;
11 import java.util.HashMap;
15 * @param <FROM> source type for conversion
18 public abstract class ConvertReactor<FROM> {
20 private Map<Short, Convertor<FROM, ?>> conversionMapping;
21 private Map<InjectionKey, ResultInjector<?, ?>> injectionMapping;
23 protected ConvertReactor() {
24 conversionMapping = new HashMap<>();
25 injectionMapping = new HashMap<>();
26 initMappings(conversionMapping, injectionMapping);
30 * fill conversion and injection mappings
34 protected abstract void initMappings(Map<Short, Convertor<FROM, ?>> conversions,
35 Map<InjectionKey, ResultInjector<?, ?>> injections);
42 @SuppressWarnings("unchecked")
43 public <RESULT, TARGET> void convert(FROM source, short version, TARGET target) {
49 Convertor<FROM, RESULT> convertor = (Convertor<FROM, RESULT>) conversionMapping.get(version);
50 if (convertor == null) {
51 throw new IllegalArgumentException("convertor for given version ["+version+"] not found");
53 RESULT convertedItem = convertor.convert(source);
56 InjectionKey key = new InjectionKey(version, target.getClass().getName());
57 ResultInjector<RESULT, TARGET> injection = (ResultInjector<RESULT, TARGET>) injectionMapping.get(key);
58 if (injection == null) {
59 throw new IllegalArgumentException("injector for given version and target ["+key+"] not found");
61 injection.inject(convertedItem, target);