Deprecate simple DataTreeFactory.create()
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Empty.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies 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.common;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.io.Serializable;
13 import java.util.concurrent.CompletableFuture;
14 import java.util.concurrent.CompletionStage;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.Immutable;
18
19 /**
20  * Dedicated type for YANG's {@code type empty} value.
21  */
22 @NonNullByDefault
23 public final class Empty implements Immutable, Serializable {
24     @java.io.Serial
25     private static final long serialVersionUID = 1L;
26     private static final Empty VALUE = new Empty();
27     private static final CompletionStage<Empty> COMPLETED_FUTURE = CompletableFuture.completedFuture(VALUE);
28     private static final ListenableFuture<Empty> IMMEDIATE_FUTURE = Futures.immediateFuture(VALUE);
29
30     private Empty() {
31         // Hidden on purpose
32     }
33
34     /**
35      * Return the singleton {@link Empty} value.
36      *
37      * @return Empty value.
38      */
39     public static Empty value()  {
40         return VALUE;
41     }
42
43     /**
44      * Return a {@link CompletionStage} completed with {@link #value()}.
45      *
46      * @return A completed CompletionStage
47      */
48     public static CompletionStage<Empty> completedFuture() {
49         return COMPLETED_FUTURE;
50     }
51
52     /**
53      * Return a {@link ListenableFuture} completed with {@link #value()}.
54      *
55      * @return A completed ListenableFuture
56      */
57     public static ListenableFuture<Empty> immediateFuture() {
58         return IMMEDIATE_FUTURE;
59     }
60
61     @Override
62     public int hashCode() {
63         return 1337;
64     }
65
66     @Override
67     public boolean equals(final @Nullable Object obj) {
68         // Note: this is nominally a singleton, but due to it being Serializable multiple instances might be created
69         //       via hand-crafted serialization streams. We therefore do not rely on '==' but on 'instanceof' check.
70         return obj instanceof Empty;
71     }
72
73     @Override
74     public String toString() {
75         return "empty";
76     }
77
78     @java.io.Serial
79     @SuppressWarnings("static-method")
80     private Object readResolve() {
81         return VALUE;
82     }
83 }