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.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.Objects;
14 import org.opendaylight.yangtools.yang.data.api.codec.BooleanCodec;
15 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
18 * Do not use this class outside of yangtools, its presence does not fall into the API stability contract.
21 public final class BooleanStringCodec extends TypeDefinitionAwareCodec<Boolean, BooleanTypeDefinition>
22 implements BooleanCodec<String> {
24 private BooleanStringCodec(final Optional<BooleanTypeDefinition> typeDef) {
25 super(typeDef, Boolean.class);
29 public String serialize(final Boolean data) {
30 return Objects.toString(data, "");
34 public Boolean deserialize(final String stringRepresentation) {
35 if (stringRepresentation == null) {
38 Preconditions.checkArgument("true".equalsIgnoreCase(stringRepresentation)
39 || "false".equalsIgnoreCase(stringRepresentation),
40 "Invalid value '%s' for boolean type. Allowed values are true and false", stringRepresentation);
41 return Boolean.valueOf(stringRepresentation);
44 public static BooleanStringCodec from(final BooleanTypeDefinition normalizedType) {
45 return new BooleanStringCodec(Optional.of(normalizedType));