Coverage Summary for Class: Parameters (net.sf.persism)

Class Class, % Method, % Line, %
Parameters 100% (1/1) 100% (15/15) 100% (47/47)


1 package net.sf.persism; 2  3 import java.util.*; 4  5 /** 6  * Helper Class to represent parameters to a query. 7  */ 8 public final class Parameters { 9  10  private static final Log log = Log.getLogger(Parameters.class); 11  12  List<Object> parameters; 13  Map<String, Object> namedParameters; 14  Map<String, List<Integer>> parameterMap; 15  16  boolean areKeys = false; 17  boolean areNamed = false; 18  19  private static final Parameters none = new Parameters(); 20  21  // Default constructor for empty parameters or for constructing a parameter list. For internal use. 22  Parameters() { 23  this.parameters = new ArrayList<>(); 24  } 25  26  // this version is internal. The static method is the better way to call this. 27  Parameters(Object... parameters) { 28  this.parameters = new ArrayList<>(Arrays.asList(parameters)); 29  } 30  31  Parameters(Map<String, Object> params) { 32  this(); 33  areNamed = true; 34  namedParameters = params; 35  } 36  37  /** 38  * Static initializer for a new set of Parameters. 39  * <pre>{@code 40  * Employee employee = session.fetch(Employee.class, sql("SELECT * FROM Employees WHERE LastName=? and FirstName=?"), params("Leverling", "Janet")); 41  * }</pre> 42  * 43  * @param values varargs list of arbitrary parameters for a query. 44  * @return new Parameters object 45  */ 46  public static Parameters params(Object... values) { 47  return new Parameters(values); 48  } 49  50  /** 51  * Static initializer for named parameters. 52  * <pre>{@code 53  * List<Contact> contacts; 54  * contacts = session.query(Contact.class, 55  * where("(:firstname = @name OR :company = @name) and :lastname = @last and :city = @city and :amountOwed > @owe ORDER BY :dateAdded"), 56  * params(Map.of("name", "Fred", "last", "Flintstone", "owe", 10, "city", "Somewhere"))); 57  * }</pre> 58  * 59  * @param nameValuePair - use Map.of("key1", value, "key2", value) etc. 60  * @return new Parameters object 61  */ 62  public static Parameters params(Map<String, Object> nameValuePair) { 63  return new Parameters(nameValuePair); 64  } 65  66  /** 67  * Represents no Parameters. 68  * 69  * @return empty array instance of Parameters 70  */ 71  public static Parameters none() { 72  return none; 73  } 74  75  int size() { 76  return parameters.size(); 77  } 78  79  void add(Object parameter) { 80  parameters.add(parameter); 81  } 82  83  Object get(int index) { 84  return parameters.get(index); 85  } 86  87  void set(int index, Object parameter) { 88  parameters.set(index, parameter); 89  } 90  91  void setParameterMap(Map<String, List<Integer>> parameterMap) { 92  this.parameterMap = parameterMap; 93  94  assert areNamed; 95  96  // find largest index 97  int max = 0; 98  for (List<Integer> integers : parameterMap.values()) { 99  Optional<Integer> x = integers.stream().max(Integer::compare); 100  if (x.isPresent() && x.get() > max) { 101  max = x.get(); 102  } 103  } 104  105  log.debug("setParameterMap MAX? %d", max); 106  Object[] arr = new Object[max]; 107  Set<String> paramsNotFound = new TreeSet<>(); 108  109  parameterMap.keySet().forEach((key) -> { 110  if (namedParameters.containsKey(key)) { 111  Object value = namedParameters.get(key); 112  113  List<Integer> list = parameterMap.get(key); 114  for (Integer integer : list) { 115  arr[integer - 1] = value; 116  } 117  } else { 118  paramsNotFound.add(key); 119  } 120  }); 121  122  Set<String> mistypeSet = new TreeSet<>(namedParameters.keySet()); 123  mistypeSet.removeAll(parameterMap.keySet()); 124  125  if (paramsNotFound.size() > 0) { 126  throw new PersismException(Message.QueryParameterNamesMissingOrNotFound.message(paramsNotFound, mistypeSet)); 127  } 128  parameters.clear(); 129  parameters.addAll(Arrays.asList(arr)); 130  } 131  132  Object[] toArray() { 133  return parameters.toArray(); 134  } 135  136  /** 137  * @hidden 138  */ 139  @Override 140  public String toString() { 141  return "" + parameters; 142  } 143 }