Merge "Bug 2374 - YANG Binding: Added support for AugmentationHolder interface"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / StringStringCodec.java
1 /*
2  * Copyright (c) 2015 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.yangtools.yang.data.impl.codec;
9
10 import com.google.common.base.Optional;
11 import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
12 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
13
14 class StringStringCodec extends TypeDefinitionAwareCodec<String, StringTypeDefinition> implements
15         StringCodec<String> {
16
17     protected StringStringCodec(final StringTypeDefinition typeDef) {
18         super(Optional.of(typeDef), String.class);
19         typeDef.getLengthConstraints();
20     }
21
22     static TypeDefinitionAwareCodec<?, StringTypeDefinition> from(final StringTypeDefinition normalizedType) {
23         if (normalizedType.getPatternConstraints().isEmpty()) {
24             return new StringStringCodec(normalizedType);
25         }
26
27         return new StringPatternCheckingCodec(normalizedType);
28     }
29
30     @Override
31     public final String deserialize(final String stringRepresentation) {
32         if (stringRepresentation == null) {
33             // FIXME: These seems buggy, but someone may be using this behaviour
34             return "";
35         }
36         validate(stringRepresentation);
37         return stringRepresentation;
38     }
39
40     @Override
41     public final String serialize(final String data) {
42         return data == null ? "" : data;
43     }
44
45     protected void validate(final String s) {
46
47     }
48 }