From: James Ahlborn Date: Sun, 31 Mar 2013 04:05:18 +0000 (+0000) Subject: oops missed these X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=580263a57a843463b8678f2a5a37264381ae4e11;p=jackcess.git oops missed these git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/jackcess-2@701 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/java/com/healthmarketscience/jackcess/util/EntryIterableBuilder.java b/src/java/com/healthmarketscience/jackcess/util/EntryIterableBuilder.java new file mode 100644 index 0000000..54b88a8 --- /dev/null +++ b/src/java/com/healthmarketscience/jackcess/util/EntryIterableBuilder.java @@ -0,0 +1,114 @@ +/* +Copyright (c) 2013 James Ahlborn + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +USA +*/ + +package com.healthmarketscience.jackcess.util; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; + +import com.healthmarketscience.jackcess.Column; +import com.healthmarketscience.jackcess.IndexCursor; +import com.healthmarketscience.jackcess.Row; +import com.healthmarketscience.jackcess.impl.IndexCursorImpl; + +/** + * Builder style class for constructing an IndexCursor entry + * Iterable/Iterator. + * + * @author James Ahlborn + */ +public class EntryIterableBuilder implements Iterable +{ + private final IndexCursor _cursor; + + private Collection _columnNames; + private Object[] _entryValues; + private ColumnMatcher _columnMatcher; + + public EntryIterableBuilder(IndexCursor cursor, Object... entryValues) { + _cursor = cursor; + _entryValues = entryValues; + } + + public Collection getColumnNames() { + return _columnNames; + } + + public ColumnMatcher getColumnMatcher() { + return _columnMatcher; + } + + public Object[] getEntryValues() { + return _entryValues; + } + + public EntryIterableBuilder setColumnNames(Collection columnNames) { + _columnNames = columnNames; + return this; + } + + public EntryIterableBuilder addColumnNames(Iterable columnNames) { + if(columnNames != null) { + for(String name : columnNames) { + addColumnName(name); + } + } + return this; + } + + public EntryIterableBuilder addColumns(Iterable cols) { + if(cols != null) { + for(Column col : cols) { + addColumnName(col.getName()); + } + } + return this; + } + + public EntryIterableBuilder addColumnNames(String... columnNames) { + if(columnNames != null) { + for(String name : columnNames) { + addColumnName(name); + } + } + return this; + } + + private void addColumnName(String columnName) { + if(_columnNames == null) { + _columnNames = new HashSet(); + } + _columnNames.add(columnName); + } + + public EntryIterableBuilder setEntryValues(Object... entryValues) { + _entryValues = entryValues; + return this; + } + + public EntryIterableBuilder setColumnMatcher(ColumnMatcher columnMatcher) { + _columnMatcher = columnMatcher; + return this; + } + + public Iterator iterator() { + return ((IndexCursorImpl)_cursor).entryIterator(this); + } +} diff --git a/src/java/com/healthmarketscience/jackcess/util/IterableBuilder.java b/src/java/com/healthmarketscience/jackcess/util/IterableBuilder.java new file mode 100644 index 0000000..089d8da --- /dev/null +++ b/src/java/com/healthmarketscience/jackcess/util/IterableBuilder.java @@ -0,0 +1,186 @@ +/* +Copyright (c) 2013 James Ahlborn + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +USA +*/ + +package com.healthmarketscience.jackcess.util; + +import java.util.AbstractMap; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; + +import com.healthmarketscience.jackcess.Column; +import com.healthmarketscience.jackcess.Cursor; +import com.healthmarketscience.jackcess.Row; +import com.healthmarketscience.jackcess.impl.CursorImpl; + +/** + * Builder style class for constructing a Cursor Iterable/Iterator. + * + * @author James Ahlborn + */ +public class IterableBuilder implements Iterable +{ + public enum Type { + SIMPLE, COLUMN_MATCH, ROW_MATCH; + } + + private final Cursor _cursor; + private Type _type = Type.SIMPLE; + private boolean _forward = true; + private boolean _reset = true; + private Collection _columnNames; + private ColumnMatcher _columnMatcher; + private Object _matchPattern; + + public IterableBuilder(Cursor cursor) { + _cursor = cursor; + } + + public Collection getColumnNames() { + return _columnNames; + } + + public ColumnMatcher getColumnMatcher() { + return _columnMatcher; + } + + public boolean isForward() { + return _forward; + } + + public boolean isReset() { + return _reset; + } + + /** + * @usage _advanced_method_ + */ + public Object getMatchPattern() { + return _matchPattern; + } + + /** + * @usage _advanced_method_ + */ + public Type getType() { + return _type; + } + + public IterableBuilder forward() { + return setForward(true); + } + + public IterableBuilder reverse() { + return setForward(false); + } + + public IterableBuilder setForward(boolean forward) { + _forward = forward; + return this; + } + + public IterableBuilder reset(boolean reset) { + _reset = reset; + return this; + } + + public IterableBuilder setColumnNames(Collection columnNames) { + _columnNames = columnNames; + return this; + } + + public IterableBuilder addColumnNames(Iterable columnNames) { + if(columnNames != null) { + for(String name : columnNames) { + addColumnName(name); + } + } + return this; + } + + public IterableBuilder addColumns(Iterable cols) { + if(cols != null) { + for(Column col : cols) { + addColumnName(col.getName()); + } + } + return this; + } + + public IterableBuilder addColumnNames(String... columnNames) { + if(columnNames != null) { + for(String name : columnNames) { + addColumnName(name); + } + } + return this; + } + + private void addColumnName(String columnName) { + if(_columnNames == null) { + _columnNames = new HashSet(); + } + _columnNames.add(columnName); + } + + public IterableBuilder setMatchPattern(Column columnPattern, + Object valuePattern) { + _type = Type.COLUMN_MATCH; + _matchPattern = new AbstractMap.SimpleImmutableEntry( + columnPattern, valuePattern); + return this; + } + + public IterableBuilder setMatchPattern(String columnNamePattern, + Object valuePattern) { + return setMatchPattern(_cursor.getTable().getColumn(columnNamePattern), + valuePattern); + } + + public IterableBuilder setMatchPattern(Map rowPattern) { + _type = Type.ROW_MATCH; + _matchPattern = rowPattern; + return this; + } + + public IterableBuilder addMatchPattern(String columnNamePattern, + Object valuePattern) + { + _type = Type.ROW_MATCH; + @SuppressWarnings("unchecked") + Map matchPattern = ((Map)_matchPattern); + if(matchPattern == null) { + matchPattern = new HashMap(); + _matchPattern = matchPattern; + } + matchPattern.put(columnNamePattern, valuePattern); + return this; + } + + public IterableBuilder setColumnMatcher(ColumnMatcher columnMatcher) { + _columnMatcher = columnMatcher; + return this; + } + + public Iterator iterator() { + return ((CursorImpl)_cursor).iterator(this); + } +}