I have a Java service returning an incremented number on each call. So I put an invoke statement into a template block to disable caching.
For sure I can ignore this warning. But when there are a lot of such a warnings they confuse other developers.
[template public incCounter(ctx : OclAny, str : String)][invoke('...service.CommonService', 'incCounter(java.lang.Object, java.lang.String)', Sequence{ctx, str})/][/template]
[template public getCounter(ctx : OclAny, str : String)][invoke('...service.CommonService', 'getCounter(java.lang.Object, java.lang.String)', Sequence{ctx, str})/][/template]
public class CommonService {
public int incCounter(Object context, String str) {
return Counter.getInstance().incCounter(context, str);
}
public int getCounter(Object context, String str) {
return Counter.getInstance().getCounter(context, str);
}
}
public class Counter {
private static volatile Counter instance;
private Map<Object, Map<String, Integer>> contexts = new HashMap<Object, Map<String, Integer>>();
private Counter() {
}
public static Counter getInstance() {
if (instance == null) {
synchronized(Counter.class){
if (instance == null) {
instance = new Counter();
}
}
}
return instance;
}
private Map<String, Integer> getCounters(Object context) {
if (contexts.containsKey(context)) {
return contexts.get(context);
}
final Map<String, Integer> counters = new HashMap<String, Integer>();
contexts.put(context, counters);
return counters;
}
public int incCounter(Object context, String str) {
final Map<String, Integer> counters = getCounters(context);
if (counters.containsKey(str)) {
int counter = counters.get(str) + 1;
counters.put(str, counter);
return counter;
}
counters.put(str, 1);
return 1;
}
public int getCounter(Object context, String str) {
final Map<String, Integer> counters = getCounters(context);
if (counters.containsKey(str)) {
return counters.get(str);
}
return 0;
}
}