1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess.util;
18
19 import java.util.AbstractMap;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.Map;
25 import java.util.stream.Stream;
26 import java.util.stream.StreamSupport;
27
28 import com.healthmarketscience.jackcess.Column;
29 import com.healthmarketscience.jackcess.Cursor;
30 import com.healthmarketscience.jackcess.Row;
31 import com.healthmarketscience.jackcess.impl.CursorImpl;
32
33
34
35
36
37
38
39 public class IterableBuilder implements Iterable<Row>
40 {
41 public enum Type {
42 SIMPLE, COLUMN_MATCH, ROW_MATCH;
43 }
44
45 private final Cursor _cursor;
46 private Type _type = Type.SIMPLE;
47 private boolean _forward = true;
48 private boolean _reset = true;
49 private Collection<String> _columnNames;
50 private ColumnMatcher _columnMatcher;
51 private Object _matchPattern;
52
53 public IterableBuilder(Cursor cursor) {
54 _cursor = cursor;
55 }
56
57 public Collection<String> getColumnNames() {
58 return _columnNames;
59 }
60
61 public ColumnMatcher getColumnMatcher() {
62 return _columnMatcher;
63 }
64
65 public boolean isForward() {
66 return _forward;
67 }
68
69 public boolean isReset() {
70 return _reset;
71 }
72
73
74
75
76 public Object getMatchPattern() {
77 return _matchPattern;
78 }
79
80
81
82
83 public Type getType() {
84 return _type;
85 }
86
87 public IterableBuilder forward() {
88 return setForward(true);
89 }
90
91 public IterableBuilder reverse() {
92 return setForward(false);
93 }
94
95 public IterableBuilder setForward(boolean forward) {
96 _forward = forward;
97 return this;
98 }
99
100 public IterableBuilder reset(boolean reset) {
101 _reset = reset;
102 return this;
103 }
104
105 public IterableBuilder setColumnNames(Collection<String> columnNames) {
106 _columnNames = columnNames;
107 return this;
108 }
109
110 public IterableBuilder addColumnNames(Iterable<String> columnNames) {
111 if(columnNames != null) {
112 for(String name : columnNames) {
113 addColumnName(name);
114 }
115 }
116 return this;
117 }
118
119 public IterableBuilder addColumns(Iterable<? extends Column> cols) {
120 if(cols != null) {
121 for(Column col : cols) {
122 addColumnName(col.getName());
123 }
124 }
125 return this;
126 }
127
128 public IterableBuilder addColumnNames(String... columnNames) {
129 if(columnNames != null) {
130 for(String name : columnNames) {
131 addColumnName(name);
132 }
133 }
134 return this;
135 }
136
137 private void addColumnName(String columnName) {
138 if(_columnNames == null) {
139 _columnNames = new HashSet<String>();
140 }
141 _columnNames.add(columnName);
142 }
143
144 public IterableBuilder setMatchPattern(Column columnPattern,
145 Object valuePattern) {
146 _type = Type.COLUMN_MATCH;
147 _matchPattern = new AbstractMap.SimpleImmutableEntry<Column,Object>(
148 columnPattern, valuePattern);
149 return this;
150 }
151
152 public IterableBuilder setMatchPattern(String columnNamePattern,
153 Object valuePattern) {
154 return setMatchPattern(_cursor.getTable().getColumn(columnNamePattern),
155 valuePattern);
156 }
157
158 public IterableBuilder setMatchPattern(Map<String,?> rowPattern) {
159 _type = Type.ROW_MATCH;
160 _matchPattern = rowPattern;
161 return this;
162 }
163
164 public IterableBuilder addMatchPattern(String columnNamePattern,
165 Object valuePattern)
166 {
167 _type = Type.ROW_MATCH;
168 @SuppressWarnings("unchecked")
169 Map<String,Object> matchPattern = ((Map<String,Object>)_matchPattern);
170 if(matchPattern == null) {
171 matchPattern = new HashMap<String,Object>();
172 _matchPattern = matchPattern;
173 }
174 matchPattern.put(columnNamePattern, valuePattern);
175 return this;
176 }
177
178 public IterableBuilder setColumnMatcher(ColumnMatcher columnMatcher) {
179 _columnMatcher = columnMatcher;
180 return this;
181 }
182
183 @Override
184 public Iterator<Row> iterator() {
185 return ((CursorImpl)_cursor).iterator(this);
186 }
187
188
189
190
191 public Stream<Row> stream() {
192 return StreamSupport.stream(spliterator(), false);
193 }
194 }