io.ebean.mocker.WhenFind Maven / Gradle / Ivy
package io.ebean.mocker;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by rob on 16/07/15.
*/
public class WhenFind {
List> byId = new ArrayList<>();
Map, WhenBeanReturn>> byUnique = new HashMap<>();
public WhenBeanReturn byId(Class beanType) {
return byId(beanType, null);
}
public WhenBeanReturn byId(Class beanType, Object id) {
WhenBeanReturn ret = new WhenBeanReturn<>(beanType, id);
byId.add(ret);
return ret;
}
public WhenBeanReturn byUnique(Class beanType) {
WhenBeanReturn ret = new WhenBeanReturn<>(beanType);
byUnique.put(beanType, ret);
return ret;
}
@SuppressWarnings("unchecked")
protected WhenBeanReturn findMatchByUnique(Class beanType) {
return (WhenBeanReturn) byUnique.get(beanType);
}
@SuppressWarnings("unchecked")
protected WhenBeanReturn findMatchById(Class beanType, Object id) {
for (WhenBeanReturn> byIdReturn : byId) {
if (byIdReturn.isMatch(beanType, id)) {
return (WhenBeanReturn) byIdReturn;
}
}
for (WhenBeanReturn> byIdReturn : byId) {
if (byIdReturn.isMatch(beanType)) {
return (WhenBeanReturn) byIdReturn;
}
}
// no match
return null;
}
}