Seal ModelStatement
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / 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.api.meta;
9
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
17 /**
18  * Abstract base class for {@link ModelStatement} implementations. It mostly provides static methods for efficiently
19  * storing lists.
20  *
21  * @param <A> Argument type ({@link Empty} if statement does not have argument.)
22  */
23 abstract sealed class AbstractModelStatement<A> implements ModelStatement<A>
24         permits AbstractDeclaredStatement, AbstractEffectiveStatement {
25     @Override
26     public final int hashCode() {
27         return System.identityHashCode(this);
28     }
29
30     @Override
31     public final boolean equals(final Object obj) {
32         return this == obj;
33     }
34
35     @Override
36     public final String toString() {
37         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
38     }
39
40     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
41         return helper.add("argument", argument());
42     }
43
44     /**
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)}.
48      *
49      * @param list list to mask
50      * @return Masked list
51      * @throws NullPointerException if list is null
52      */
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;
56     }
57
58     /**
59      * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
60      *
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
65      */
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));
73     }
74
75     protected static final @NonNull Object maskSet(final ImmutableSet<?> set) {
76         return set.size() == 1 ? set.iterator().next() : set;
77     }
78
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));
85     }
86 }