XList is now an ex-List.

This commit is contained in:
Ray Greenwell
2010-02-25 06:12:22 +00:00
parent e972ebc72b
commit 503f3893cb
6 changed files with 60 additions and 127 deletions
@@ -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 <T extends PersistentRecord> XList<T> loadAll (
public <T extends PersistentRecord> List<T> loadAll (
Class<T> type, Collection<? extends Comparable<?>> primaryKeys)
throws DatabaseException
{
// convert the raw keys into real key records
DepotMarshaller<T> marsh = _ctx.getMarshaller(type);
List<Key<T>> 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 <T extends PersistentRecord> XList<T> loadAll (Collection<Key<T>> keys)
public <T extends PersistentRecord> List<T> loadAll (Collection<Key<T>> keys)
throws DatabaseException
{
return (keys.size() == 0) ? new XArrayList<T>() :
return keys.isEmpty() ? Collections.emptyList() :
_ctx.invoke(new FindAllQuery.WithKeys<T>(_ctx, keys));
}
@@ -209,7 +206,7 @@ public abstract class DepotRepository
*
* @throws DatabaseException if any problem is encountered communicating with the database.
*/
public <T extends PersistentRecord> XList<T> findAll (Class<T> type, QueryClause... clauses)
public <T extends PersistentRecord> List<T> findAll (Class<T> 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 <T extends PersistentRecord> XList<T> findAll (
public <T extends PersistentRecord> List<T> findAll (
Class<T> type, Collection<? extends QueryClause> clauses)
throws DatabaseException
{
@@ -240,7 +237,7 @@ public abstract class DepotRepository
*
* @throws DatabaseException if any problem is encountered communicating with the database.
*/
public <T extends PersistentRecord> XList<T> findAll (
public <T extends PersistentRecord> List<T> findAll (
Class<T> 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 <T extends PersistentRecord> XList<T> findAll (
public <T extends PersistentRecord> List<T> findAll (
Class<T> type, CacheStrategy cache, Collection<? extends QueryClause> clauses)
throws DatabaseException
{
@@ -314,7 +311,7 @@ public abstract class DepotRepository
*
* @throws DatabaseException if any problem is encountered communicating with the database.
*/
public <T extends PersistentRecord> XList<Key<T>> findAllKeys (
public <T extends PersistentRecord> List<Key<T>> findAllKeys (
Class<T> 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 <T extends PersistentRecord> XList<Key<T>> findAllKeys (
public <T extends PersistentRecord> List<Key<T>> findAllKeys (
Class<T> type, boolean forUpdate, Collection<? extends QueryClause> clauses)
throws DatabaseException
{
@@ -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<T> extends ArrayList<T>
implements XList<T>
{
@Deprecated // from interface XList<T>
public <R> Collection<R> map (Function<? super T, ? extends R> mapper)
{
return Lists.transform(this, mapper);
}
}
-43
View File
@@ -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<T> extends List<T>
{
/**
* @deprecated Use {@link DepotRepository#map} and {@link Sequence}.
*/
@Deprecated
public <R> Collection<R> map (Function<? super T, ? extends R> mapper);
}
@@ -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<T extends PersistentRecord>
*/
public Key<T> 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<T extends PersistentRecord>
*/
public Key<T> makePrimaryKey (Comparable<?> value)
{
if (!hasPrimaryKey()) {
throw new UnsupportedOperationException(
getClass().getName() + " does not define a primary key");
}
checkHasPrimaryKey();
return new Key<T>(_pClass, new Comparable<?>[] { value });
}
/**
* Creates a Function that changes Comparables into primary keys.
*/
public Function<Comparable<?>, Key<T>> primaryKeyFunction ()
{
checkHasPrimaryKey();
return new Function<Comparable<?>, Key<T>>() {
public Key<T> apply (Comparable<?> value) {
return new Key<T>(_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<T extends PersistentRecord>
}
}
/**
* 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;
@@ -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<T extends PersistentRecord> extends Query<XList<Key<T>>>
public class FindAllKeysQuery<T extends PersistentRecord> extends Query<List<Key<T>>>
{
public FindAllKeysQuery (PersistenceContext ctx, Class<T> type, boolean forUpdate,
Collection<? extends QueryClause> clauses)
@@ -63,16 +63,16 @@ public class FindAllKeysQuery<T extends PersistentRecord> extends Query<XList<Ke
}
@Override // from Query
public XList<Key<T>> getCachedResult (PersistenceContext ctx)
public List<Key<T>> getCachedResult (PersistenceContext ctx)
{
return null; // TODO
}
// from Query
public XList<Key<T>> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison)
public List<Key<T>> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison)
throws SQLException
{
XList<Key<T>> keys = new XArrayList<Key<T>>();
List<Key<T>> keys = new ArrayList<Key<T>>();
PreparedStatement stmt = _builder.prepare(conn);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
@@ -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<T extends PersistentRecord> extends Query<XList<T>>
public abstract class FindAllQuery<T extends PersistentRecord> extends Query<List<T>>
{
/**
* The two-pass collection query implementation. See {@link DepotRepository#findAll} for
@@ -102,7 +101,7 @@ public abstract class FindAllQuery<T extends PersistentRecord> extends Query<XLi
}
@Override // from Query
public XList<T> getCachedResult (PersistenceContext ctx)
public List<T> getCachedResult (PersistenceContext ctx)
{
if (_qkey == null) {
return null;
@@ -117,7 +116,7 @@ public abstract class FindAllQuery<T extends PersistentRecord> extends Query<XLi
}
// from Query
public XList<T> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison)
public List<T> 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<T extends PersistentRecord> extends Query<XLi
}
@Override // from Query
public XList<T> getCachedResult (PersistenceContext ctx)
public List<T> getCachedResult (PersistenceContext ctx)
{
// look up what we can from the cache
_fetchKeys = loadFromCache(ctx, _keys, _entities);
@@ -184,7 +183,7 @@ public abstract class FindAllQuery<T extends PersistentRecord> extends Query<XLi
}
// from Query
public XList<T> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison)
public List<T> 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<T extends PersistentRecord> extends Query<XLi
}
@Override // from Query
public XList<T> getCachedResult (PersistenceContext ctx)
public List<T> getCachedResult (PersistenceContext ctx)
{
if (_qkey != null) {
_cachedQueries++;
@@ -227,10 +226,10 @@ public abstract class FindAllQuery<T extends PersistentRecord> extends Query<XLi
}
// from Query
public XList<T> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison)
public List<T> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison)
throws SQLException
{
XList<T> result = new XArrayList<T>();
List<T> result = new ArrayList<T>();
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<T extends PersistentRecord> extends Query<XLi
return fetchKeys;
}
protected XList<T> resolve (Iterable<Key<T>> allKeys, Map<Key<T>, T> entities)
protected List<T> resolve (Iterable<Key<T>> allKeys, Map<Key<T>, T> entities)
{
XList<T> result = new XArrayList<T>();
List<T> result = new ArrayList<T>();
for (Key<T> key : allKeys) {
T value = entities.get(key);
if (value != null) {
@@ -299,7 +298,7 @@ public abstract class FindAllQuery<T extends PersistentRecord> extends Query<XLi
return result;
}
protected XList<T> loadAndResolve (PersistenceContext ctx, Connection conn,
protected List<T> loadAndResolve (PersistenceContext ctx, Connection conn,
Iterable<Key<T>> allKeys, Set<Key<T>> fetchKeys,
Map<Key<T>, T> entities, String origStmt)
throws SQLException