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