/** * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common; import java.math.BigInteger; import java.util.HashMap; import java.util.Map; import com.google.common.collect.ImmutableMap; /** * @param source type for conversion * */ public abstract class ConvertReactor { private final Map> injectionMapping; private final Map> conversionMapping; protected ConvertReactor() { final Map> conversions = new HashMap<>(); final Map> injections = new HashMap<>(); initMappings(conversions, injections); // Create optimized view of mappings this.conversionMapping = ImmutableMap.copyOf(conversions); this.injectionMapping = ImmutableMap.copyOf(injections); } /** * fill conversion and injection mappings * @param conversions * @param injections */ protected abstract void initMappings(Map> conversions, Map> injections); /** * @param source * @param version * @param target * @param datapathid */ @SuppressWarnings("unchecked") public void convert(final FROM source, final short version, final TARGET target, final BigInteger datapathid) { //lookup converter Convertor convertor = (Convertor) conversionMapping.get(version); if (convertor == null) { throw new IllegalArgumentException("convertor for given version ["+version+"] not found"); } RESULT convertedItem = convertor.convert(source,datapathid); //lookup injection InjectionKey key = buildInjectionKey(version, convertedItem, target); ResultInjector injection = (ResultInjector) injectionMapping.get(key); if (injection == null) { throw new IllegalArgumentException("injector for given version and target ["+key+"] not found"); } injection.inject(convertedItem, target); } /** * @param version * @param convertedItem to be injected * @param target object * @return */ protected InjectionKey buildInjectionKey(final short version, final Object convertedItem, final Object target) { return new InjectionKey(version, target.getClass()); } }