Skip to content

Method: toString()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.util;
19:
20:
21: import static cz.cvut.kbss.ontodriver.util.TransactionState.*;
22:
23: public final class Transaction {
24:
25: private TransactionState state;
26:
27: /**
28: * Begins a transaction.
29: */
30: public void begin() {
31: if (state != null && state != ABORTED && state != COMMITTED) {
32: throw new IllegalStateException("Cannot begin transaction. Current state is " + state);
33: }
34: this.state = ACTIVE;
35: }
36:
37: /**
38: * Marks the beginning of transaction commit.
39: *
40: * @see #afterCommit()
41: */
42: public void commit() {
43: verifyActive();
44: this.state = PARTIALLY_COMMITTED;
45: }
46:
47: /**
48: * Marks successful completion of transaction commit.
49: *
50: * @see #commit()
51: */
52: public void afterCommit() {
53: if (state != PARTIALLY_COMMITTED) {
54: throw new IllegalStateException("Cannot finish commit. Current state is " + state);
55: }
56: this.state = COMMITTED;
57: }
58:
59: /**
60: * Commences transaction rollback.
61: *
62: * @see #afterRollback()
63: */
64: public void rollback() {
65: if (state != ACTIVE && state != PARTIALLY_COMMITTED && state != FAILED) {
66: throw new IllegalStateException("Cannot rollback transaction. Current state is "
67: + state);
68: }
69: this.state = FAILED;
70: }
71:
72: /**
73: * Marks successful finish of transaction rollback.
74: *
75: * @see #rollback()
76: */
77: public void afterRollback() {
78: if (state != FAILED) {
79: throw new IllegalStateException("Cannot finish rollback. Current state is " + state);
80: }
81: this.state = ABORTED;
82: }
83:
84: public boolean isActive() {
85: return state == ACTIVE;
86: }
87:
88: public TransactionState getState() {
89: return state;
90: }
91:
92: /**
93: * Verifies that the transaction is active.
94: *
95: * @throws IllegalStateException When transaction is not active
96: */
97: public void verifyActive() {
98: if (!isActive()) {
99: throw new IllegalStateException("Transaction is not active. Current state is " + state);
100: }
101: }
102:
103: @Override
104: public int hashCode() {
105: final int prime = 31;
106: int result = 1;
107: result = prime * result + ((state == null) ? 0 : state.hashCode());
108: return result;
109: }
110:
111: @Override
112: public boolean equals(Object obj) {
113: if (this == obj)
114: return true;
115: if (obj == null)
116: return false;
117: if (getClass() != obj.getClass())
118: return false;
119: Transaction other = (Transaction) obj;
120: return state == other.state;
121: }
122:
123: @Override
124: public String toString() {
125:• return state != null ? state.toString() : "null";
126: }
127: }