Simplify CompositeValueCodec instantiation
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / CompositeValueCodec.java
1 /*
2  * Copyright (c) 2014 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.mdsal.binding.dom.codec.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
13
14 final class CompositeValueCodec extends ValueTypeCodec {
15     private final EncapsulatedValueCodec typeObjectCodec;
16     @SuppressWarnings("rawtypes")
17     // FIXME: specialize for the two possibilities
18     private final IllegalArgumentCodec valueCodec;
19
20     CompositeValueCodec(final Class<?> valueType, final IdentityCodec codec) {
21         typeObjectCodec = EncapsulatedValueCodec.ofUnchecked(valueType);
22         valueCodec = requireNonNull(codec);
23     }
24
25     CompositeValueCodec(final Class<?> valueType, final InstanceIdentifierCodec codec) {
26         typeObjectCodec = EncapsulatedValueCodec.ofUnchecked(valueType);
27         valueCodec = requireNonNull(codec);
28     }
29
30     @SuppressWarnings("unchecked")
31     @Override
32     public Object deserialize(final Object input) {
33         return typeObjectCodec.deserialize(valueCodec.deserialize(input));
34     }
35
36     @SuppressWarnings("unchecked")
37     @Override
38     public Object serialize(final Object input) {
39         return valueCodec.serialize(typeObjectCodec.serialize(input));
40     }
41 }