View Javadoc
1   /*
2   Copyright (c) 2015 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.Iterator;
20  import java.util.stream.Stream;
21  import java.util.stream.StreamSupport;
22  
23  import com.healthmarketscience.jackcess.Database;
24  import com.healthmarketscience.jackcess.Table;
25  import com.healthmarketscience.jackcess.impl.DatabaseImpl;
26  
27  /**
28   * Builder style class for constructing a {@link Database} Iterable/Iterator
29   * for {@link Table}s.  By default, normal (non-system, non-linked tables) and
30   * linked tables are included and system tables are not.
31   *
32   * @author James Ahlborn
33   * @usage _general_class_
34   */
35  public class TableIterableBuilder implements Iterable<Table>
36  {
37    private final Database _db;
38    private boolean _includeNormalTables = true;
39    private boolean _includeSystemTables;
40    private boolean _includeLinkedTables = true;
41  
42    public TableIterableBuilder(Database db) {
43      _db = db;
44    }
45  
46    public boolean isIncludeNormalTables() {
47      return _includeNormalTables;
48    }
49  
50    public boolean isIncludeSystemTables() {
51      return _includeSystemTables;
52    }
53  
54    public boolean isIncludeLinkedTables() {
55      return _includeLinkedTables;
56    }
57  
58    public TableIterableBuilder setIncludeNormalTables(boolean includeNormalTables) {
59      _includeNormalTables = includeNormalTables;
60      return this;
61    }
62  
63    public TableIterableBuilder setIncludeSystemTables(boolean includeSystemTables) {
64      _includeSystemTables = includeSystemTables;
65      return this;
66    }
67  
68    public TableIterableBuilder setIncludeLinkedTables(boolean includeLinkedTables) {
69      _includeLinkedTables = includeLinkedTables;
70      return this;
71    }
72  
73    /**
74     * Convenience method to set the flags to include only non-linked (local)
75     * user tables.
76     */
77    public TableIterableBuilder withLocalUserTablesOnly() {
78      setIncludeNormalTables(true);
79      setIncludeSystemTables(false);
80      return setIncludeLinkedTables(false);
81    }
82  
83    /**
84     * Convenience method to set the flags to include only system tables.
85     */
86    public TableIterableBuilder withSystemTablesOnly() {
87      setIncludeNormalTables(false);
88      setIncludeSystemTables(true);
89      return setIncludeLinkedTables(false);
90    }
91  
92    @Override
93    public Iterator<Table> iterator() {
94      return ((DatabaseImpl)_db).iterator(this);
95    }
96  
97    /**
98     * @return a Stream using the default Iterator.
99     */
100   public Stream<Table> stream() {
101     return StreamSupport.stream(spliterator(), false);
102   }
103 }