You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IterableBuilder.java 4.8KB

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