2 * Copyright (c) 2020 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.tree.impl;
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
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;
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.
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,
34 list -> new UniqueValues(list.toArray()));
36 private final Object[] objects;
37 private final int hashCode;
39 private UniqueValues(final Object[] objects) {
40 verify(objects.length != 0);
41 this.objects = objects;
42 hashCode = Arrays.deepHashCode(objects);
46 public Iterator<Object> iterator() {
47 return new Itr(objects);
51 public int hashCode() {
56 public boolean equals(final Object obj) {
57 return this == obj || obj instanceof UniqueValues other && Arrays.deepEquals(objects, other.objects);
61 public String toString() {
62 return toString(objects);
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]));
71 return sb.append(']').toString();
74 private static final class Itr implements Iterator<Object> {
75 private final Object[] objects;
77 private int offset = 0;
79 Itr(final Object[] objects) {
80 this.objects = requireNonNull(objects);
84 public boolean hasNext() {
85 return offset < objects.length;
89 public Object next() {
91 if (local >= objects.length) {
92 throw new NoSuchElementException();
95 return objects[local];