    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.configuration2.sync;
    018
    019import java.util.concurrent.locks.ReadWriteLock;
    020import java.util.concurrent.locks.ReentrantReadWriteLock;
    021
    022/**
    023 * <p>
    024 * A special implementation of {@code Synchronizer} based on the JDK's {@code ReentrantReadWriteLock} class.
    025 * </p>
    026 * <p>
    027 * This class manages a {@code ReadWriteLock} object internally. The methods of the {@code Synchronizer} interface are
    028 * delegated to this lock. So this class behaves in the same way as documented for {@code ReentrantReadWriteLock}.
    029 * </p>
    030 * <p>
    031 * Using this {@code Synchronizer} implementation is appropriate to make configuration objects thread-safe. This means
    032 * that multiple threads can read configuration data in parallel; if one thread wants to update the configuration, this
    033 * happens with an exclusive lock.
    034 * </p>
    035 *
    036 * @since 2.0
    037 */
    038public class ReadWriteSynchronizer implements Synchronizer {
    039    /**
    040     * Returns a new default lock object which is used if no lock is passed to the constructor.
    041     *
    042     * @return the new default lock object
    043     */
    044    private static ReadWriteLock createDefaultLock() {
    045        return new ReentrantReadWriteLock();
    046    }
    047
    048    /** The lock object used by this Synchronizer. */
    049    private final ReadWriteLock lock;
    050
    051    /**
    052     * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with a lock object of type
    053     * {@code ReentrantReadWriteLock}.
    054     */
    055    public ReadWriteSynchronizer() {
    056        this(null);
    057    }
    058
    059    /**
    060     * Creates a new instance of {@code ReadWriteSynchronizer} and initializes it with the given lock object. This
    061     * constructor can be used to pass a lock object which has been configured externally. If the lock object is
    062     * <b>null</b>, a default lock object is created.
    063     *
    064     * @param l the lock object to be used (can be <b>null</b>)
    065     */
    066    public ReadWriteSynchronizer(final ReadWriteLock l) {
    067        lock = l != null ? l : createDefaultLock();
    068    }
    069
    070    @Override
    071    public void beginRead() {
    072        lock.readLock().lock();
    073    }
    074
    075    @Override
    076    public void beginWrite() {
    077        lock.writeLock().lock();
    078    }
    079
    080    @Override
    081    public void endRead() {
    082        lock.readLock().unlock();
    083    }
    084
    085    @Override
    086    public void endWrite() {
    087        lock.writeLock().unlock();
    088    }
    089}