mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-24 21:07:26 +08:00
added reflection based FactoryMap implementation
This commit is contained in:
parent
75fe6532e0
commit
d2ccc3159f
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2006 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* FactoryMap provides
|
||||
*
|
||||
* @author Timo Westkämper
|
||||
* @version $Id: FactoryMap.java 3549 2008-06-10 17:56:47Z tiwe $
|
||||
*/
|
||||
public abstract class FactoryMap<V>{
|
||||
|
||||
private final Map<List<?>,V> cache = new HashMap<List<?>,V>();
|
||||
|
||||
private Method createMethod;
|
||||
|
||||
public FactoryMap(){
|
||||
for (Method m : getClass().getMethods()){
|
||||
if (m.getName().equals("create")) createMethod = m;
|
||||
}
|
||||
if (createMethod == null){
|
||||
throw new IllegalArgumentException("No create method given");
|
||||
}else{
|
||||
createMethod.setAccessible(true);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public final V get(Object... args){
|
||||
List<?> key = Arrays.asList(args);
|
||||
if (cache.containsKey(key)){
|
||||
return cache.get(key);
|
||||
}else{
|
||||
try {
|
||||
V value = (V) createMethod.invoke(this, args);
|
||||
cache.put(key, value);
|
||||
return value;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user