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.Collection;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  
23  import com.healthmarketscience.jackcess.Column;
24  import com.healthmarketscience.jackcess.IndexCursor;
25  import com.healthmarketscience.jackcess.Row;
26  import com.healthmarketscience.jackcess.impl.IndexCursorImpl;
27  
28  /**
29   * Builder style class for constructing an {@link IndexCursor} entry
30   * Iterable/Iterator.
31   *
32   * @author James Ahlborn
33   * @usage _general_class_
34   */
35  public class EntryIterableBuilder implements Iterable<Row>
36  {
37    private final IndexCursor _cursor;
38  
39    private Collection<String> _columnNames;
40    private Object[] _entryValues;
41    private ColumnMatcher _columnMatcher;
42  
43    public EntryIterableBuilder(IndexCursor cursor, Object... entryValues) {
44      _cursor = cursor;
45      _entryValues = entryValues;
46    }
47  
48    public Collection<String> getColumnNames() {
49      return _columnNames;
50    }
51  
52    public ColumnMatcher getColumnMatcher() {
53      return _columnMatcher;
54    }
55  
56    public Object[] getEntryValues() {
57      return _entryValues;
58    }
59  
60    public EntryIterableBuilder setColumnNames(Collection<String> columnNames) {
61      _columnNames = columnNames;
62      return this;
63    }
64  
65    public EntryIterableBuilder addColumnNames(Iterable<String> columnNames) {
66      if(columnNames != null) {
67        for(String name : columnNames) {
68          addColumnName(name);
69        }
70      }
71      return this;
72    }
73  
74    public EntryIterableBuilder addColumns(Iterable<? extends Column> cols) {
75      if(cols != null) {
76        for(Column col : cols) {
77          addColumnName(col.getName());
78        }
79      }
80      return this;
81    }
82  
83    public EntryIterableBuilder addColumnNames(String... columnNames) {
84      if(columnNames != null) {
85        for(String name : columnNames) {
86          addColumnName(name);
87        }
88      }
89      return this;
90    }
91  
92    private void addColumnName(String columnName) {
93      if(_columnNames == null) {
94        _columnNames = new HashSet<String>();
95      }
96      _columnNames.add(columnName);
97    }
98  
99    public EntryIterableBuilder setEntryValues(Object... entryValues) {
100     _entryValues = entryValues;
101     return this;
102   }
103 
104   public EntryIterableBuilder setColumnMatcher(ColumnMatcher columnMatcher) {
105     _columnMatcher = columnMatcher;
106     return this;
107   }
108 
109   public Iterator<Row> iterator() {
110     return ((IndexCursorImpl)_cursor).entryIterator(this);
111   }  
112 }