View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.ehcache.transaction;
17  
18  import java.util.concurrent.atomic.AtomicInteger;
19  
20  /***
21   * A transaction ID implementation with uniqueness across a single JVM
22   *
23   * @author Ludovic Orban
24   */
25  public final class TransactionIDImpl implements TransactionID {
26  
27      private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
28  
29      private final int id;
30      private volatile boolean commit;
31  
32      /***
33       * Create a new TransactionIDImpl instance
34       */
35      TransactionIDImpl() {
36          this.id = ID_GENERATOR.getAndIncrement();
37      }
38  
39      /***
40       * {@inheritDoc}
41       */
42      public boolean isDecisionCommit() {
43          return commit;
44      }
45  
46      /***
47       * {@inheritDoc}
48       */
49      public void markForCommit() {
50          this.commit = true;
51      }
52  
53  
54      /***
55       * {@inheritDoc}
56       */
57      @Override
58      public final boolean equals(Object obj) {
59          if (obj instanceof TransactionIDImpl) {
60              TransactionIDImpl otherId = (TransactionIDImpl) obj;
61              return id == otherId.id;
62          }
63          return false;
64      }
65  
66      /***
67       * {@inheritDoc}
68       */
69      @Override
70      public final int hashCode() {
71          return id;
72      }
73  
74      /***
75       * {@inheritDoc}
76       */
77      @Override
78      public String toString() {
79          return "" + id + (commit ? " (marked for commit)" : "");
80      }
81  }