BUG-865: deprecate concepts
[yangtools.git] / common / concepts / src / main / java / org / opendaylight / yangtools / concepts / CompositeObjectRegistration.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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 java.util.Collection;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.Set;
14
15 /**
16  * @deprecated This class does not have good semantics with regard to constituent registrations' lifecycle and should
17  *             not be used.
18  */
19 @Deprecated
20 public final class CompositeObjectRegistration<T> extends AbstractObjectRegistration<T> {
21
22     private final Set<Registration> registrations;
23
24     public CompositeObjectRegistration(final T instance, final Collection<? extends Registration> registrations) {
25         super(instance);
26         if (registrations == null) {
27             throw new IllegalArgumentException();
28         }
29         this.registrations = Collections.unmodifiableSet(new HashSet<>(registrations));
30     }
31
32     @Override
33     protected void removeRegistration() {
34         for (Registration registration : registrations) {
35             try {
36                 registration.close();
37             } catch (Exception e) {
38                 e.printStackTrace();
39             }
40         }
41     }
42
43     public static <T> CompositeObjectRegistrationBuilder<T> builderFor(final T instance) {
44         return new CompositeObjectRegistrationBuilder<>(instance);
45     }
46
47     public static final class CompositeObjectRegistrationBuilder<T> implements Builder<CompositeObjectRegistration<T>> {
48
49         private final T instance;
50         private final Set<Registration> registrations;
51
52         public CompositeObjectRegistrationBuilder(final T instance) {
53             this.instance = instance;
54             registrations = new HashSet<>();
55         }
56
57         public CompositeObjectRegistrationBuilder<T> add(final ObjectRegistration<? super T> registration) {
58             if (!registration.getInstance().equals(instance)) {
59                 throw new IllegalArgumentException("Instance must be same.");
60             }
61             registrations.add(registration);
62             return this;
63         }
64
65         public CompositeObjectRegistrationBuilder<T> remove(final ObjectRegistration<? super T> registration) {
66             if (!registration.getInstance().equals(instance)) {
67                 throw new IllegalArgumentException("Instance must be same.");
68             }
69             registrations.remove(registration);
70             return this;
71         }
72
73         @Override
74         public CompositeObjectRegistration<T> build() {
75             return new CompositeObjectRegistration<>(instance, registrations);
76         }
77     }
78 }