Make Delegator.getDelegate() non-null
[yangtools.git] / common / concepts / src / main / java / org / opendaylight / yangtools / concepts / AbstractDelegator.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.concepts;
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 com.google.common.base.MoreObjects.ToStringHelper;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17
18 /**
19  * Simple base class for classes which wish to implement {@link Delegator} interface and are not otherwise constrained
20  * in their class hierarchy.
21  *
22  * @param <T> Type of delegate
23  */
24 @Beta
25 @NonNullByDefault
26 public abstract class AbstractDelegator<T> implements Delegator<T> {
27     private final @NonNull T delegate;
28
29     protected AbstractDelegator(final T delegate) {
30         this.delegate = requireNonNull(delegate);
31     }
32
33     @Override
34     public final T getDelegate() {
35         return delegate;
36     }
37
38     @Override
39     public final String toString() {
40         return addToString(MoreObjects.toStringHelper(this).omitNullValues()).toString();
41     }
42
43     protected ToStringHelper addToString(final ToStringHelper helper) {
44         return helper.add("delegate", delegate);
45     }
46 }