From b022382231e73d8ba7f9b090195eee751ed3a8cd Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 23 Feb 2010 08:35:30 +0000 Subject: [PATCH] XList is going away. To be replaced with this, still somewhat out of place in a database library but at least slightly more ignorable, bit of functional helpfulness. --- .../com/samskivert/depot/DepotRepository.java | 14 +++- .../com/samskivert/depot/util/Sequence.java | 83 +++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/java/com/samskivert/depot/util/Sequence.java diff --git a/src/java/com/samskivert/depot/DepotRepository.java b/src/java/com/samskivert/depot/DepotRepository.java index cd10210..4884161 100644 --- a/src/java/com/samskivert/depot/DepotRepository.java +++ b/src/java/com/samskivert/depot/DepotRepository.java @@ -31,19 +31,21 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.google.common.base.Function; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.samskivert.util.ArrayUtil; -import com.samskivert.jdbc.ConnectionProvider; -import com.samskivert.jdbc.DatabaseLiaison; import com.samskivert.depot.clause.FieldOverride; import com.samskivert.depot.clause.InsertClause; import com.samskivert.depot.clause.QueryClause; import com.samskivert.depot.clause.WhereClause; import com.samskivert.depot.expression.ColumnExp; import com.samskivert.depot.expression.SQLExpression; +import com.samskivert.depot.util.Sequence; +import com.samskivert.jdbc.ConnectionProvider; +import com.samskivert.jdbc.DatabaseLiaison; import com.samskivert.depot.impl.DepotMarshaller; import com.samskivert.depot.impl.DepotMigrationHistoryRecord; @@ -905,6 +907,14 @@ public abstract class DepotRepository return (value instanceof SQLExpression) ? (SQLExpression)value : new ValueExp(value); } + /** + * Concise way to call {@link Sequence#map} to transform query results. + */ + protected Sequence map (Collection source, Function func) + { + return Sequence.map(source, func); + } + protected PersistenceContext _ctx; protected List _dataMigs = Lists.newArrayList(); } diff --git a/src/java/com/samskivert/depot/util/Sequence.java b/src/java/com/samskivert/depot/util/Sequence.java new file mode 100644 index 0000000..e70290a --- /dev/null +++ b/src/java/com/samskivert/depot/util/Sequence.java @@ -0,0 +1,83 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2009 Michael Bayne and Pär Winzell +// +// 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.samskivert.depot.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +import com.google.common.base.Function; +import com.google.common.collect.Iterators; + +/** + * Provides read-only access to a sequence of elements. Note that these elements will generally be + * computed via the application of a {@link Function} *every time* you iterate over this sequence. + * Thus a caller is expected to either simply iterate over the elements once, or use {@link + * #toList} to convert the sequence to concrete list to avoid repeated application of the + * conversion function on elements of the sequence. + */ +public abstract class Sequence implements Iterable +{ + /** + * Applies the supplied mapping function to the elements in the supplied collection, returning + * a lazily created read-only view of the mapped elements. + */ + public static Sequence map (final Collection source, + final Function func) + { + return new Sequence() { + public Iterator iterator () { + return Iterators.transform(source.iterator(), func); + } + public int size () { + return source.size(); + } + public boolean isEmpty () { + return source.isEmpty(); + } + public ArrayList toList () { + ArrayList list = new ArrayList(source.size()); + for (F elem : source) { + list.add(func.apply(elem)); + } + return list; + } + }; + } + + // from interface Iterable + public abstract Iterator iterator (); + + /** + * Returns the number of elements in this sequence. + */ + public abstract int size (); + + /** + * Returns true if this sequence is empty, false if it contains at least one element. + */ + public abstract boolean isEmpty (); + + /** + * Converts this sequence into an array list. + */ + public abstract ArrayList toList (); +}