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.parser.rfc7950.stmt;
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.model.api.meta.ModelStatement;
15 abstract class AbstractModelStatement<A> implements ModelStatement<A> {
18 * Utility method for squashing singleton lists into single objects. This is a CPU/mem trade-off, which we are
19 * usually willing to make: for the cost of an instanceof check we can save one object and re-create it when needed.
20 * The inverse operation is #unmaskSubstatements(Object)}.
22 * @param list list to mask
24 * @throws NullPointerException if list is null
26 protected static final @NonNull Object maskList(final ImmutableList<?> list) {
27 // Note: ImmutableList guarantees non-null content
28 return list.size() == 1 ? list.get(0) : list;
32 * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
34 * @param masked list to unmask
35 * @return Unmasked list
36 * @throws NullPointerException if any argument is null
37 * @throws ClassCastException if masked object does not match expected class
39 @SuppressWarnings("unchecked")
40 protected static final <T> @NonNull ImmutableList<T> unmaskList(final @NonNull Object masked,
41 final @NonNull Class<T> type) {
42 return masked instanceof ImmutableList ? (ImmutableList<T>) masked
43 // Yes, this is ugly code, which could use an explicit verify, that would just change the what sort
44 // of exception we throw. ClassCastException is as good as VerifyException.
45 : ImmutableList.of(type.cast(masked));
48 protected static final @NonNull Object maskSet(final ImmutableSet<?> set) {
49 return set.size() == 1 ? set.iterator().next() : set;
52 protected static final <T> @NonNull ImmutableSet<? extends T> unmaskSet(final @NonNull Object masked,
53 final @NonNull Class<T> type) {
54 return masked instanceof ImmutableSet ? (ImmutableSet<T>) masked
55 // Yes, this is ugly code, which could use an explicit verify, that would just change the what sort
56 // of exception we throw. ClassCastException is as good as VerifyException.
57 : ImmutableSet.of(type.cast(masked));