1 package org.davidmoten.rx.jdbc;
2
3 import java.util.Collection;
4 import java.util.List;
5
6 import com.github.davidmoten.guavamini.Lists;
7
8
9
10
11 public final class Parameter {
12
13 static final Parameter NULL = new Parameter(null);
14
15 private final String name;
16
17
18
19 private final Object value;
20
21
22
23
24
25
26 Parameter(Object value) {
27 this(null, value);
28 }
29
30 Parameter(String name, Object value) {
31 this.name = name;
32 this.value = value;
33 }
34
35
36
37
38
39
40 Object value() {
41 return value;
42 }
43
44 boolean isCollection() {
45 return value != null && (value instanceof Collection);
46 }
47
48 int size() {
49
50 if (value != null) {
51 if (value instanceof Collection) {
52 return ((Collection<?>) value).size();
53 } else {
54 return 1;
55 }
56 } else {
57 return 1;
58 }
59 }
60
61 boolean hasName() {
62 return name != null;
63 }
64
65 String name() {
66 return name;
67 }
68
69 public static ParameterListBuilder named(String name, String value) {
70 return new ParameterListBuilder(Lists.newArrayList()).named(name, value);
71 }
72
73 public static class ParameterListBuilder {
74 private final List<Parameter> list;
75 private String lastName;
76
77 ParameterListBuilder(List<Parameter> list) {
78 this.list = list;
79 }
80
81 public ParameterListBuilder named(String name, String value) {
82 list.add(new Parameter(name, value));
83 lastName = name;
84 return this;
85 }
86
87 public ParameterListBuilder value(String value) {
88 return named(lastName, value);
89 }
90
91 public List<Parameter> list() {
92 return list;
93 }
94 }
95
96 @Override
97 public String toString() {
98 StringBuilder b = new StringBuilder();
99 b.append("Parameter[");
100 b.append("name=");
101 b.append(name);
102 b.append(", value=");
103 b.append(String.valueOf(value));
104 b.append("]");
105 return b.toString();
106 }
107
108 public static Parameter create(String name, Object value) {
109 return new Parameter(name, value);
110 }
111
112 }