1 package com.exsoinn.util.epf;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.stream.Collectors;
7
8 /**
9 * Created by QuijadaJ on 9/7/2017.
10 */
11 public class Utilities {
12 public static final String MULTI_VAL_DELIM = "||";
13
14 /*
15 * Suppresses default constructor, ensuring non-instantiability.
16 */
17 private Utilities() {
18
19 }
20
21
22 /**
23 * Converts the passed in pCtxList to a {@link Utilities#MULTI_VAL_DELIM} delimited String. Certain
24 * things are enforced in the passed in {@link Context} list. If any are violated, then a runtime {@link IllegalArgumentException}
25 * gets thrown:
26 * 1) Every element in the list must be either a primitive or recursible (I.e. complex) Context
27 * object.
28 * 2) In case of a recursible (I.e. complex) Context, then it must contain at most one name/value pair.
29 * Later on, as needed, can relax these enforcements as it makes sense for rules engine evolution/needs.
30 *
31 * @param pCtxList - pCtxList
32 * @return - A delimited string, using {@link Utilities#MULTI_VAL_DELIM} as the delimiter.
33 * @throws IllegalArgumentException - TODO
34 */
35 public static String toDelimitedString(List<Context> pCtxList) throws IllegalArgumentException {
36 List<String> leftOpVals = new ArrayList<>();
37 Iterator<Context> srIt = pCtxList.iterator();
38 while (srIt.hasNext()) {
39 Context c = srIt.next();
40
41 if (c.isRecursible()) {
42 if (c.entrySet().size() > 1) {
43 throw new IllegalArgumentException("A complex object in the context list "
44 + "contained more than one name/value pair: " + c.stringRepresentation());
45 }
46 leftOpVals.add(c.entrySet().iterator().next().getValue().stringRepresentation());
47 } else if (c.isPrimitive()) {
48 leftOpVals.add(c.stringRepresentation());
49 } else {
50 throw new IllegalArgumentException("An object in the array "
51 + "is neither complex nor a primitive, unsure what to do: " + c.stringRepresentation());
52 }
53 }
54 return leftOpVals.stream().collect(Collectors.joining(MULTI_VAL_DELIM));
55 }
56 }