Enforce modernizer in openflowplugin
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / common / ConvertorCase.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common;
9
10 import static java.util.Objects.requireNonNull;
11
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;
17
18 /**
19  * The Convertor case used in
20  * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor}.
21  *
22  * @param <F> the source type
23  * @param <T>   the result type
24  * @param <D> the data type
25  */
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;
30
31     /**
32      * Instantiates a new Convertor case.
33      *
34      * @param type              the type
35      * @param errorOnEmpty      the error on empty
36      * @param supportedVersions the supported versions
37      */
38     protected ConvertorCase(final Class<F> type, final boolean errorOnEmpty, final Short... supportedVersions) {
39         this.type = type;
40         this.errorOnEmpty = errorOnEmpty;
41         this.supportedVersions = Arrays.asList(requireNonNull(supportedVersions));
42     }
43
44     /**
45      * Process source and return result, what can be empty.
46      *
47      * @param source the source
48      * @param data   the data
49      * @param convertorExecutor convertor executor
50      * @return the optional
51      */
52     public abstract Optional<T> process(@NonNull F source, D data, ConvertorExecutor convertorExecutor);
53
54     /**
55      * Should {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor}
56      * throw error when result of process method is empty.
57      *
58      * @return the boolean
59      */
60     boolean isErrorOnEmpty() {
61         return errorOnEmpty;
62     }
63
64     /**
65      * Cast untyped source to type of this case and sends it to actual process method.
66      *
67      *
68      * @param source the source
69      * @param data   the data
70      * @param convertorExecutor convertor executor
71      * @return the optional
72      */
73     Optional<T> processRaw(@NonNull final Object source, final D data, final ConvertorExecutor convertorExecutor) {
74         return process(getType().cast(source), data, convertorExecutor);
75     }
76
77     /**
78      * Gets type of this convertor case.
79      *
80      * @return the type
81      */
82     Class<F> getType() {
83         return type;
84     }
85
86     /**
87      * Gets supported Openflow versions.
88      *
89      * @return the supported versions
90      */
91     List<Short> getSupportedVersions() {
92         return supportedVersions;
93     }
94 }