diff --git a/src/java/com/samskivert/depot/DepotRepository.java b/src/java/com/samskivert/depot/DepotRepository.java index 4884161..4c1e359 100644 --- a/src/java/com/samskivert/depot/DepotRepository.java +++ b/src/java/com/samskivert/depot/DepotRepository.java @@ -32,6 +32,7 @@ import java.util.Map; import java.util.Set; import com.google.common.base.Function; +import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -179,17 +180,13 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - public XList loadAll ( + public List loadAll ( Class type, Collection> primaryKeys) throws DatabaseException { // convert the raw keys into real key records - DepotMarshaller marsh = _ctx.getMarshaller(type); - List> keys = Lists.newArrayList(); - for (Comparable key : primaryKeys) { - keys.add(marsh.makePrimaryKey(key)); - } - return loadAll(keys); + return loadAll( + Collections2.transform(primaryKeys, _ctx.getMarshaller(type).primaryKeyFunction())); } /** @@ -197,10 +194,10 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - public XList loadAll (Collection> keys) + public List loadAll (Collection> keys) throws DatabaseException { - return (keys.size() == 0) ? new XArrayList() : + return keys.isEmpty() ? Collections.emptyList() : _ctx.invoke(new FindAllQuery.WithKeys(_ctx, keys)); } @@ -209,7 +206,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - public XList findAll (Class type, QueryClause... clauses) + public List findAll (Class type, QueryClause... clauses) throws DatabaseException { return findAll(type, Arrays.asList(clauses)); @@ -228,7 +225,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - public XList findAll ( + public List findAll ( Class type, Collection clauses) throws DatabaseException { @@ -240,7 +237,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - public XList findAll ( + public List findAll ( Class type, CacheStrategy strategy, QueryClause... clauses) throws DatabaseException { @@ -252,7 +249,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - public XList findAll ( + public List findAll ( Class type, CacheStrategy cache, Collection clauses) throws DatabaseException { @@ -314,7 +311,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - public XList> findAllKeys ( + public List> findAllKeys ( Class type, boolean forUpdate, QueryClause... clause) throws DatabaseException { @@ -332,7 +329,7 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - public XList> findAllKeys ( + public List> 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 deleted file mode 100644 index 3961721..0000000 --- a/src/java/com/samskivert/depot/XArrayList.java +++ /dev/null @@ -1,41 +0,0 @@ -// -// $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 java.util.Collection; - -import com.google.common.base.Function; -import com.google.common.collect.Lists; - -/** - * 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 -{ - @Deprecated // from interface XList - public Collection map (Function mapper) - { - return Lists.transform(this, mapper); - } -} diff --git a/src/java/com/samskivert/depot/XList.java b/src/java/com/samskivert/depot/XList.java deleted file mode 100644 index 18f0f05..0000000 --- a/src/java/com/samskivert/depot/XList.java +++ /dev/null @@ -1,43 +0,0 @@ -// -// $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 java.util.Collection; -import java.util.List; - -import com.google.common.base.Function; - -import com.samskivert.depot.util.Sequence; - -/** - * Extends the {@link List} interface with a method {@link #map} that makes it easy to convert the - * contents of the list to an ordered {@link Collection} of a different type via the application of - * a {@link Function}. - */ -public interface XList extends List -{ - /** - * @deprecated Use {@link DepotRepository#map} and {@link Sequence}. - */ - @Deprecated - public Collection map (Function mapper); -} diff --git a/src/java/com/samskivert/depot/impl/DepotMarshaller.java b/src/java/com/samskivert/depot/impl/DepotMarshaller.java index bb2b8ca..3a53e86 100644 --- a/src/java/com/samskivert/depot/impl/DepotMarshaller.java +++ b/src/java/com/samskivert/depot/impl/DepotMarshaller.java @@ -33,6 +33,7 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; +import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; @@ -397,11 +398,10 @@ public class DepotMarshaller */ public Key getPrimaryKey (Object object, boolean requireKey) { - if (!hasPrimaryKey()) { - if (requireKey) { - throw new UnsupportedOperationException( - _pClass.getName() + " does not define a primary key"); - } + if (requireKey) { + checkHasPrimaryKey(); + + } else if (!hasPrimaryKey()) { return null; } @@ -453,13 +453,23 @@ public class DepotMarshaller */ public Key makePrimaryKey (Comparable value) { - if (!hasPrimaryKey()) { - throw new UnsupportedOperationException( - getClass().getName() + " does not define a primary key"); - } + checkHasPrimaryKey(); return new Key(_pClass, new Comparable[] { value }); } + /** + * Creates a Function that changes Comparables into primary keys. + */ + public Function, Key> primaryKeyFunction () + { + checkHasPrimaryKey(); + return new Function, Key>() { + public Key apply (Comparable value) { + return new Key(_pClass, new Comparable[] { value }); + } + }; + } + /** * Creates a primary key record for the type of object handled by this marshaller, using the * supplied result set. @@ -992,6 +1002,17 @@ public class DepotMarshaller } } + /** + * Check to see if we have a primary key, otherwise throw an UnsupportedOperationException. + */ + protected void checkHasPrimaryKey () + { + if (!hasPrimaryKey()) { + throw new UnsupportedOperationException( + getClass().getName() + " does not define a primary key"); + } + } + protected static class TableMetaData { public boolean tableExists; diff --git a/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java b/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java index cca5aba..c64cf42 100644 --- a/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java +++ b/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java @@ -20,7 +20,9 @@ package com.samskivert.depot.impl; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.sql.Connection; import java.sql.PreparedStatement; @@ -33,8 +35,6 @@ 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; @@ -43,7 +43,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) @@ -63,16 +63,16 @@ public class FindAllKeysQuery extends Query> getCachedResult (PersistenceContext ctx) + public List> getCachedResult (PersistenceContext ctx) { return null; // TODO } // from Query - public XList> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) + public List> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { - XList> keys = new XArrayList>(); + List> keys = new ArrayList>(); PreparedStatement stmt = _builder.prepare(conn); ResultSet rs = stmt.executeQuery(); while (rs.next()) { diff --git a/src/java/com/samskivert/depot/impl/FindAllQuery.java b/src/java/com/samskivert/depot/impl/FindAllQuery.java index 81a44a1..d968c2c 100644 --- a/src/java/com/samskivert/depot/impl/FindAllQuery.java +++ b/src/java/com/samskivert/depot/impl/FindAllQuery.java @@ -25,6 +25,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -45,8 +46,6 @@ import com.samskivert.depot.KeySet; 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.FieldOverride; import com.samskivert.depot.clause.QueryClause; import com.samskivert.depot.clause.SelectClause; @@ -58,7 +57,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 @@ -102,7 +101,7 @@ public abstract class FindAllQuery extends Query getCachedResult (PersistenceContext ctx) + public List getCachedResult (PersistenceContext ctx) { if (_qkey == null) { return null; @@ -117,7 +116,7 @@ public abstract class FindAllQuery extends Query invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) + public List invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { // we want this to remain null if our key set came from the cache @@ -174,7 +173,7 @@ public abstract class FindAllQuery extends Query getCachedResult (PersistenceContext ctx) + public List getCachedResult (PersistenceContext ctx) { // look up what we can from the cache _fetchKeys = loadFromCache(ctx, _keys, _entities); @@ -184,7 +183,7 @@ public abstract class FindAllQuery extends Query invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) + public List invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { return loadAndResolve(ctx, conn, _keys, _fetchKeys, _entities, null); @@ -217,7 +216,7 @@ public abstract class FindAllQuery extends Query getCachedResult (PersistenceContext ctx) + public List getCachedResult (PersistenceContext ctx) { if (_qkey != null) { _cachedQueries++; @@ -227,10 +226,10 @@ public abstract class FindAllQuery extends Query invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) + public List invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { - XList result = new XArrayList(); + List result = new ArrayList(); SQLBuilder builder = ctx.getSQLBuilder(DepotTypes.getDepotTypes(ctx, _select)); builder.newQuery(_select); ResultSet rs = builder.prepare(conn).executeQuery(); @@ -287,9 +286,9 @@ public abstract class FindAllQuery extends Query resolve (Iterable> allKeys, Map, T> entities) + protected List resolve (Iterable> allKeys, Map, T> entities) { - XList result = new XArrayList(); + List result = new ArrayList(); for (Key key : allKeys) { T value = entities.get(key); if (value != null) { @@ -299,7 +298,7 @@ public abstract class FindAllQuery extends Query loadAndResolve (PersistenceContext ctx, Connection conn, + protected List loadAndResolve (PersistenceContext ctx, Connection conn, Iterable> allKeys, Set> fetchKeys, Map, T> entities, String origStmt) throws SQLException