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     */
017    package org.apache.commons.configuration;
018    
019    /**
020     * <p>
021     * A simple class acting as lock.
022     * </p>
023     * <p>
024     * Instances of this class are used by some configuration classes to synchronize
025     * themselves.
026     * </p>
027     *
028     * @author <a
029     *         href="http://commons.apache.org/configuration/team-list.html">Commons
030     *         Configuration team</a>
031     * @since 1.7
032     * @version $Id: Lock.java 1162387 2011-08-27 16:05:20Z oheger $
033     */
034    public class Lock
035    {
036        /** A string used internally to synchronize counter updates. */
037        private static String counterLock = "Lock";
038    
039        /** A counter for generating unique instance IDs. */
040        private static int counter;
041    
042        /** The name of this lock. */
043        private final String name;
044    
045        /** The unique ID of this lock instance. */
046        private final int instanceId;
047    
048        /**
049         * Creates a new instance of {@code Lock} with the specified name.
050         *
051         * @param name the name of this lock
052         */
053        public Lock(String name)
054        {
055            this.name = name;
056            synchronized (counterLock)
057            {
058                instanceId = ++counter;
059            }
060        }
061    
062        /**
063         * Returns the name of this lock.
064         *
065         * @return the name of this lock
066         */
067        public String getName()
068        {
069            return name;
070        }
071    
072        /**
073         * Returns a string representation of this object. This implementation
074         * returns a string which contains the lock name and the instance ID.
075         *
076         * @return a string for this object
077         */
078        public String toString()
079        {
080            return "Lock: " + name + " id = " + instanceId + ": "
081                    + super.toString();
082        }
083    }