Seal ItemOrder and MutationBehaviour
[yangtools.git] / common / concepts / src / main / java / org / opendaylight / yangtools / concepts / ItemOrder.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.concepts;
9
10 import com.google.common.annotations.Beta;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Set;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16
17 /**
18  * Marker interface for specifying ordering of items. There are two orderings, {@link Ordered} and {@link Unordered},
19  * and they are mutually exclusive. Item ordering is considered semantically important, such as it impacts
20  * {@link Object#equals(Object)} contract. This can be thought of as the missing interface distinction between
21  * {@link Collection}, {@link List} and {@link Set}.
22  *
23  * @param <T> Item order type
24  */
25 @Beta
26 public sealed interface ItemOrder<T extends ItemOrder<T>> {
27     /**
28      * Items are ordered and their order is significant. A {@link List} is an example of a collection which conforms to
29      * this contract.
30      */
31     non-sealed interface Ordered extends ItemOrder<Ordered> {
32         @Override
33         default Class<Ordered> itemOrder() {
34             return Ordered.class;
35         }
36
37         /**
38          * {@inheritDoc}
39          *
40          * <p>
41          * Hash code contract of {@link Ordered} objects <b>should</b> be sensitive to item order. In general similar to
42          * {@link List#hashCode()} (in the {@code must} reading of sensitivity. {@code need not} reading of sensitivity
43          * could also be implemented as {@code Map.hashCode()} in case of a map-like container.
44          */
45         // FIXME: 8.0.0: tighten 'should' to 'must'?
46         @Override
47         int hashCode();
48
49         /**
50          * {@inheritDoc}
51          *
52          * <p>
53          * Equality contract of {@link Ordered} objects <b>must</b> be sensitive to item order, similar to
54          * {@link List#equals(Object)}.
55          */
56         @Override
57         boolean equals(Object obj);
58     }
59
60     /**
61      * Items are unordered and their order is insignificant. A {@link Set} is an example of a collection which conforms
62      * to this contract.
63      */
64     non-sealed interface Unordered extends ItemOrder<Unordered> {
65         @Override
66         default Class<Unordered> itemOrder() {
67             return Unordered.class;
68         }
69
70         /**
71          * {@inheritDoc}
72          *
73          * <p>
74          * Hash code contract of {@link Unordered} objects <b>must</b> be insensitive to item order, similar to
75          * {@link Set#hashCode()}.
76          *
77          * <p>
78          * This contract is also exposed through {@link #itemOrder()}.
79          */
80         @Override
81         int hashCode();
82
83         /**
84          * {@inheritDoc}
85          *
86          * <p>
87          * Equality contract of {@link Unordered} objects <b>must</b> be insensitive to item order, similar to
88          * {@link Set#equals(Object)}.
89          *
90          * <p>
91          * This contract is also exposed through {@link #itemOrder()}.
92          */
93         @Override
94         boolean equals(Object obj);
95     }
96
97     /**
98      * Return the item order class of this object. The class' equality contracts apply to this object's equality
99      * contract.
100      *
101      * @return Item order class.
102      */
103     @NonNull Class<T> itemOrder();
104
105     /**
106      * {@link ItemOrder} has impact on {@link #hashCode()}.
107      */
108     @Override
109     int hashCode();
110
111     /**
112      * {@link ItemOrder} has impact on {@link #equals(Object)}.
113      */
114     @Override
115     boolean equals(@Nullable Object obj);
116 }