View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration;
18  
19  /**
20   * <p>
21   * A simple class acting as lock.
22   * </p>
23   * <p>
24   * Instances of this class are used by some configuration classes to synchronize
25   * themselves.
26   * </p>
27   *
28   * @author <a
29   *         href="http://commons.apache.org/configuration/team-list.html">Commons
30   *         Configuration team</a>
31   * @since 1.7
32   * @version $Id: Lock.java 1301995 2012-03-17 20:24:16Z sebb $
33   */
34  public class Lock
35  {
36      /** A string used internally to synchronize counter updates. */
37      private static String counterLock = "Lock";
38  
39      /** A counter for generating unique instance IDs. */
40      private static int counter;
41  
42      /** The name of this lock. */
43      private final String name;
44  
45      /** The unique ID of this lock instance. */
46      private final int instanceId;
47  
48      /**
49       * Creates a new instance of {@code Lock} with the specified name.
50       *
51       * @param name the name of this lock
52       */
53      public Lock(String name)
54      {
55          this.name = name;
56          synchronized (counterLock)
57          {
58              instanceId = ++counter;
59          }
60      }
61  
62      /**
63       * Returns the name of this lock.
64       *
65       * @return the name of this lock
66       */
67      public String getName()
68      {
69          return name;
70      }
71  
72      /**
73       * Returns a string representation of this object. This implementation
74       * returns a string which contains the lock name and the instance ID.
75       *
76       * @return a string for this object
77       */
78      @Override
79      public String toString()
80      {
81          return "Lock: " + name + " id = " + instanceId + ": "
82                  + super.toString();
83      }
84  }