12/04/2007

Enum wrapper for javax.transaction.Status

javax.transaction.Status contains a list of int constants representing transaction status. It suffers many drawbacks like lack of type-check and unreadable output. Here is my attempt to create a wrapper enum type:
package javahowto;

import java.util.HashMap;
import java.util.Map;
import javax.transaction.Status;
public enum TransactionStatusEnum {
STATUS_ACTIVE (Status.STATUS_ACTIVE),
STATUS_COMMITTED (Status.STATUS_COMMITTED),
STATUS_COMMITTING (Status.STATUS_COMMITTING),
STATUS_MARKED_ROLLBACK (Status.STATUS_MARKED_ROLLBACK),
STATUS_NO_TRANSACTION (Status.STATUS_NO_TRANSACTION),
STATUS_PREPARED (Status.STATUS_PREPARED),
STATUS_PREPARING (Status.STATUS_PREPARING),
STATUS_ROLLEDBACK (Status.STATUS_ROLLEDBACK),
STATUS_ROLLING_BACK (Status.STATUS_ROLLING_BACK),
STATUS_UNKNOWN (Status.STATUS_UNKNOWN);

private static Map<Integer, TransactionStatusEnum> codeToEnums
= new HashMap<Integer, TransactionStatusEnum>();

static {
codeToEnums.put(Status.STATUS_ACTIVE, STATUS_ACTIVE);
codeToEnums.put(Status.STATUS_COMMITTED, STATUS_COMMITTED);
codeToEnums.put(Status.STATUS_COMMITTING, STATUS_COMMITTING);
codeToEnums.put(Status.STATUS_MARKED_ROLLBACK, STATUS_MARKED_ROLLBACK);
codeToEnums.put(Status.STATUS_NO_TRANSACTION, STATUS_NO_TRANSACTION);
codeToEnums.put(Status.STATUS_PREPARED, STATUS_PREPARED);
codeToEnums.put(Status.STATUS_PREPARING, STATUS_PREPARING);
codeToEnums.put(Status.STATUS_ROLLEDBACK, STATUS_ROLLEDBACK);
codeToEnums.put(Status.STATUS_ROLLING_BACK, STATUS_ROLLING_BACK);
codeToEnums.put(Status.STATUS_UNKNOWN, STATUS_UNKNOWN);
}

private Integer statusCode;

public Integer getStatusCode() {
return statusCode;
}

public static TransactionStatusEnum getEnumFor(Integer i) {
return codeToEnums.get(i);
}

@Override public String toString() {
return super.toString() + "(" + statusCode + ")";
}

private TransactionStatusEnum(int statusCode) {
this.statusCode = statusCode;
}
}
All 10 transaction status code are mirrored in the enum type. You can get the enum instance based on javax.transaction.Status code. The toString method of the enum is also overridden to print out both the string literal and the status code. This is a sample test program to illustrate these usage. Comments are welcome. Another implementation without using static mapping is described in this post (Enum wrapper for javax.transaction.Status (2))

Below is a testcase that prints the 2-way association:
package javahowto;
import java.util.EnumSet;

public class TestTransactionStatusEnum {
public static void main(String[] args) {
System.out.println("status code -> enum:");
for(int i = 0; i < TransactionStatusEnum.values().length; i++) {
System.out.println(TransactionStatusEnum.getEnumFor(i));
}

System.out.println("enum -> status code:");
for(TransactionStatusEnum e : EnumSet.allOf(TransactionStatusEnum.class)) {
System.out.println(e.toString() + "\t" + e.getStatusCode());
}
}
}
In the above test, I use 2 ways to get all elements of an enum type:
  • values() is a built-in public static method in all enum type, and returns an array of this type of enum.
  • allOf(Class<E> elementType) is a public static method in java.util.EnumSet, which returns a set of this type of enum.
Technorati Tags: ,

3 comments:

Anonymous said...

for(TransactionStatusEnum e : EnumSet.allOf(TransactionStatusEnum.class)) {
should be written as
for(TransactionStatusEnum e : TransactionStatusEnum.values()) {

Steve Smith said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Vũ Diệu Linh said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000