/* * Copyright (c) 2016 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 static java.util.Objects.requireNonNull; import java.util.Arrays; import java.util.List; import java.util.Optional; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor; import org.opendaylight.yangtools.yang.common.Uint8; /** * The Convertor case used in * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor}. * * @param the source type * @param the result type * @param the data type */ public abstract class ConvertorCase { private final List supportedVersions; private final Class type; private final boolean errorOnEmpty; /** * Instantiates a new Convertor case. * * @param type the type * @param errorOnEmpty the error on empty * @param supportedVersions the supported versions */ protected ConvertorCase(final Class type, final boolean errorOnEmpty, final Uint8... supportedVersions) { this.type = type; this.errorOnEmpty = errorOnEmpty; this.supportedVersions = Arrays.asList(requireNonNull(supportedVersions)); } /** * Process source and return result, what can be empty. * * @param source the source * @param data the data * @param convertorExecutor convertor executor * @return the optional */ public abstract Optional process(@NonNull F source, D data, ConvertorExecutor convertorExecutor); /** * Should {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor} * throw error when result of process method is empty. * * @return the boolean */ boolean isErrorOnEmpty() { return errorOnEmpty; } /** * Cast untyped source to type of this case and sends it to actual process method. * * * @param source the source * @param data the data * @param convertorExecutor convertor executor * @return the optional */ Optional processRaw(@NonNull final Object source, final D data, final ConvertorExecutor convertorExecutor) { return process(getType().cast(source), data, convertorExecutor); } /** * Gets type of this convertor case. * * @return the type */ Class getType() { return type; } /** * Gets supported Openflow versions. * * @return the supported versions */ List getSupportedVersions() { return supportedVersions; } }