001package com.avaje.ebean;
002
003import java.util.ArrayList;
004import java.util.HashMap;
005import java.util.List;
006import java.util.Map;
007
008/**
009 * Created by rob on 16/07/15.
010 */
011public class WhenFind {
012
013  List<WhenBeanReturn<?>> byId = new ArrayList();
014
015  Map<Class<?>,WhenBeanReturn<?>> byUnique = new HashMap<Class<?>,WhenBeanReturn<?>>();
016
017  public <T> WhenBeanReturn<T> byId(Class<T> beanType) {
018
019    return byId(beanType, null);
020  }
021
022  public <T> WhenBeanReturn<T> byId(Class<T> beanType, Object id) {
023
024    WhenBeanReturn<T> ret = new WhenBeanReturn<T>(beanType, id);
025    byId.add(ret);
026    return ret;
027  }
028
029  public <T> WhenBeanReturn<T> byUnique(Class<T> beanType) {
030
031    WhenBeanReturn<T> ret = new WhenBeanReturn<T>(beanType);
032    byUnique.put(beanType, ret);
033    return ret;
034  }
035
036  protected WhenBeanReturn findMatchByUnique(Class<?> beanType) {
037    return byUnique.get(beanType);
038  }
039
040  protected WhenBeanReturn findMatchById(Class<?> beanType, Object id) {
041
042    for (WhenBeanReturn<?> byIdReturn : byId) {
043      if (byIdReturn.isMatch(beanType, id)){
044        return byIdReturn;
045      }
046    }
047
048    for (WhenBeanReturn<?> byIdReturn : byId) {
049      if (byIdReturn.isMatch(beanType)){
050        return byIdReturn;
051      }
052    }
053
054    // no match
055    return null;
056  }
057}