/* * Copyright (c) 2014 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.mdsal.binding.dom.codec.impl; import java.lang.reflect.Method; import java.util.List; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode; class ListNodeCodecContext extends DataObjectCodecContext { ListNodeCodecContext(final DataContainerCodecPrototype prototype) { super(prototype); } ListNodeCodecContext(final DataContainerCodecPrototype prototype, final Method keyMethod) { super(prototype, keyMethod); } @Override public D deserialize(final NormalizedNode node) { if (node instanceof MapEntryNode) { return createBindingProxy((MapEntryNode) node); } else if (node instanceof UnkeyedListEntryNode) { return createBindingProxy((UnkeyedListEntryNode) node); } else { throw new IllegalStateException("Unsupported data type " + node.getClass()); } } @Override protected Object deserializeObject(final NormalizedNode node) { if (node instanceof MapNode) { return fromMap((MapNode) node); } else if (node instanceof MapEntryNode) { return createBindingProxy((MapEntryNode) node); } else if (node instanceof UnkeyedListNode) { return fromUnkeyedList((UnkeyedListNode) node); } else if (node instanceof UnkeyedListEntryNode) { return createBindingProxy((UnkeyedListEntryNode) node); } else { throw new IllegalStateException("Unsupported data type " + node.getClass()); } } @NonNull Object fromMap(final MapNode map, final int size) { return LazyBindingList.create(this, size, map.body()); } private Object fromMap(final MapNode map) { final int size; // This should never happen, but we do need to ensure users never see an empty Map return (size = map.size()) == 0 ? null : fromMap(map, size); } private List fromUnkeyedList(final UnkeyedListNode node) { final int size; // This should never happen, but we do need to ensure users never see an empty List return (size = node.size()) == 0 ? null : LazyBindingList.create(this, size, node.body()); } }