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.common;
11 import com.google.common.base.Preconditions;
12 import java.util.Arrays;
13 import java.util.List;
14 import java.util.Optional;
15 import javax.annotation.Nonnull;
18 * The Convertor case used in {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor}.
20 * @param <FROM> the source type
21 * @param <TO> the result type
22 * @param <DATA> the data type
24 public abstract class ConvertorCase<FROM, TO, DATA extends ConvertorData> {
25 private final List<Short> supportedVersions;
26 private final Class<FROM> type;
27 private final boolean errorOnEmpty;
30 * Instantiates a new Convertor case.
32 * @param type the type
33 * @param errorOnEmpty the error on empty
34 * @param supportedVersions the supported versions
36 protected ConvertorCase(Class<FROM> type, boolean errorOnEmpty, Short... supportedVersions) {
38 this.errorOnEmpty = errorOnEmpty;
39 this.supportedVersions = Arrays.asList(Preconditions.checkNotNull(supportedVersions));
43 * Process source and return result, what can be empty
45 * @param source the source
46 * @param data the data
47 * @return the optional
49 public abstract Optional<TO> process(@Nonnull final FROM source, final DATA data);
52 * Should {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor}
53 * throw error when result of process method is empty?
57 boolean isErrorOnEmpty() {
62 * Cast untyped source to type of this case and sends it to actual process method.
64 * @param source the source
65 * @param data the data
66 * @return the optional
68 Optional<TO> processRaw(@Nonnull final Object source, final DATA data) {
69 return process(getType().cast(source), data);
73 * Gets type of this convertor case.
77 Class<FROM> getType() {
82 * Gets supported Openflow versions.
84 * @return the supported versions
86 List<Short> getSupportedVersions() {
87 return supportedVersions;