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.
This commit is contained in:
Michael Bayne
2010-02-23 08:35:30 +00:00
parent 152ff056d9
commit b022382231
2 changed files with 95 additions and 2 deletions
@@ -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 <F, T> Sequence<T> map (Collection<F> source, Function<? super F, ? extends T> func)
{
return Sequence.map(source, func);
}
protected PersistenceContext _ctx;
protected List<DataMigration> _dataMigs = Lists.newArrayList();
}
@@ -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<T> implements Iterable<T>
{
/**
* 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 <F, T> Sequence<T> map (final Collection<F> source,
final Function<? super F, ? extends T> func)
{
return new Sequence<T>() {
public Iterator<T> iterator () {
return Iterators.transform(source.iterator(), func);
}
public int size () {
return source.size();
}
public boolean isEmpty () {
return source.isEmpty();
}
public ArrayList<T> toList () {
ArrayList<T> list = new ArrayList<T>(source.size());
for (F elem : source) {
list.add(func.apply(elem));
}
return list;
}
};
}
// from interface Iterable
public abstract Iterator<T> 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<T> toList ();
}