2 * Copyright (c) 2015 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
8 package org.opendaylight.yangtools.yang.data.impl.codec;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.data.api.codec.BooleanCodec;
13 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
15 class BooleanStringCodec extends TypeDefinitionAwareCodec<Boolean, BooleanTypeDefinition>
16 implements BooleanCodec<String> {
18 protected BooleanStringCodec(final Optional<BooleanTypeDefinition> typeDef) {
19 super(typeDef, Boolean.class);
23 public final String serialize(final Boolean data) {
24 return data == null ? "" : data.toString();
28 public final Boolean deserialize(final String stringRepresentation) {
29 if (stringRepresentation == null) {
32 validate(stringRepresentation);
33 return Boolean.valueOf(stringRepresentation);
36 private static void validate(final String string) {
37 Preconditions.checkArgument(string.toLowerCase().equals("true")
38 || string.toLowerCase().equals("false"),
39 "Invalid value '%s' for boolean type. Allowed values are true and false", string);
42 static TypeDefinitionAwareCodec<?,BooleanTypeDefinition> from(final BooleanTypeDefinition normalizedType) {
43 return new BooleanStringCodec(Optional.fromNullable(normalizedType));