Migrate yang-common to use JDT annotations
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ObjectRegistry.java
1 /*
2  * Copyright (c) 2018 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.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.function.Consumer;
19 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
23 import org.opendaylight.yangtools.concepts.ObjectRegistration;
24
25 @Beta
26 @NonNullByDefault
27 public final class ObjectRegistry<T> {
28     private final Set<ObjectRegistration<? extends T>> objects;
29     private final Set<ObjectRegistration<? extends T>> unmodifiableView;
30     private final String name;
31
32     private ObjectRegistry(final String name, Set<ObjectRegistration<? extends T>> objects) {
33         this.name = requireNonNull(name);
34         this.objects = requireNonNull(objects);
35         this.unmodifiableView = Collections.unmodifiableSet(objects);
36     }
37
38     public static <T> ObjectRegistry<T> createConcurrent(final String name) {
39         return new ObjectRegistry<>(name, ConcurrentHashMap.newKeySet());
40     }
41
42     public static <T> ObjectRegistry<T> createSimple(final String name) {
43         return new ObjectRegistry<>(name, new HashSet<>(1));
44     }
45
46     public boolean isEmpty() {
47         return objects.isEmpty();
48     }
49
50     public Stream<? extends T> streamObjects() {
51         return streamRegistrations().map(ObjectRegistration::getInstance);
52     }
53
54     public Set<ObjectRegistration<? extends T>> getRegistrations() {
55         return unmodifiableView;
56     }
57
58     public Stream<ObjectRegistration<? extends T>> streamRegistrations() {
59         return objects.stream();
60     }
61
62     public <O extends T> ObjectRegistration<O> register(final O object) {
63         final ObjectRegistration<O> ret = new Reg<>(object, objects::remove);
64         objects.add(ret);
65         return ret;
66     }
67
68     @Override
69     public String toString() {
70         return MoreObjects.toStringHelper(this).add("name", name).add("objects", objects.size()).toString();
71     }
72
73     private static final class Reg<T> extends AbstractObjectRegistration<T> {
74         private @Nullable @javax.annotation.Nullable Consumer<ObjectRegistration<? super T>> removeCall;
75
76         Reg(final T instance, final Consumer<ObjectRegistration<? super T>> removeCall) {
77             super(instance);
78             this.removeCall = requireNonNull(removeCall);
79         }
80
81         @Override
82         protected void removeRegistration() {
83             removeCall.accept(this);
84             // Do not retail reference to that state
85             removeCall = null;
86         }
87     }
88 }