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
8 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Arrays;
13 import java.util.List;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
19 * The Convertor case used in
20 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor}.
22 * @param <F> the source type
23 * @param <T> the result type
24 * @param <D> the data type
26 public abstract class ConvertorCase<F, T, D extends ConvertorData> {
27 private final List<Short> supportedVersions;
28 private final Class<F> type;
29 private final boolean errorOnEmpty;
32 * Instantiates a new Convertor case.
34 * @param type the type
35 * @param errorOnEmpty the error on empty
36 * @param supportedVersions the supported versions
38 protected ConvertorCase(final Class<F> type, final boolean errorOnEmpty, final Short... supportedVersions) {
40 this.errorOnEmpty = errorOnEmpty;
41 this.supportedVersions = Arrays.asList(requireNonNull(supportedVersions));
45 * Process source and return result, what can be empty.
47 * @param source the source
48 * @param data the data
49 * @param convertorExecutor convertor executor
50 * @return the optional
52 public abstract Optional<T> process(@NonNull F source, D data, ConvertorExecutor convertorExecutor);
55 * Should {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor}
56 * throw error when result of process method is empty.
60 boolean isErrorOnEmpty() {
65 * Cast untyped source to type of this case and sends it to actual process method.
68 * @param source the source
69 * @param data the data
70 * @param convertorExecutor convertor executor
71 * @return the optional
73 Optional<T> processRaw(@NonNull final Object source, final D data, final ConvertorExecutor convertorExecutor) {
74 return process(getType().cast(source), data, convertorExecutor);
78 * Gets type of this convertor case.
87 * Gets supported Openflow versions.
89 * @return the supported versions
91 List<Short> getSupportedVersions() {
92 return supportedVersions;