From 65f4c8dff46a4d1f867190ad289216fad0e2230a Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 9 Jul 2009 20:02:00 +0000 Subject: [PATCH] This is possibly getting a little too jiggy, but I'm having all Depot methods that return a collection return an XList which extends List and adds the method: public Iterable map (Function mapper); which allows you to do things the following: return findAll(CategoryRecord.class, new Where(CategoryRecord.PARENT_ID.eq(parentId))). map(CategoryRecord.TO_CATEGORY); which is sufficiently awesome that I think it merits taking these liberties. Plus it opens the door to adding other transformations that one might want to do with a collection just loaded from the database. --- .../com/samskivert/depot/DepotRepository.java | 18 ++++----- src/java/com/samskivert/depot/XArrayList.java | 40 +++++++++++++++++++ src/java/com/samskivert/depot/XList.java | 38 ++++++++++++++++++ .../depot/impl/FindAllKeysQuery.java | 13 +++--- .../samskivert/depot/impl/FindAllQuery.java | 28 +++++++------ 5 files changed, 108 insertions(+), 29 deletions(-) create mode 100644 src/java/com/samskivert/depot/XArrayList.java create mode 100644 src/java/com/samskivert/depot/XList.java diff --git a/src/java/com/samskivert/depot/DepotRepository.java b/src/java/com/samskivert/depot/DepotRepository.java index 0e2e9da..84cf5ab 100644 --- a/src/java/com/samskivert/depot/DepotRepository.java +++ b/src/java/com/samskivert/depot/DepotRepository.java @@ -264,7 +264,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected List loadAll ( + protected XList loadAll ( Class type, Collection> primaryKeys) throws DatabaseException { @@ -282,10 +282,10 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected List loadAll (Collection> keys) + protected XList loadAll (Collection> keys) throws DatabaseException { - return (keys.size() == 0) ? Collections.emptyList() : + return (keys.size() == 0) ? new XArrayList() : _ctx.invoke(new FindAllQuery.WithKeys(_ctx, keys)); } @@ -294,7 +294,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected List findAll (Class type, QueryClause... clauses) + protected XList findAll (Class type, QueryClause... clauses) throws DatabaseException { return findAll(type, Arrays.asList(clauses)); @@ -313,7 +313,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected List findAll ( + protected XList findAll ( Class type, Collection clauses) throws DatabaseException { @@ -325,7 +325,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected List findAll ( + protected XList findAll ( Class type, CacheStrategy strategy, QueryClause... clauses) throws DatabaseException { @@ -337,7 +337,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected List findAll ( + protected XList findAll ( Class type, CacheStrategy cache, Collection clauses) throws DatabaseException { @@ -395,7 +395,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected List> findAllKeys ( + protected XList> findAllKeys ( Class type, boolean forUpdate, QueryClause... clause) throws DatabaseException { @@ -413,7 +413,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected List> findAllKeys ( + protected XList> findAllKeys ( Class type, boolean forUpdate, Collection clauses) throws DatabaseException { diff --git a/src/java/com/samskivert/depot/XArrayList.java b/src/java/com/samskivert/depot/XArrayList.java new file mode 100644 index 0000000..f6b148f --- /dev/null +++ b/src/java/com/samskivert/depot/XArrayList.java @@ -0,0 +1,40 @@ +// +// $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; + +import java.util.ArrayList; + +import com.google.common.base.Function; +import com.google.common.collect.Iterables; + +/** + * An array list specialization that implements {@link XList}. Depot returns this list from all of + * its methods to make it easy for callers to map the results to runtime records as desired. + */ +public class XArrayList extends ArrayList + implements XList +{ + // from interface XList + public Iterable map (Function mapper) + { + return Iterables.transform(this, mapper); + } +} diff --git a/src/java/com/samskivert/depot/XList.java b/src/java/com/samskivert/depot/XList.java new file mode 100644 index 0000000..8fa9ef2 --- /dev/null +++ b/src/java/com/samskivert/depot/XList.java @@ -0,0 +1,38 @@ +// +// $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; + +import java.util.List; + +import com.google.common.base.Function; + +/** + * Extends the {@link List} interface with a method {@link #map} that makes it easy to convert the + * contents of the list to an {@link Iterable} of a different type via the application of a {@link + * Function}. + */ +public interface XList extends List +{ + /** + * Returns an iterable over a mapping of this list via the specified mapping function. + */ + public Iterable map (Function mapper); +} diff --git a/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java b/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java index 9b02a9b..65dd5a8 100644 --- a/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java +++ b/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java @@ -21,15 +21,12 @@ package com.samskivert.depot.impl; import java.util.Collection; -import java.util.List; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import com.google.common.collect.Lists; - import com.samskivert.jdbc.DatabaseLiaison; import com.samskivert.jdbc.JDBCUtil; @@ -38,6 +35,8 @@ import com.samskivert.depot.Key; import com.samskivert.depot.PersistenceContext; import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.Stats; +import com.samskivert.depot.XArrayList; +import com.samskivert.depot.XList; import com.samskivert.depot.clause.QueryClause; import com.samskivert.depot.clause.SelectClause; @@ -46,7 +45,7 @@ import static com.samskivert.depot.Log.log; /** * Loads all primary keys for the records matching the supplied clause. */ -public class FindAllKeysQuery extends Query>> +public class FindAllKeysQuery extends Query>> { public FindAllKeysQuery (PersistenceContext ctx, Class type, boolean forUpdate, Collection clauses) @@ -66,16 +65,16 @@ public class FindAllKeysQuery extends Query> getCachedResult (PersistenceContext ctx) + public XList> getCachedResult (PersistenceContext ctx) { return null; // TODO } // from Query - public List> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) + public XList> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { - List> keys = Lists.newArrayList(); + XList> keys = new XArrayList>(); PreparedStatement stmt = _builder.prepare(conn); try { ResultSet rs = stmt.executeQuery(); diff --git a/src/java/com/samskivert/depot/impl/FindAllQuery.java b/src/java/com/samskivert/depot/impl/FindAllQuery.java index 70c4343..7b26222 100644 --- a/src/java/com/samskivert/depot/impl/FindAllQuery.java +++ b/src/java/com/samskivert/depot/impl/FindAllQuery.java @@ -48,6 +48,8 @@ import com.samskivert.depot.PersistenceContext; import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.SimpleCacheKey; import com.samskivert.depot.Stats; +import com.samskivert.depot.XArrayList; +import com.samskivert.depot.XList; import com.samskivert.depot.clause.FieldOverride; import com.samskivert.depot.clause.QueryClause; import com.samskivert.depot.clause.SelectClause; @@ -59,7 +61,7 @@ import static com.samskivert.depot.Log.log; * This class implements the functionality required by {@link DepotRepository#findAll}: fetch * a collection of persistent objects using one of two included strategies. */ -public abstract class FindAllQuery extends Query> +public abstract class FindAllQuery extends Query> { /** * The two-pass collection query implementation. See {@link DepotRepository#findAll} for @@ -103,7 +105,7 @@ public abstract class FindAllQuery extends Query getCachedResult (PersistenceContext ctx) + public XList getCachedResult (PersistenceContext ctx) { if (_qkey == null) { return null; @@ -118,7 +120,7 @@ public abstract class FindAllQuery extends Query invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) + public XList invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { // we want this to remain null if our key set came from the cache @@ -179,7 +181,7 @@ public abstract class FindAllQuery extends Query getCachedResult (PersistenceContext ctx) + public XList getCachedResult (PersistenceContext ctx) { // look up what we can from the cache _fetchKeys = loadFromCache(ctx, _keys, _entities); @@ -189,7 +191,7 @@ public abstract class FindAllQuery extends Query invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) + public XList invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { return loadAndResolve(ctx, conn, _keys, _fetchKeys, _entities, null); @@ -222,7 +224,7 @@ public abstract class FindAllQuery extends Query getCachedResult (PersistenceContext ctx) + public XList getCachedResult (PersistenceContext ctx) { if (_qkey != null) { _cachedQueries++; @@ -232,10 +234,10 @@ public abstract class FindAllQuery extends Query invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) + public XList invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { - List result = Lists.newArrayList(); + XList result = new XArrayList(); SQLBuilder builder = ctx.getSQLBuilder(DepotTypes.getDepotTypes(ctx, _select)); builder.newQuery(_select); PreparedStatement stmt = builder.prepare(conn); @@ -297,9 +299,9 @@ public abstract class FindAllQuery extends Query resolve (Iterable> allKeys, Map, T> entities) + protected XList resolve (Iterable> allKeys, Map, T> entities) { - List result = Lists.newArrayList(); + XList result = new XArrayList(); for (Key key : allKeys) { T value = entities.get(key); if (value != null) { @@ -309,9 +311,9 @@ public abstract class FindAllQuery extends Query loadAndResolve (PersistenceContext ctx, Connection conn, - Iterable> allKeys, Set> fetchKeys, - Map, T> entities, String origStmt) + protected XList loadAndResolve (PersistenceContext ctx, Connection conn, + Iterable> allKeys, Set> fetchKeys, + Map, T> entities, String origStmt) throws SQLException { if (PersistenceContext.CACHE_DEBUG && fetchKeys.size() > 0) {