Skip to content

Commit 79701ed

Browse files
committed
General purpose transaction id helper
1 parent 91bc6e9 commit 79701ed

File tree

1 file changed

+118
-0
lines changed
  • modules/txn/src/main/java/org/jpos/transaction

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* jPOS Project [http://jpos.org]
3+
* Copyright (C) 2000-2017 jPOS Software SRL
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program 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
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
package org.jpos.transaction;
20+
21+
import org.joda.time.DateTime;
22+
import org.joda.time.DateTimeZone;
23+
24+
import java.util.Objects;
25+
import java.util.regex.Matcher;
26+
import java.util.regex.Pattern;
27+
28+
public class TxnId {
29+
private long id;
30+
private static final long YMUL = 1000000000000000L;
31+
private static final long DMUL = 1000000000000L;
32+
private static final long SMUL = 10000000L;
33+
private static final long NMUL = 100000L;
34+
private Pattern pattern = Pattern.compile("^([\\d]{4})-([\\d]{3})-([\\d]{5})-([\\d]{2})-([\\d]{5})$");
35+
36+
/**
37+
*
38+
* @param dt Transaction's TIMESTAMP DateTime
39+
* @param node node id
40+
* @param transactionId TransactionManager's ID
41+
*/
42+
public TxnId(DateTime dt, int node, long transactionId) {
43+
if (dt.getZone() != DateTimeZone.UTC)
44+
dt = dt.toDateTime(DateTimeZone.UTC);
45+
46+
init (dt.getYear(), dt.getDayOfYear(), dt.getSecondOfDay(), node, transactionId);
47+
}
48+
49+
/**
50+
* @param idString TxnId in YYYY-DDD-SSS-NN-TTTTT format
51+
*
52+
* <ul>
53+
* <li><code>YYYY</code> year</li>
54+
* <li><code>DDD</code> day of year</li>
55+
* <li><code>SSS</code> second of day</li>
56+
* <li><code>NN</code> unique node number (00 to 99)</li>
57+
* <li><code>TTTTT</code> last 5 digits of transaction manager's transaction id</li>
58+
* </ul>
59+
*/
60+
public TxnId(String idString) {
61+
Matcher matcher = pattern.matcher(idString);
62+
if (!matcher.matches() && matcher.groupCount() != 5)
63+
throw new IllegalArgumentException("Invalid idString '" + idString + "'");
64+
init(
65+
Integer.parseInt(matcher.group(1)),
66+
Integer.parseInt(matcher.group(2)),
67+
Integer.parseInt(matcher.group(3)),
68+
Integer.parseInt(matcher.group(4)),
69+
Integer.parseInt(matcher.group(5))
70+
);
71+
}
72+
73+
public TxnId(long l) {
74+
this.id = l;
75+
}
76+
77+
public long id() {
78+
return id;
79+
}
80+
81+
private void init (int year, int dayOfYear, int secondOfDay, int node, long transactionId) {
82+
id = year * YMUL
83+
+ dayOfYear * DMUL
84+
+ secondOfDay * SMUL
85+
+ (node % 100) * NMUL
86+
+ transactionId % 100000;
87+
}
88+
89+
@Override
90+
public String toString() {
91+
long l = id;
92+
int yy = (int) (id / YMUL);
93+
l -= yy*YMUL;
94+
95+
int dd = (int) (l / DMUL);
96+
l -= dd * DMUL;
97+
98+
int ss = (int) (l / SMUL);
99+
l -= ss * SMUL;
100+
101+
int node = (int) (l / NMUL);
102+
l -= node * NMUL;
103+
return String.format("%04d-%03d-%05d-%02d-%05d", yy, dd, ss, node, l);
104+
}
105+
106+
@Override
107+
public boolean equals(Object o) {
108+
if (this == o) return true;
109+
if (o == null || getClass() != o.getClass()) return false;
110+
TxnId tranLogId = (TxnId) o;
111+
return id == tranLogId.id;
112+
}
113+
114+
@Override
115+
public int hashCode() {
116+
return Objects.hash(id);
117+
}
118+
}

0 commit comments

Comments
 (0)