Skip to content

Commit

Permalink
Repository and template now return Lists.
Browse files Browse the repository at this point in the history
Closes #1623
  • Loading branch information
schauder committed Sep 25, 2024
1 parent ad5890b commit dd4da8a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.springframework.data.relational.core.mapping.event.*;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -171,7 +172,7 @@ public <T> T save(T instance) {
}

@Override
public <T> Iterable<T> saveAll(Iterable<T> instances) {
public <T> List<T> saveAll(Iterable<T> instances) {

Assert.notNull(instances, "Aggregate instances must not be null");

Expand Down Expand Up @@ -204,7 +205,7 @@ public <T> T insert(T instance) {
}

@Override
public <T> Iterable<T> insertAll(Iterable<T> instances) {
public <T> List<T> insertAll(Iterable<T> instances) {

Assert.notNull(instances, "Aggregate instances must not be null");

Expand Down Expand Up @@ -239,7 +240,7 @@ public <T> T update(T instance) {
}

@Override
public <T> Iterable<T> updateAll(Iterable<T> instances) {
public <T> List<T> updateAll(Iterable<T> instances) {

Assert.notNull(instances, "Aggregate instances must not be null");

Expand Down Expand Up @@ -298,7 +299,7 @@ public <T> T findById(Object id, Class<T> domainType) {
}

@Override
public <T> Iterable<T> findAll(Class<T> domainType, Sort sort) {
public <T> List<T> findAll(Class<T> domainType, Sort sort) {

Assert.notNull(domainType, "Domain type must not be null");

Expand All @@ -323,8 +324,13 @@ public <T> Optional<T> findOne(Query query, Class<T> domainType) {
}

@Override
public <T> Iterable<T> findAll(Query query, Class<T> domainType) {
return accessStrategy.findAll(query, domainType);
public <T> List<T> findAll(Query query, Class<T> domainType) {

Iterable<T> all = accessStrategy.findAll(query, domainType);
if (all instanceof List<T> list) {
return list;
}
return Streamable.of(all).toList();
}

@Override
Expand All @@ -337,7 +343,7 @@ public <T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable)
}

@Override
public <T> Iterable<T> findAll(Class<T> domainType) {
public <T> List<T> findAll(Class<T> domainType) {

Assert.notNull(domainType, "Domain type must not be null");

Expand All @@ -346,7 +352,7 @@ public <T> Iterable<T> findAll(Class<T> domainType) {
}

@Override
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
public <T> List<T> findAllById(Iterable<?> ids, Class<T> domainType) {

Assert.notNull(ids, "Ids must not be null");
Assert.notNull(domainType, "Domain type must not be null");
Expand Down Expand Up @@ -607,7 +613,7 @@ private MutableAggregateChange<?> createDeletingChange(Class<?> domainType) {
return aggregateChange;
}

private <T> Iterable<T> triggerAfterConvert(Iterable<T> all) {
private <T> List<T> triggerAfterConvert(Iterable<T> all) {

List<T> result = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.jdbc.repository.support;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

Expand All @@ -30,6 +31,7 @@
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.data.util.Streamable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -70,8 +72,8 @@ public <S extends T> S save(S instance) {

@Transactional
@Override
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
return entityOperations.saveAll(entities);
public <S extends T> List<S> saveAll(Iterable<S> entities) {
return asList(entityOperations.saveAll(entities));
}

@Override
Expand All @@ -85,13 +87,13 @@ public boolean existsById(ID id) {
}

@Override
public Iterable<T> findAll() {
return entityOperations.findAll(entity.getType());
public List<T> findAll() {
return asList(entityOperations.findAll(entity.getType()));
}

@Override
public Iterable<T> findAllById(Iterable<ID> ids) {
return entityOperations.findAllById(ids, entity.getType());
public List<T> findAllById(Iterable<ID> ids) {
return asList(entityOperations.findAllById(ids, entity.getType()));
}

@Override
Expand Down Expand Up @@ -130,8 +132,8 @@ public void deleteAll() {
}

@Override
public Iterable<T> findAll(Sort sort) {
return entityOperations.findAll(entity.getType(), sort);
public List<T> findAll(Sort sort) {
return asList(entityOperations.findAll(entity.getType(), sort));
}

@Override
Expand All @@ -148,21 +150,21 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
}

@Override
public <S extends T> Iterable<S> findAll(Example<S> example) {
public <S extends T> List<S> findAll(Example<S> example) {

Assert.notNull(example, "Example must not be null");

return findAll(example, Sort.unsorted());
}

@Override
public <S extends T> Iterable<S> findAll(Example<S> example, Sort sort) {
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {

Assert.notNull(example, "Example must not be null");
Assert.notNull(sort, "Sort must not be null");

return this.entityOperations.findAll(this.exampleMapper.getMappedExample(example).sort(sort),
example.getProbeType());
return asList(this.entityOperations.findAll(this.exampleMapper.getMappedExample(example).sort(sort),
example.getProbeType()));
}

@Override
Expand Down Expand Up @@ -200,4 +202,14 @@ public <S extends T, R> R findBy(Example<S> example, Function<FluentQuery.Fetcha

return queryFunction.apply(fluentQuery);
}


private <S extends T> List<S> asList(Iterable<S> iterable) {

if (iterable instanceof List<S> list) {
return list;
}
return Streamable.of(iterable).stream().toList();
}

}

0 comments on commit dd4da8a

Please sign in to comment.