Bug 5540 - ConvertorManager base
[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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common;
10
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;
16
17 /**
18  * The Convertor case used in {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor}.
19  *
20  * @param <FROM> the source type
21  * @param <TO>   the result type
22  * @param <DATA> the data type
23  */
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;
28
29     /**
30      * Instantiates a new Convertor case.
31      *
32      * @param type              the type
33      * @param errorOnEmpty      the error on empty
34      * @param supportedVersions the supported versions
35      */
36     protected ConvertorCase(Class<FROM> type, boolean errorOnEmpty, Short... supportedVersions) {
37         this.type = type;
38         this.errorOnEmpty = errorOnEmpty;
39         this.supportedVersions = Arrays.asList(Preconditions.checkNotNull(supportedVersions));
40     }
41
42     /**
43      * Process source and return result, what can be empty
44      *
45      * @param source the source
46      * @param data   the data
47      * @return the optional
48      */
49     public abstract Optional<TO> process(@Nonnull final FROM source, final DATA data);
50
51     /**
52      * Should {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor}
53      * throw error when result of process method is empty?
54      *
55      * @return the boolean
56      */
57     boolean isErrorOnEmpty() {
58         return errorOnEmpty;
59     }
60
61     /**
62      * Cast untyped source to type of this case and sends it to actual process method.
63      *
64      * @param source the source
65      * @param data   the data
66      * @return the optional
67      */
68     Optional<TO> processRaw(@Nonnull final Object source, final DATA data) {
69         return process(getType().cast(source), data);
70     }
71
72     /**
73      * Gets type of this convertor case.
74      *
75      * @return the type
76      */
77     Class<FROM> getType() {
78         return type;
79     }
80
81     /**
82      * Gets supported Openflow versions.
83      *
84      * @return the supported versions
85      */
86     List<Short> getSupportedVersions() {
87         return supportedVersions;
88     }
89 }