2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.schema.nodes;
10 import static java.util.Objects.requireNonNull;
12 import java.util.AbstractCollection;
13 import java.util.Iterator;
15 import java.util.Map.Entry;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
19 // This is *almost* the same as Guava's TransformedCollection. The main difference is delegation of hashCode()/equals()
20 // towards the backing map. This is needed because we do not retain a reference to this object and thus
21 // NormalizedNode.getValue() does not compare as equal. When invoked twice and lazy leaves are in effect. Note that
22 // Collection.equals() is undefined, but the expectation from users is that we will return the same view object, which
23 // equals on identity.
24 final class LazyValues extends AbstractCollection<DataContainerChild> {
25 private final Map<PathArgument, Object> map;
27 LazyValues(final Map<PathArgument, Object> map) {
28 this.map = requireNonNull(map);
37 public boolean isEmpty() {
42 public Iterator<DataContainerChild> iterator() {
43 return new Iter(map.entrySet().iterator());
47 public int hashCode() {
48 return map.hashCode();
52 public boolean equals(final Object obj) {
53 return this == obj || obj instanceof LazyValues && map.equals(((LazyValues)obj).map);
56 private static final class Iter implements Iterator<DataContainerChild> {
57 private final Iterator<Entry<PathArgument, Object>> iterator;
59 Iter(final Iterator<Entry<PathArgument, Object>> iterator) {
60 this.iterator = requireNonNull(iterator);
64 public boolean hasNext() {
65 return iterator.hasNext();
69 public DataContainerChild next() {
70 final Entry<PathArgument, Object> entry = iterator.next();
71 final Object value = entry.getValue();
72 return value instanceof DataContainerChild ? (DataContainerChild) value
73 : LazyLeafOperations.coerceLeaf(entry.getKey(), value);