Populate data/ hierarchy
[yangtools.git] / model / yang-model-spi / src / main / java / org / opendaylight / yangtools / yang / model / spi / meta / AbstractModelStatement.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.model.spi.meta;
9
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.common.Empty;
14 import org.opendaylight.yangtools.yang.model.api.meta.ModelStatement;
15
16 /**
17  * Abstract base class for {@link ModelStatement} implementations. It mostly provides static methods for efficiently
18  * storing lists.
19  *
20  * @param <A> Argument type ({@link Empty} if statement does not have argument.)
21  */
22 abstract class AbstractModelStatement<A> implements ModelStatement<A> {
23     /**
24      * Utility method for squashing singleton lists into single objects. This is a CPU/mem trade-off, which we are
25      * usually willing to make: for the cost of an instanceof check we can save one object and re-create it when needed.
26      * The inverse operation is #unmaskSubstatements(Object)}.
27      *
28      * @param list list to mask
29      * @return Masked list
30      * @throws NullPointerException if list is null
31      */
32     protected static final @NonNull Object maskList(final ImmutableList<?> list) {
33         // Note: ImmutableList guarantees non-null content
34         return list.size() == 1 ? list.get(0) : list;
35     }
36
37     /**
38      * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
39      *
40      * @param masked list to unmask
41      * @return Unmasked list
42      * @throws NullPointerException if any argument is null
43      * @throws ClassCastException if masked object does not match expected class
44      */
45     @SuppressWarnings("unchecked")
46     protected static final <T> @NonNull ImmutableList<T> unmaskList(final @NonNull Object masked,
47             final @NonNull Class<T> type) {
48         return masked instanceof ImmutableList ? (ImmutableList<T>) masked
49             // Yes, this is ugly code, which could use an explicit verify, that would just change the what sort
50             // of exception we throw. ClassCastException is as good as VerifyException.
51             : ImmutableList.of(type.cast(masked));
52     }
53
54     protected static final @NonNull Object maskSet(final ImmutableSet<?> set) {
55         return set.size() == 1 ? set.iterator().next() : set;
56     }
57
58     protected static final <T> @NonNull ImmutableSet<T> unmaskSet(final @NonNull Object masked,
59             final @NonNull Class<T> type) {
60         return masked instanceof ImmutableSet ? (ImmutableSet<T>) masked
61             // Yes, this is ugly code, which could use an explicit verify, that would just change the what sort
62             // of exception we throw. ClassCastException is as good as VerifyException.
63             : ImmutableSet.of(type.cast(masked));
64     }
65 }