Skip to content

Commit

Permalink
Target type inference in Context
Browse files Browse the repository at this point in the history
Using type inference in generic methods allows to avoid explicit type casting
in code, eg.:
  @SuppressWarnings("unchecked")
  Integer someMilis = (Integer) ctx.get(CTX_SOME_TIME);

Now it can be written as:
  Integer someMilis = ctx.get(CTX_SOME_TIME);
  • Loading branch information
demsey committed May 3, 2018
1 parent fb83087 commit 6ed7f7d
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions jpos/src/main/java/org/jpos/transaction/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,44 @@ public void persist (Object key) {
public void evict (Object key) {
getPMap().remove (key);
}

/**
* Get
* Get object instance from transaction context.
*
* @param <T> desired type of object instance
* @param key the key of object instance
* @return object instance if exist in context or {@code null} otherwise
*/
public Object get (Object key) {
return getMap().get (key);
public <T> T get(Object key) {
@SuppressWarnings("unchecked")
T obj = (T) getMap().get(key);
return obj;
}
public Object get (Object key, Object defValue) {
Object obj = getMap().get (key);

/**
* Get object instance from transaction context.
*
* @param <T> desired type of object instance
* @param key the key of object instance
* @param defValue default value returned if there is no value in context
* @return object instance if exist in context or {@code defValue} otherwise
*/
public <T> T get(Object key, T defValue) {
@SuppressWarnings("unchecked")
T obj = (T) getMap().get(key);
return obj != null ? obj : defValue;
}

/**
* Transient remove
*/
public synchronized Object remove (Object key) {
getPMap().remove (key);
return getMap().remove (key);
public synchronized <T> T remove(Object key) {
getPMap().remove(key);
@SuppressWarnings("unchecked")
T obj = (T) getMap().get(key);
return obj;
}

public String getString (Object key) {
Object obj = getMap().get (key);
if (obj instanceof String)
Expand Down

0 comments on commit 6ed7f7d

Please sign in to comment.