Do not pretty-print body class
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / LazyValues.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.nodes;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.AbstractCollection;
13 import java.util.Iterator;
14 import java.util.Map;
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;
18
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;
26
27     LazyValues(final Map<PathArgument, Object> map) {
28         this.map = requireNonNull(map);
29     }
30
31     @Override
32     public int size() {
33         return map.size();
34     }
35
36     @Override
37     public boolean isEmpty() {
38         return map.isEmpty();
39     }
40
41     @Override
42     public Iterator<DataContainerChild> iterator() {
43         return new Iter(map.entrySet().iterator());
44     }
45
46     @Override
47     public int hashCode() {
48         return map.hashCode();
49     }
50
51     @Override
52     public boolean equals(final Object obj) {
53         return this == obj || obj instanceof LazyValues && map.equals(((LazyValues)obj).map);
54     }
55
56     private static final class Iter implements Iterator<DataContainerChild> {
57         private final Iterator<Entry<PathArgument, Object>> iterator;
58
59         Iter(final Iterator<Entry<PathArgument, Object>> iterator) {
60             this.iterator = requireNonNull(iterator);
61         }
62
63         @Override
64         public boolean hasNext() {
65             return iterator.hasNext();
66         }
67
68         @Override
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);
74         }
75     }
76 }