1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*
Copyright (c) 2013 James Ahlborn
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.healthmarketscience.jackcess.util;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.impl.CursorImpl;
/**
* Builder style class for constructing a Cursor Iterable/Iterator.
*
* @author James Ahlborn
*/
public class IterableBuilder implements Iterable<Row>
{
public enum Type {
SIMPLE, COLUMN_MATCH, ROW_MATCH;
}
private final Cursor _cursor;
private Type _type = Type.SIMPLE;
private boolean _forward = true;
private boolean _reset = true;
private Collection<String> _columnNames;
private ColumnMatcher _columnMatcher;
private Object _matchPattern;
public IterableBuilder(Cursor cursor) {
_cursor = cursor;
}
public Collection<String> getColumnNames() {
return _columnNames;
}
public ColumnMatcher getColumnMatcher() {
return _columnMatcher;
}
public boolean isForward() {
return _forward;
}
public boolean isReset() {
return _reset;
}
/**
* @usage _advanced_method_
*/
public Object getMatchPattern() {
return _matchPattern;
}
/**
* @usage _advanced_method_
*/
public Type getType() {
return _type;
}
public IterableBuilder forward() {
return setForward(true);
}
public IterableBuilder reverse() {
return setForward(false);
}
public IterableBuilder setForward(boolean forward) {
_forward = forward;
return this;
}
public IterableBuilder reset(boolean reset) {
_reset = reset;
return this;
}
public IterableBuilder setColumnNames(Collection<String> columnNames) {
_columnNames = columnNames;
return this;
}
public IterableBuilder addColumnNames(Iterable<String> columnNames) {
if(columnNames != null) {
for(String name : columnNames) {
addColumnName(name);
}
}
return this;
}
public IterableBuilder addColumns(Iterable<? extends Column> cols) {
if(cols != null) {
for(Column col : cols) {
addColumnName(col.getName());
}
}
return this;
}
public IterableBuilder addColumnNames(String... columnNames) {
if(columnNames != null) {
for(String name : columnNames) {
addColumnName(name);
}
}
return this;
}
private void addColumnName(String columnName) {
if(_columnNames == null) {
_columnNames = new HashSet<String>();
}
_columnNames.add(columnName);
}
public IterableBuilder setMatchPattern(Column columnPattern,
Object valuePattern) {
_type = Type.COLUMN_MATCH;
_matchPattern = new AbstractMap.SimpleImmutableEntry<Column,Object>(
columnPattern, valuePattern);
return this;
}
public IterableBuilder setMatchPattern(String columnNamePattern,
Object valuePattern) {
return setMatchPattern(_cursor.getTable().getColumn(columnNamePattern),
valuePattern);
}
public IterableBuilder setMatchPattern(Map<String,?> rowPattern) {
_type = Type.ROW_MATCH;
_matchPattern = rowPattern;
return this;
}
public IterableBuilder addMatchPattern(String columnNamePattern,
Object valuePattern)
{
_type = Type.ROW_MATCH;
@SuppressWarnings("unchecked")
Map<String,Object> matchPattern = ((Map<String,Object>)_matchPattern);
if(matchPattern == null) {
matchPattern = new HashMap<String,Object>();
_matchPattern = matchPattern;
}
matchPattern.put(columnNamePattern, valuePattern);
return this;
}
public IterableBuilder setColumnMatcher(ColumnMatcher columnMatcher) {
_columnMatcher = columnMatcher;
return this;
}
public Iterator<Row> iterator() {
return ((CursorImpl)_cursor).iterator(this);
}
}
|