Define a feature-parent
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / UniqueValues.java
1 /*
2  * Copyright (c) 2020 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.tree.impl;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Iterator;
16 import java.util.NoSuchElementException;
17 import java.util.stream.Collector;
18 import org.opendaylight.yangtools.concepts.Immutable;
19
20 /**
21  * A vector of values associated with a unique constraint. This is almost an {@link ArrayList}, except it is
22  * unmodifiable. The only way to construct this instance is through {@link #COLLECTOR}. This should be used only in case
23  * of more than one value.
24  */
25 // Design note: this could have been a List, because the order matters, but it does not matter much, we are expected
26 // to be compared against ourselves -- and we treat byte[] specially, breaking reflexivity of equality. We could also go
27 // for Collection, but at this point nobody cares about size().
28 final class UniqueValues implements Immutable, Iterable<Object> {
29     static final Collector<Object, ?, UniqueValues> COLLECTOR = Collector.of(ArrayList::new, ArrayList::add,
30         (left, right) -> {
31             left.addAll(right);
32             return left;
33         },
34         list -> new UniqueValues(list.toArray()));
35
36     private final Object[] objects;
37     private final int hashCode;
38
39     private UniqueValues(final Object[] objects) {
40         verify(objects.length != 0);
41         this.objects = objects;
42         hashCode = Arrays.deepHashCode(objects);
43     }
44
45     @Override
46     public Iterator<Object> iterator() {
47         return new Itr(objects);
48     }
49
50     @Override
51     public int hashCode() {
52         return hashCode;
53     }
54
55     @Override
56     public boolean equals(final Object obj) {
57         return this == obj || obj instanceof UniqueValues other && Arrays.deepEquals(objects, other.objects);
58     }
59
60     @Override
61     public String toString() {
62         return toString(objects);
63     }
64
65     private static String toString(final Object[] objects) {
66         final var sb = new StringBuilder();
67         sb.append('[').append(BinaryValue.wrapToString(objects[0]));
68         for (int i = 1; i < objects.length; ++i) {
69             sb.append(", ").append(BinaryValue.wrapToString(objects[i]));
70         }
71         return sb.append(']').toString();
72     }
73
74     private static final class Itr implements Iterator<Object> {
75         private final Object[] objects;
76
77         private int offset = 0;
78
79         Itr(final Object[] objects) {
80             this.objects = requireNonNull(objects);
81         }
82
83         @Override
84         public boolean hasNext() {
85             return offset < objects.length;
86         }
87
88         @Override
89         public Object next() {
90             int local = offset;
91             if (local >= objects.length) {
92                 throw new NoSuchElementException();
93             }
94             offset = local + 1;
95             return objects[local];
96         }
97     }
98 }