Skip to content

Commit

Permalink
JAMES-2107 Run IntelliJ inspection: "Move from imperative to Stream API"
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Aug 16, 2017
1 parent 855a3c8 commit cb0f621
Show file tree
Hide file tree
Showing 58 changed files with 350 additions and 457 deletions.
Expand Up @@ -86,19 +86,16 @@ public Set<PartContentDescriptor> getPartContentDescriptors() {
*/ */
public void addPartContent(MimePath path, int content) { public void addPartContent(MimePath path, int content) {
if (partContentDescriptors == null) { if (partContentDescriptors == null) {
partContentDescriptors = new HashSet<PartContentDescriptor>(); partContentDescriptors = new HashSet<>();
}
PartContentDescriptorImpl currentDescriptor = null;
for (PartContentDescriptor descriptor : partContentDescriptors) {
if (path.equals(descriptor.path())) {
currentDescriptor = (PartContentDescriptorImpl) descriptor;
break;
}
}
if (currentDescriptor == null) {
currentDescriptor = new PartContentDescriptorImpl(path);
partContentDescriptors.add(currentDescriptor);
} }
PartContentDescriptorImpl currentDescriptor = (PartContentDescriptorImpl) partContentDescriptors.stream()
.filter(descriptor -> path.equals(descriptor.path()))
.findFirst()
.orElseGet(() -> {
PartContentDescriptorImpl result = new PartContentDescriptorImpl(path);
partContentDescriptors.add(result);
return result;
});


currentDescriptor.or(content); currentDescriptor.or(content);
} }
Expand Down
Expand Up @@ -318,11 +318,7 @@ public long getHeaderOctets() {
* This implementation supports user flags * This implementation supports user flags
*/ */
public String[] createUserFlags() { public String[] createUserFlags() {
String[] flags = new String[userFlags.size()]; return userFlags.toArray(new String[userFlags.size()]);
for (int i = 0; i < userFlags.size(); i++) {
flags[i] = userFlags.get(i);
}
return flags;
} }


@Override @Override
Expand Down
Expand Up @@ -53,6 +53,8 @@
import org.apache.james.mailbox.store.search.comparator.UidComparator; import org.apache.james.mailbox.store.search.comparator.UidComparator;
import org.slf4j.Logger; import org.slf4j.Logger;


import com.github.steveash.guavate.Guavate;

public class JCRMailboxMessage implements MailboxMessage, JCRImapConstants, Persistent { public class JCRMailboxMessage implements MailboxMessage, JCRImapConstants, Persistent {


private static final Comparator<MailboxMessage> MESSAGE_UID_COMPARATOR = new UidComparator(); private static final Comparator<MailboxMessage> MESSAGE_UID_COMPARATOR = new UidComparator();
Expand Down Expand Up @@ -304,10 +306,9 @@ public void merge(Node node) throws RepositoryException, IOException {




List<Property> currentProperties = getProperties(); List<Property> currentProperties = getProperties();
List<Property> newProperties = new ArrayList<Property>(); List<Property> newProperties = currentProperties.stream()
for (Property prop : currentProperties) { .map(prop -> new JCRProperty(prop, logger))
newProperties.add(new JCRProperty(prop, logger)); .collect(Guavate.toImmutableList());
}
// remove old properties, we will add a bunch of new ones // remove old properties, we will add a bunch of new ones
NodeIterator iterator = node.getNodes("messageProperty"); NodeIterator iterator = node.getNodes("messageProperty");
while (iterator.hasNext()) { while (iterator.hasNext()) {
Expand Down
Expand Up @@ -463,11 +463,9 @@ public Flags createFlags() {
} }


protected String[] createUserFlags() { protected String[] createUserFlags() {
String[] flags = new String[userFlags.size()]; return userFlags.stream()
for (int i = 0; i < userFlags.size(); i++) { .map(JPAUserFlag::getName)
flags[i] = userFlags.get(i).getName(); .toArray(String[]::new);
}
return flags;
} }


/** /**
Expand Down
Expand Up @@ -50,10 +50,8 @@ public UidConstraint between(long lower, long upper) {
} }


public boolean isAllowed(long uid) { public boolean isAllowed(long uid) {
for (Constraint constraint : constraints) return constraints.stream()
if (!constraint.isAllowed(uid)) .allMatch(constraint -> constraint.isAllowed(uid));
return false;
return true;
} }


public abstract static class Constraint { public abstract static class Constraint {
Expand Down
Expand Up @@ -26,7 +26,6 @@
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;


Expand All @@ -37,6 +36,10 @@
import org.apache.james.mailbox.store.user.model.Subscription; import org.apache.james.mailbox.store.user.model.Subscription;
import org.apache.james.mailbox.store.user.model.impl.SimpleSubscription; import org.apache.james.mailbox.store.user.model.impl.SimpleSubscription;


import com.github.steveash.guavate.Guavate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

public class MaildirSubscriptionMapper extends NonTransactionalMapper implements SubscriptionMapper { public class MaildirSubscriptionMapper extends NonTransactionalMapper implements SubscriptionMapper {


private static final String FILE_SUBSCRIPTION = "subscriptions"; private static final String FILE_SUBSCRIPTION = "subscriptions";
Expand All @@ -53,10 +56,11 @@ public MaildirSubscriptionMapper(MaildirStore store) {
public void delete(Subscription subscription) throws SubscriptionException { public void delete(Subscription subscription) throws SubscriptionException {
// TODO: we need some kind of file locking here // TODO: we need some kind of file locking here
Set<String> subscriptionNames = readSubscriptionsForUser(subscription.getUser()); Set<String> subscriptionNames = readSubscriptionsForUser(subscription.getUser());
boolean changed = subscriptionNames.remove(subscription.getMailbox()); Set<String> newSubscriptions = Sets.difference(subscriptionNames, ImmutableSet.of(subscription.getMailbox()));
boolean changed = subscriptionNames.size() != newSubscriptions.size();
if (changed) { if (changed) {
try { try {
writeSubscriptions(new File(store.userRoot(subscription.getUser())), subscriptionNames); writeSubscriptions(new File(store.userRoot(subscription.getUser())), newSubscriptions);
} catch (IOException e) { } catch (IOException e) {
throw new SubscriptionException(e); throw new SubscriptionException(e);
} }
Expand All @@ -69,11 +73,9 @@ public void delete(Subscription subscription) throws SubscriptionException {
@Override @Override
public List<Subscription> findSubscriptionsForUser(String user) throws SubscriptionException { public List<Subscription> findSubscriptionsForUser(String user) throws SubscriptionException {
Set<String> subscriptionNames = readSubscriptionsForUser(user); Set<String> subscriptionNames = readSubscriptionsForUser(user);
ArrayList<Subscription> subscriptions = new ArrayList<Subscription>(); return subscriptionNames.stream()
for (String subscription : subscriptionNames) { .map(subscription -> new SimpleSubscription(user, subscription))
subscriptions.add(new SimpleSubscription(user, subscription)); .collect(Guavate.toImmutableList());
}
return subscriptions;
} }


/** /**
Expand All @@ -100,10 +102,14 @@ public Subscription findMailboxSubscriptionForUser(String user, String mailbox)
public void save(Subscription subscription) throws SubscriptionException { public void save(Subscription subscription) throws SubscriptionException {
// TODO: we need some kind of file locking here // TODO: we need some kind of file locking here
Set<String> subscriptionNames = readSubscriptionsForUser(subscription.getUser()); Set<String> subscriptionNames = readSubscriptionsForUser(subscription.getUser());
boolean changed = subscriptionNames.add(subscription.getMailbox()); Set<String> newSubscriptions = ImmutableSet.<String>builder()
.addAll(subscriptionNames)
.add(subscription.getMailbox())
.build();
boolean changed = subscriptionNames.size() != newSubscriptions.size();
if (changed) { if (changed) {
try { try {
writeSubscriptions(new File(store.userRoot(subscription.getUser())), subscriptionNames); writeSubscriptions(new File(store.userRoot(subscription.getUser())), newSubscriptions);
} catch (IOException e) { } catch (IOException e) {
throw new SubscriptionException(e); throw new SubscriptionException(e);
} }
Expand All @@ -127,13 +133,11 @@ public void endRequest() {
*/ */
private Set<String> readSubscriptionsForUser(String user) throws SubscriptionException { private Set<String> readSubscriptionsForUser(String user) throws SubscriptionException {
File userRoot = new File(store.userRoot(user)); File userRoot = new File(store.userRoot(user));
Set<String> subscriptionNames;
try { try {
subscriptionNames = readSubscriptions(userRoot); return readSubscriptions(userRoot);
} catch (IOException e) { } catch (IOException e) {
throw new SubscriptionException(e); throw new SubscriptionException(e);
} }
return subscriptionNames;
} }


/** /**
Expand All @@ -144,16 +148,14 @@ private Set<String> readSubscriptionsForUser(String user) throws SubscriptionExc
*/ */
private Set<String> readSubscriptions(File mailboxFolder) throws IOException { private Set<String> readSubscriptions(File mailboxFolder) throws IOException {
File subscriptionFile = new File(mailboxFolder, FILE_SUBSCRIPTION); File subscriptionFile = new File(mailboxFolder, FILE_SUBSCRIPTION);
HashSet<String> subscriptions = new HashSet<String>();
if (!subscriptionFile.exists()) { if (!subscriptionFile.exists()) {
return subscriptions; return ImmutableSet.of();
} }
FileReader fileReader = new FileReader(subscriptionFile); FileReader fileReader = new FileReader(subscriptionFile);
BufferedReader reader = new BufferedReader(fileReader); BufferedReader reader = new BufferedReader(fileReader);
String subscription; Set<String> subscriptions = reader.lines()
while ((subscription = reader.readLine()) != null) .filter(subscription -> !subscription.equals(""))
if (!subscription.equals("")) .collect(Guavate.toImmutableSet());
subscriptions.add(subscription);
reader.close(); reader.close();
fileReader.close(); fileReader.close();
return subscriptions; return subscriptions;
Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.apache.james.mailbox.store.mail.model.Mailbox; import org.apache.james.mailbox.store.mail.model.Mailbox;
import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox; import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;


import com.github.steveash.guavate.Guavate;
import com.google.common.base.Objects; import com.google.common.base.Objects;


public class InMemoryMailboxMapper implements MailboxMapper { public class InMemoryMailboxMapper implements MailboxMapper {
Expand Down Expand Up @@ -84,13 +85,11 @@ public synchronized Mailbox findMailboxById(MailboxId id) throws MailboxExceptio
*/ */
public List<Mailbox> findMailboxWithPathLike(MailboxPath path) throws MailboxException { public List<Mailbox> findMailboxWithPathLike(MailboxPath path) throws MailboxException {
final String regex = path.getName().replace("%", ".*"); final String regex = path.getName().replace("%", ".*");
List<Mailbox> results = new ArrayList<Mailbox>(); return mailboxesByPath.values()
for (Mailbox mailbox: mailboxesByPath.values()) { .stream()
if (mailboxMatchesRegex(mailbox, path, regex)) { .filter(mailbox -> mailboxMatchesRegex(mailbox, path, regex))
results.add(new SimpleMailbox(mailbox)); .map(SimpleMailbox::new)
} .collect(Guavate.toImmutableList());
}
return results;
} }


private boolean mailboxMatchesRegex(Mailbox mailbox, MailboxPath path, String regex) { private boolean mailboxMatchesRegex(Mailbox mailbox, MailboxPath path, String regex) {
Expand Down Expand Up @@ -134,12 +133,9 @@ public void endRequest() {
*/ */
public boolean hasChildren(Mailbox mailbox, char delimiter) throws MailboxException { public boolean hasChildren(Mailbox mailbox, char delimiter) throws MailboxException {
String mailboxName = mailbox.getName() + delimiter; String mailboxName = mailbox.getName() + delimiter;
for (Mailbox box: mailboxesByPath.values()) { return mailboxesByPath.values()
if (belongsToSameUser(mailbox, box) && box.getName().startsWith(mailboxName)) { .stream()
return true; .anyMatch(box -> belongsToSameUser(mailbox, box) && box.getName().startsWith(mailboxName));
}
}
return false;
} }


private boolean belongsToSameUser(Mailbox mailbox, Mailbox otherMailbox) { private boolean belongsToSameUser(Mailbox mailbox, Mailbox otherMailbox) {
Expand Down
Expand Up @@ -45,6 +45,8 @@
import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage; import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
import org.apache.james.mailbox.store.mail.utils.ApplicableFlagCalculator; import org.apache.james.mailbox.store.mail.utils.ApplicableFlagCalculator;


import com.github.steveash.guavate.Guavate;

public class InMemoryMessageMapper extends AbstractMessageMapper { public class InMemoryMessageMapper extends AbstractMessageMapper {
private final Map<InMemoryId, Map<MessageUid, MailboxMessage>> mailboxByUid; private final Map<InMemoryId, Map<MessageUid, MailboxMessage>> mailboxByUid;
private static final int INITIAL_SIZE = 256; private static final int INITIAL_SIZE = 256;
Expand Down Expand Up @@ -75,13 +77,10 @@ public long countMessagesInMailbox(Mailbox mailbox) throws MailboxException {


@Override @Override
public long countUnseenMessagesInMailbox(Mailbox mailbox) throws MailboxException { public long countUnseenMessagesInMailbox(Mailbox mailbox) throws MailboxException {
long count = 0; return getMembershipByUidForMailbox(mailbox).values()
for (MailboxMessage member : getMembershipByUidForMailbox(mailbox).values()) { .stream()
if (!member.isSeen()) { .filter(member -> !member.isSeen())
count++; .count();
}
}
return count;
} }


@Override @Override
Expand Down Expand Up @@ -118,27 +117,23 @@ public Iterator<MailboxMessage> findInMailbox(Mailbox mailbox, MessageRange set,


@Override @Override
public List<MessageUid> findRecentMessageUidsInMailbox(Mailbox mailbox) throws MailboxException { public List<MessageUid> findRecentMessageUidsInMailbox(Mailbox mailbox) throws MailboxException {
final List<MessageUid> results = new ArrayList<MessageUid>(); return getMembershipByUidForMailbox(mailbox).values()
for (MailboxMessage member : getMembershipByUidForMailbox(mailbox).values()) { .stream()
if (member.isRecent()) { .filter(MailboxMessage::isRecent)
results.add(member.getUid()); .map(MailboxMessage::getUid)
} .sorted()
} .collect(Guavate.toImmutableList());
Collections.sort(results);

return results;
} }


@Override @Override
public MessageUid findFirstUnseenMessageUid(Mailbox mailbox) throws MailboxException { public MessageUid findFirstUnseenMessageUid(Mailbox mailbox) throws MailboxException {
List<MailboxMessage> memberships = new ArrayList<MailboxMessage>(getMembershipByUidForMailbox(mailbox).values()); List<MailboxMessage> memberships = new ArrayList<MailboxMessage>(getMembershipByUidForMailbox(mailbox).values());
Collections.sort(memberships); Collections.sort(memberships);
for (MailboxMessage m : memberships) { return memberships.stream()
if (m.isSeen() == false) { .filter(m -> !m.isSeen())
return m.getUid(); .findFirst()
} .map(MailboxMessage::getUid)
} .orElse(null);
return null;
} }


@Override @Override
Expand Down
Expand Up @@ -48,16 +48,13 @@ public synchronized void delete(Subscription subscription) {


public Subscription findMailboxSubscriptionForUser(String user, String mailbox) { public Subscription findMailboxSubscriptionForUser(String user, String mailbox) {
final List<Subscription> subscriptions = subscriptionsByUser.get(user); final List<Subscription> subscriptions = subscriptionsByUser.get(user);
Subscription result = null;
if (subscriptions != null) { if (subscriptions != null) {
for(Subscription subscription:subscriptions) { return subscriptions.stream()
if (subscription.getMailbox().equals(mailbox)) { .filter(subscription -> subscription.getMailbox().equals(mailbox))
result = subscription; .findFirst()
break; .orElse(null);
}
}
} }
return result; return null;
} }


public List<Subscription> findSubscriptionsForUser(String user) { public List<Subscription> findSubscriptionsForUser(String user) {
Expand Down
4 changes: 4 additions & 0 deletions mailbox/store/pom.xml
Expand Up @@ -72,6 +72,10 @@
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.github.steveash.guavate</groupId>
<artifactId>guavate</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
Expand Down
Expand Up @@ -84,6 +84,7 @@
import org.apache.james.mailbox.store.transaction.TransactionalMapper; import org.apache.james.mailbox.store.transaction.TransactionalMapper;
import org.slf4j.Logger; import org.slf4j.Logger;


import com.github.steveash.guavate.Guavate;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
Expand Down Expand Up @@ -775,13 +776,11 @@ public void startProcessingRequest(MailboxSession session) {


@Override @Override
public List<MailboxPath> list(MailboxSession session) throws MailboxException { public List<MailboxPath> list(MailboxSession session) throws MailboxException {
List<MailboxPath> mList = new ArrayList<MailboxPath>(); return mailboxSessionMapperFactory.getMailboxMapper(session)
List<Mailbox> mailboxes = mailboxSessionMapperFactory.getMailboxMapper(session).list(); .list()
for (Mailbox m : mailboxes) { .stream()
mList.add(m.generateAssociatedPath()); .map(Mailbox::generateAssociatedPath)
} .collect(Guavate.toImmutableList());
return Collections.unmodifiableList(mList);

} }


@Override @Override
Expand Down
Expand Up @@ -20,7 +20,7 @@


import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.stream.Collectors;


import javax.inject.Inject; import javax.inject.Inject;


Expand Down Expand Up @@ -88,13 +88,11 @@ protected Subscription createSubscription(MailboxSession session, String mailbox
* @see org.apache.james.mailbox.SubscriptionManager#subscriptions(org.apache.james.mailbox.MailboxSession) * @see org.apache.james.mailbox.SubscriptionManager#subscriptions(org.apache.james.mailbox.MailboxSession)
*/ */
public Collection<String> subscriptions(MailboxSession session) throws SubscriptionException { public Collection<String> subscriptions(MailboxSession session) throws SubscriptionException {
final SubscriptionMapper mapper = mapperFactory.getSubscriptionMapper(session); return mapperFactory.getSubscriptionMapper(session)
final List<Subscription> subscriptions = mapper.findSubscriptionsForUser(session.getUser().getUserName()); .findSubscriptionsForUser(session.getUser().getUserName())
final Collection<String> results = new HashSet<String>(INITIAL_SIZE); .stream()
for (Subscription subscription:subscriptions) { .map(Subscription::getMailbox)
results.add(subscription.getMailbox()); .collect(Collectors.toCollection(() -> new HashSet<>(INITIAL_SIZE)));
}
return results;
} }


/** /**
Expand Down

0 comments on commit cb0f621

Please sign in to comment.