Populate data/ hierarchy
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / BooleanStringCodec.java
diff --git a/data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/BooleanStringCodec.java b/data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/BooleanStringCodec.java
new file mode 100644 (file)
index 0000000..643b4a8
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.codec;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.data.api.codec.BooleanCodec;
+import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
+
+/**
+ * Do not use this class outside of yangtools, its presence does not fall into the API stability contract.
+ */
+@Beta
+public final class BooleanStringCodec extends TypeDefinitionAwareCodec<Boolean, BooleanTypeDefinition>
+        implements BooleanCodec<String> {
+    private BooleanStringCodec(final @NonNull BooleanTypeDefinition typeDef) {
+        super(typeDef, Boolean.class);
+    }
+
+    public static @NonNull BooleanStringCodec from(final BooleanTypeDefinition normalizedType) {
+        return new BooleanStringCodec(requireNonNull(normalizedType));
+    }
+
+    @Override
+    protected Boolean deserializeImpl(final String product) {
+        switch (product) {
+            case "true":
+                return Boolean.TRUE;
+            case "false":
+                return Boolean.FALSE;
+            default:
+                throw new IllegalArgumentException("Invalid value '" + product + "' for boolean type. Allowed values "
+                    + "are 'true' and 'false'");
+        }
+    }
+
+    @Override
+    protected String serializeImpl(final Boolean input) {
+        return input.toString();
+    }
+}