diff --git a/modules/txn/src/main/java/org/jpos/transaction/TxnId.java b/modules/txn/src/main/java/org/jpos/transaction/TxnId.java deleted file mode 100644 index b88184d21f..0000000000 --- a/modules/txn/src/main/java/org/jpos/transaction/TxnId.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * jPOS Project [http://jpos.org] - * Copyright (C) 2000-2021 jPOS Software SRL - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package org.jpos.transaction; - -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; - -import java.io.File; -import java.time.Instant; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class TxnId { - private long id; - private static final long YMUL = 10000000000000000L; - private static final long DMUL = 10000000000000L; - private static final long SMUL = 100000000L; - private static final long NMUL = 100000L; - private static final long MAX_VALUE = Long.parseLong("zzzzzzzzzzzz", 36); - private static Pattern pattern = Pattern.compile("^([\\d]{3})-([\\d]{3})-([\\d]{5})-([\\d]{3})-([\\d]{5})$"); - private static ZoneId UTC = ZoneId.of("UTC"); - - private TxnId() { - super(); - } - - private TxnId(long l) { - this.id = l; - } - - public long id() { - return id; - } - - private TxnId init (int year, int dayOfYear, int secondOfDay, int node, long transactionId) { - id = year * YMUL - + dayOfYear * DMUL - + secondOfDay * SMUL - + (node % 1000) * NMUL - + transactionId % 100000; - return this; - } - /** - * Returns a file suitable to store contents of this transaction. - * File format is yyyy/mm/dd/hh-mm-ss-node-id - * @return file in said format - */ - public File toFile () { - long l = id; - int yy = (int) (id / YMUL); l -= yy*YMUL; - int dd = (int) (l / DMUL); l -= dd*DMUL; - int sod = (int) (l / SMUL); l -= sod*SMUL; - int node = (int) (l / NMUL); l -= node * NMUL; - int hh = sod/3600; - int mm = (sod-3600*hh) / 60; - int ss = sod % 60; - - DateTime dt = new DateTime() - .withYear(2000+yy) - .withDayOfYear(dd) - .withHourOfDay(hh) - .withMinuteOfHour(mm) - .withSecondOfMinute(ss); - - return new File( - String.format("%04d/%02d/%02d/%02d-%02d-%02d-%03d-%05d", - dt.year().get(), - dt.monthOfYear().get(), - dt.dayOfMonth().get(), - dt.hourOfDay().get(), - dt.minuteOfHour().get(), - dt.secondOfMinute().get(), - node, - l - ) - ); - } - - @Override - public String toString() { - long l = id; - int yy = (int) (id / YMUL); - l -= yy*YMUL; - - int dd = (int) (l / DMUL); - l -= dd * DMUL; - - int ss = (int) (l / SMUL); - l -= ss * SMUL; - - int node = (int) (l / NMUL); - l -= node * NMUL; - return String.format("%03d-%03d-%05d-%03d-%05d", yy, dd, ss, node, l); - } - - public String toRrn() { - return Long.toString(id, 36); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - TxnId tranLogId = (TxnId) o; - return id == tranLogId.id; - } - - @Override - public int hashCode() { - return Objects.hash(id); - } - - /** - * Creates new TxnId object - * - * @param dt Transaction's TIMESTAMP DateTime - * @param node node id - * @param transactionId TransactionManager's ID - */ - public static TxnId create (DateTime dt, int node, long transactionId) { - TxnId id = new TxnId(); - if (dt.getZone() != DateTimeZone.UTC) - dt = dt.toDateTime(DateTimeZone.UTC); - - return id.init (dt.getYear()-2000, dt.getDayOfYear(), dt.getSecondOfDay(), node, transactionId); - } - - public static TxnId create (Instant instant, int node, long transactionId) { - TxnId id = new TxnId(); - ZonedDateTime utc = instant.atZone(UTC); - int seconds = (utc.getHour() * 3600) + (utc.getMinute()*60) + utc.getSecond(); - return id.init (utc.getYear()-2000, utc.getDayOfYear(), seconds, node, transactionId); - } - - /** - * @param idString TxnId in YYYY-DDD-SSS-NN-TTTTT format - * - * - */ - public static TxnId parse (String idString) { - Matcher matcher = pattern.matcher(idString); - if (!matcher.matches() && matcher.groupCount() != 5) - throw new IllegalArgumentException("Invalid idString '" + idString + "'"); - return new TxnId().init( - Integer.parseInt(matcher.group(1)), - Integer.parseInt(matcher.group(2)), - Integer.parseInt(matcher.group(3)), - Integer.parseInt(matcher.group(4)), - Integer.parseInt(matcher.group(5)) - ); - } - - /** - * Parse TxnId from long - * - * @param id value - * @return newly created TxnId - */ - public static TxnId parse (long id) { - if (id <0 || id > MAX_VALUE) - throw new IllegalArgumentException("Invalid id " + id); - return new TxnId(id); - } - - /** - * Parse TxnId from rrn - * - * @param rrn value - * @return newly created TxnId - */ - public static TxnId fromRrn (String rrn) { - long id = Long.parseLong(rrn, 36); - if (id <0 || id > MAX_VALUE) - throw new IllegalArgumentException("Invalid rrn " + rrn); - return new TxnId(id); - } -} diff --git a/modules/txn/src/test/java/org/jpos/transaction/TxnIdTest.java b/modules/txn/src/test/java/org/jpos/transaction/TxnIdTest.java deleted file mode 100644 index 2a8645b086..0000000000 --- a/modules/txn/src/test/java/org/jpos/transaction/TxnIdTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * jPOS Project [http://jpos.org] - * Copyright (C) 2000-2021 jPOS Software SRL - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package org.jpos.transaction; - -import org.joda.time.DateTime; -import org.junit.jupiter.api.Test; - -import java.time.Instant; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -public class TxnIdTest { - @Test - public void test() { - TxnId txnId = TxnId.create(DateTime.now(), 0, 1L); - TxnId txnId1 = TxnId.parse(txnId.toString()); - TxnId txnId2 = TxnId.parse(txnId.id()); - TxnId txnId3 = TxnId.fromRrn(txnId.toRrn()); - assertEquals(txnId1, txnId2); - assertEquals(txnId1.toString(), txnId2.toString()); - assertEquals(txnId2, txnId3); - assertEquals(txnId2.toString(), txnId3.toString()); - assertEquals(12, txnId.toRrn().length()); - } - - @Test - public void testBigId() { - try { - TxnId.parse(Long.MAX_VALUE); - fail ("Should raise exception"); - } catch (IllegalArgumentException ignored) { } - } - - @Test - public void testNegativeId() { - try { - TxnId.parse(-Long.MAX_VALUE); - fail ("Should raise exception"); - } catch (IllegalArgumentException ignored) { } - } - - @Test - public void testInvalidRrn() { - try { - TxnId id = TxnId.fromRrn(Long.toString(Long.MAX_VALUE,36)); - fail ("Should raise exception - TxnId=" + id); - } catch (IllegalArgumentException e) { - assertEquals("Invalid rrn 1y2p0ij32e8e7", e.getMessage()); - } - } - - @Test - public void testMaxValue() { - TxnId txnId = TxnId.parse(4738381338321616895L); - TxnId txnId1 = TxnId.parse(txnId.toString()); - TxnId txnId2 = TxnId.parse(txnId.id()); - TxnId txnId3 = TxnId.fromRrn(txnId.toRrn()); - assertEquals(txnId1, txnId2); - assertEquals(txnId1.toString(), txnId2.toString()); - assertEquals(txnId2, txnId3); - assertEquals(txnId2.toString(), txnId3.toString()); - assertEquals(12, txnId.toRrn().length()); - } - - @Test - public void testJodaTimeCompatibility() { - DateTime now = DateTime.now(); - Instant instant = now.toDate().toInstant(); - TxnId txnId1 = TxnId.create(now, 0, 1L); - TxnId txnId2 = TxnId.create(instant, 0, 1L); - assertEquals(txnId1, txnId2); - } -}