2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.common.impl.util;
10 import java.util.concurrent.locks.Lock;
11 import java.util.concurrent.locks.ReentrantReadWriteLock;
12 import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
13 import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
15 import org.opendaylight.yangtools.concepts.Delegator;
17 import com.google.common.base.Preconditions;
19 public class AbstractLockableDelegator<T> implements Delegator<T> {
21 private final ReentrantReadWriteLock delegateLock = new ReentrantReadWriteLock();
22 private final ReadLock delegateReadLock = delegateLock.readLock();
23 private final WriteLock delegateWriteLock = delegateLock.writeLock();
26 protected Lock getDelegateReadLock() {
27 return delegateReadLock;
32 public AbstractLockableDelegator() {
36 public AbstractLockableDelegator(T initialDelegate) {
37 delegate = initialDelegate;
41 public T getDelegate() {
43 delegateReadLock.lock();
46 delegateReadLock.unlock();
50 public T retrieveDelegate() {
52 delegateReadLock.lock();
53 Preconditions.checkState(delegate != null,"Delegate is null");
56 delegateReadLock.unlock();
65 public final T changeDelegate(T newDelegate) {
67 delegateWriteLock.lock();
68 T oldDelegate = delegate;
69 delegate = newDelegate;
70 onDelegateChanged(oldDelegate, newDelegate);
73 delegateWriteLock.unlock();
78 protected void onDelegateChanged(T oldDelegate, T newDelegate) {
79 // NOOP in abstract calss;