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.model.api.meta;
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.Empty;
18 * Abstract base class for {@link ModelStatement} implementations. It mostly provides static methods for efficiently
21 * @param <A> Argument type ({@link Empty} if statement does not have argument.)
23 abstract sealed class AbstractModelStatement<A> implements ModelStatement<A>
24 permits AbstractDeclaredStatement, AbstractEffectiveStatement {
26 public final int hashCode() {
27 return System.identityHashCode(this);
31 public final boolean equals(final Object obj) {
36 public final String toString() {
37 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
40 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
41 return helper.add("argument", argument());
45 * Utility method for squashing singleton lists into single objects. This is a CPU/mem trade-off, which we are
46 * usually willing to make: for the cost of an instanceof check we can save one object and re-create it when needed.
47 * The inverse operation is #unmaskSubstatements(Object)}.
49 * @param list list to mask
51 * @throws NullPointerException if list is null
53 protected static final @NonNull Object maskList(final ImmutableList<?> list) {
54 // Note: ImmutableList guarantees non-null content
55 return list.size() == 1 ? list.get(0) : list;
59 * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
61 * @param masked list to unmask
62 * @return Unmasked list
63 * @throws NullPointerException if any argument is null
64 * @throws ClassCastException if masked object does not match expected class
66 @SuppressWarnings("unchecked")
67 protected static final <T> @NonNull ImmutableList<T> unmaskList(final @NonNull Object masked,
68 final @NonNull Class<T> type) {
69 return masked instanceof ImmutableList ? (ImmutableList<T>) masked
70 // Yes, this is ugly code, which could use an explicit verify, that would just change the what sort
71 // of exception we throw. ClassCastException is as good as VerifyException.
72 : ImmutableList.of(type.cast(masked));
75 protected static final @NonNull Object maskSet(final ImmutableSet<?> set) {
76 return set.size() == 1 ? set.iterator().next() : set;
79 protected static final <T> @NonNull ImmutableSet<T> unmaskSet(final @NonNull Object masked,
80 final @NonNull Class<T> type) {
81 return masked instanceof ImmutableSet ? (ImmutableSet<T>) masked
82 // Yes, this is ugly code, which could use an explicit verify, that would just change the what sort
83 // of exception we throw. ClassCastException is as good as VerifyException.
84 : ImmutableSet.of(type.cast(masked));