rewrite using lambdas

This commit is contained in:
Athou
2014-12-12 10:06:23 +01:00
parent 2e475c35cc
commit 504e4eab3e
15 changed files with 44 additions and 150 deletions

View File

@@ -2,6 +2,7 @@ package com.commafeed.backend.dao;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -12,7 +13,6 @@ import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.QFeedCategory;
import com.commafeed.backend.model.QUser;
import com.commafeed.backend.model.User;
import com.google.common.collect.Lists;
import com.mysema.query.types.Predicate;
@Singleton
@@ -54,14 +54,7 @@ public class FeedCategoryDAO extends GenericDAO<FeedCategory> {
}
public List<FeedCategory> findAllChildrenCategories(User user, FeedCategory parent) {
List<FeedCategory> list = Lists.newArrayList();
List<FeedCategory> all = findAll(user);
for (FeedCategory cat : all) {
if (isChild(cat, parent)) {
list.add(cat);
}
}
return list;
return findAll(user).stream().filter(c -> isChild(c, parent)).collect(Collectors.toList());
}
private boolean isChild(FeedCategory child, FeedCategory parent) {

View File

@@ -1,6 +1,7 @@
package com.commafeed.backend.dao;
import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -15,7 +16,6 @@ import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.QFeedEntry;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.mysema.query.Tuple;
import com.mysema.query.types.expr.NumberExpression;
@@ -36,13 +36,9 @@ public class FeedEntryDAO extends GenericDAO<FeedEntry> {
}
public List<FeedCapacity> findFeedsExceedingCapacity(long maxCapacity, long max) {
List<FeedCapacity> list = Lists.newArrayList();
NumberExpression<Long> count = entry.id.countDistinct();
List<Tuple> tuples = newQuery().from(entry).groupBy(entry.feed).having(count.gt(maxCapacity)).limit(max).list(entry.feed.id, count);
for (Tuple tuple : tuples) {
list.add(new FeedCapacity(tuple.get(entry.feed.id), tuple.get(count)));
}
return list;
return tuples.stream().map(t -> new FeedCapacity(t.get(entry.feed.id), t.get(count))).collect(Collectors.toList());
}
public int deleteOldEntries(Long feedId, long max) {

View File

@@ -1,6 +1,8 @@
package com.commafeed.backend.dao;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -13,9 +15,7 @@ import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.Models;
import com.commafeed.backend.model.QFeedSubscription;
import com.commafeed.backend.model.User;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.mysema.query.jpa.hibernate.HibernateQuery;
@Singleton
@@ -60,26 +60,13 @@ public class FeedSubscriptionDAO extends GenericDAO<FeedSubscription> {
}
public List<FeedSubscription> findByCategories(User user, List<FeedCategory> categories) {
List<Long> categoryIds = Lists.transform(categories, new Function<FeedCategory, Long>() {
@Override
public Long apply(FeedCategory input) {
return input.getId();
}
});
List<FeedSubscription> subscriptions = Lists.newArrayList();
for (FeedSubscription sub : findAll(user)) {
if (sub.getCategory() != null && categoryIds.contains(sub.getCategory().getId())) {
subscriptions.add(sub);
}
}
return subscriptions;
Set<Long> categoryIds = categories.stream().map(c -> c.getId()).collect(Collectors.toSet());
return findAll(user).stream().filter(s -> s.getCategory() != null && categoryIds.contains(s.getCategory().getId()))
.collect(Collectors.toList());
}
private List<FeedSubscription> initRelations(List<FeedSubscription> list) {
for (FeedSubscription sub : list) {
initRelations(sub);
}
list.forEach(s -> initRelations(s));
return list;
}

View File

@@ -24,9 +24,7 @@ public abstract class GenericDAO<T extends AbstractModel> extends AbstractDAO<T>
}
public void saveOrUpdate(Collection<T> models) {
for (T model : models) {
persist(model);
}
models.forEach(m -> persist(m));
}
public void merge(T model) {
@@ -34,9 +32,7 @@ public abstract class GenericDAO<T extends AbstractModel> extends AbstractDAO<T>
}
public void merge(Collection<T> models) {
for (T model : models) {
merge(model);
}
models.forEach(m -> merge(m));
}
public T findById(Long id) {
@@ -50,9 +46,7 @@ public abstract class GenericDAO<T extends AbstractModel> extends AbstractDAO<T>
}
public int delete(Collection<T> objects) {
for (T object : objects) {
delete(object);
}
objects.forEach(o -> delete(o));
return objects.size();
}

View File

@@ -2,6 +2,7 @@ package com.commafeed.backend.dao;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -12,7 +13,6 @@ import com.commafeed.backend.model.QUserRole;
import com.commafeed.backend.model.User;
import com.commafeed.backend.model.UserRole;
import com.commafeed.backend.model.UserRole.Role;
import com.google.common.collect.Sets;
@Singleton
public class UserRoleDAO extends GenericDAO<UserRole> {
@@ -33,10 +33,6 @@ public class UserRoleDAO extends GenericDAO<UserRole> {
}
public Set<Role> findRoles(User user) {
Set<Role> list = Sets.newHashSet();
for (UserRole role : findAll(user)) {
list.add(role.getRole());
}
return list;
return findAll(user).stream().map(r -> r.getRole()).collect(Collectors.toSet());
}
}