Skip to content

Method: getState()

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.sesame;
16:
17: import static cz.cvut.kbss.ontodriver.sesame.TransactionState.ABORTED;
18: import static cz.cvut.kbss.ontodriver.sesame.TransactionState.ACTIVE;
19: import static cz.cvut.kbss.ontodriver.sesame.TransactionState.COMMITTED;
20: import static cz.cvut.kbss.ontodriver.sesame.TransactionState.FAILED;
21: import static cz.cvut.kbss.ontodriver.sesame.TransactionState.PARTIALLY_COMMITTED;
22:
23: public final class Transaction {
24:
25: private TransactionState state;
26:
27: public void begin() {
28: if (state != null && state != ABORTED && state != COMMITTED) {
29: throw new IllegalStateException("Cannot begin transaction. Current state is " + state);
30: }
31: this.state = ACTIVE;
32: }
33:
34: public void commit() {
35: if (state != ACTIVE) {
36: throw new IllegalStateException("Cannot commit transaction. Current state is " + state);
37: }
38: this.state = PARTIALLY_COMMITTED;
39: }
40:
41: public void afterCommit() {
42: if (state != PARTIALLY_COMMITTED) {
43: throw new IllegalStateException("Cannot finish commit. Current state is " + state);
44: }
45: this.state = COMMITTED;
46: }
47:
48: public void rollback() {
49: if (state != ACTIVE && state != PARTIALLY_COMMITTED && state != FAILED) {
50: throw new IllegalStateException("Cannot rollback transaction. Current state is "
51: + state);
52: }
53: this.state = FAILED;
54: }
55:
56: public void afterRollback() {
57: if (state != FAILED) {
58: throw new IllegalStateException("Cannot finish rollback. Current state is " + state);
59: }
60: this.state = ABORTED;
61: }
62:
63: public boolean isActive() {
64: return state == ACTIVE;
65: }
66:
67: public TransactionState getState() {
68: return state;
69: }
70:
71: @Override
72: public int hashCode() {
73: final int prime = 31;
74: int result = 1;
75: result = prime * result + ((state == null) ? 0 : state.hashCode());
76: return result;
77: }
78:
79: @Override
80: public boolean equals(Object obj) {
81: if (this == obj)
82: return true;
83: if (obj == null)
84: return false;
85: if (getClass() != obj.getClass())
86: return false;
87: Transaction other = (Transaction) obj;
88: if (state != other.state)
89: return false;
90: return true;
91: }
92:
93: @Override
94: public String toString() {
95: return state != null ? state.toString() : "null";
96: }
97: }