/* * 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 com.google.common.base.Joiner; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import java.util.List; import java.util.Set; import org.opendaylight.yangtools.yang.data.api.codec.BitsCodec; import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit; final class BitsStringCodec extends TypeDefinitionAwareCodec, BitsTypeDefinition> implements BitsCodec { private static final Joiner JOINER = Joiner.on(" ").skipNulls(); private static final Splitter SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults(); private final Set bits; @SuppressWarnings("unchecked") private BitsStringCodec(final Optional typeDef) { super(typeDef, (Class>) ((Class) Set.class)); if (typeDef.isPresent()) { final List yangBits = typeDef.get().getBits(); final Set bitsBuilder = Sets.newHashSet(); for (final Bit bit : yangBits) { bitsBuilder.add(bit.getName()); } bits = ImmutableSet.copyOf(bitsBuilder); } else { bits = null; } } static TypeDefinitionAwareCodec from(final BitsTypeDefinition normalizedType) { return new BitsStringCodec(Optional.fromNullable(normalizedType)); } @Override public String serialize(final Set data) { return data == null ? "" : JOINER.join(data); } @Override public Set deserialize(final String stringRepresentation) { if (stringRepresentation == null) { return ImmutableSet.of(); } final Iterable strings = SPLITTER.split(stringRepresentation); validate(strings); return ImmutableSet.copyOf(strings); } private void validate(final Iterable strings) { if (bits != null) { for (final String bit : strings) { Preconditions.checkArgument(bits.contains(bit), "Invalid value '%s' for bits type. Allowed values are: %s", bit, bits); } } } }