View Javadoc
1   /*
2   Copyright (c) 2013 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
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  
26  import com.healthmarketscience.jackcess.Column;
27  import com.healthmarketscience.jackcess.Cursor;
28  import com.healthmarketscience.jackcess.Row;
29  import com.healthmarketscience.jackcess.impl.CursorImpl;
30  
31  /**
32   * Builder style class for constructing a {@link Cursor} Iterable/Iterator.
33   *
34   * @author James Ahlborn
35   * @usage _general_class_
36   */
37  public class IterableBuilder implements Iterable<Row>
38  {
39    public enum Type {
40      SIMPLE, COLUMN_MATCH, ROW_MATCH;
41    }
42  
43    private final Cursor _cursor;
44    private Type _type = Type.SIMPLE;
45    private boolean _forward = true;
46    private boolean _reset = true;
47    private Collection<String> _columnNames;
48    private ColumnMatcher _columnMatcher;
49    private Object _matchPattern;
50  
51    public IterableBuilder(Cursor cursor) {
52      _cursor = cursor;
53    }
54  
55    public Collection<String> getColumnNames() {
56      return _columnNames;
57    }
58  
59    public ColumnMatcher getColumnMatcher() {
60      return _columnMatcher;
61    }
62  
63    public boolean isForward() {
64      return _forward;
65    }
66  
67    public boolean isReset() {
68      return _reset;
69    }
70  
71    /**
72     * @usage _advanced_method_
73     */
74    public Object getMatchPattern() {
75      return _matchPattern;
76    }
77  
78    /**
79     * @usage _advanced_method_
80     */
81    public Type getType() {
82      return _type;
83    }
84  
85    public IterableBuilder forward() {
86      return setForward(true);
87    }
88  
89    public IterableBuilder reverse() {
90      return setForward(false);
91    }
92  
93    public IterableBuilder setForward(boolean forward) {
94      _forward = forward;
95      return this;
96    }
97  
98    public IterableBuilder reset(boolean reset) {
99      _reset = reset;
100     return this;
101   }
102 
103   public IterableBuilder setColumnNames(Collection<String> columnNames) {
104     _columnNames = columnNames;
105     return this;
106   }
107 
108   public IterableBuilder addColumnNames(Iterable<String> columnNames) {
109     if(columnNames != null) {
110       for(String name : columnNames) {
111         addColumnName(name);
112       }
113     }
114     return this;
115   }
116 
117   public IterableBuilder addColumns(Iterable<? extends Column> cols) {
118     if(cols != null) {
119       for(Column col : cols) {
120         addColumnName(col.getName());
121       }
122     }
123     return this;
124   }
125 
126   public IterableBuilder addColumnNames(String... columnNames) {
127     if(columnNames != null) {
128       for(String name : columnNames) {
129         addColumnName(name);
130       }
131     }
132     return this;
133   }
134 
135   private void addColumnName(String columnName) {
136     if(_columnNames == null) {
137       _columnNames = new HashSet<String>();
138     }
139     _columnNames.add(columnName);
140   }
141 
142   public IterableBuilder setMatchPattern(Column columnPattern, 
143                                          Object valuePattern) {
144     _type = Type.COLUMN_MATCH;
145     _matchPattern = new AbstractMap.SimpleImmutableEntry<Column,Object>(
146         columnPattern, valuePattern);
147     return this;
148   }
149 
150   public IterableBuilder setMatchPattern(String columnNamePattern, 
151                                          Object valuePattern) {
152     return setMatchPattern(_cursor.getTable().getColumn(columnNamePattern),
153                            valuePattern);
154   }
155 
156   public IterableBuilder setMatchPattern(Map<String,?> rowPattern) {
157     _type = Type.ROW_MATCH;
158     _matchPattern = rowPattern;
159     return this;
160   }
161 
162   public IterableBuilder addMatchPattern(String columnNamePattern, 
163                                          Object valuePattern)
164   {
165     _type = Type.ROW_MATCH;
166     @SuppressWarnings("unchecked")
167     Map<String,Object> matchPattern = ((Map<String,Object>)_matchPattern);
168     if(matchPattern == null) {
169       matchPattern = new HashMap<String,Object>();
170       _matchPattern = matchPattern;
171     }
172     matchPattern.put(columnNamePattern, valuePattern);
173     return this;
174   }    
175 
176   public IterableBuilder setColumnMatcher(ColumnMatcher columnMatcher) {
177     _columnMatcher = columnMatcher;
178     return this;
179   }
180 
181   public Iterator<Row> iterator() {
182     return ((CursorImpl)_cursor).iterator(this);
183   }
184 }