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