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