Skip to content

Commit

Permalink
#4: Implement draft of search mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroup committed Apr 5, 2019
1 parent 4748cab commit 83f816c
Show file tree
Hide file tree
Showing 21 changed files with 646 additions and 145 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void main(final String[] args) {
...
final Inbox inbox = new JavaxMailInbox(smtp);
final Iterable<Msg> msgs = inbox.read(
new Query("imaps", "INBOX", new All())
new Query("pop3s", "INBOX", new All())
);
for(final Msg msg : msgs) {
System.out.println(msg);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/github/dgroup/mbox4j/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

package io.github.dgroup.mbox4j;

import io.github.dgroup.mbox4j.query.mode.Mode;
import io.github.dgroup.mbox4j.query.Mode;

/**
* The query for email search procedure..
Expand All @@ -50,9 +50,9 @@ public interface Query {
String folder();

/**
* The type of search mode within email folder.
* The type of the search mode within email folder.
* @return The mode.
* @see io.github.dgroup.mbox4j.query.mode.All
* @see io.github.dgroup.mbox4j.query.mode
*/
Mode mode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import io.github.dgroup.mbox4j.Inbox;
import io.github.dgroup.mbox4j.Msg;
import io.github.dgroup.mbox4j.Query;
import io.github.dgroup.mbox4j.inbox.javax.search.mode.Modes;
import java.util.Collections;
import io.github.dgroup.mbox4j.inbox.javax.search.Modes;
import io.github.dgroup.mbox4j.inbox.javax.search.Search;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.MessagingException;
Expand All @@ -44,8 +44,6 @@
* {@code final Inbox inbox = new JavaxMailInbox(smptProperties);}
*
* @since 0.1.0
* @todo #/DEV Implement search to the javax instead of fetching all messages.
* The search should be based on {@link Query}.
*/
public final class JavaxMailInbox implements Inbox {

Expand Down Expand Up @@ -100,8 +98,9 @@ public Iterable<Msg> read(final Query query) throws EmailException {
folder = store.getFolder(query.folder());
folder.open(Folder.READ_ONLY);
return this.modes.getOrDefault(
query.mode(), fallback -> Collections.emptySet()
).apply(folder);
query.mode().name(),
new Search.Empty()
).apply(query, folder);
// @checkstyle IllegalCatchCheck (3 lines)
} catch (final Exception cause) {
throw new EmailException(cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,25 @@
* OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.dgroup.mbox4j.inbox.javax.search.mode;
package io.github.dgroup.mbox4j.inbox.javax.search;

import io.github.dgroup.mbox4j.Msg;
import io.github.dgroup.mbox4j.inbox.javax.ToMsg;
import java.util.ArrayList;
import javax.mail.Folder;
import javax.mail.Message;
import org.cactoos.Func;
import org.cactoos.collection.Mapped;
import io.github.dgroup.mbox4j.query.Mode;
import org.cactoos.iterable.IterableOf;

/**
* Search mode within the email folder which is fetching all emails
* using {@link javax.mail}.
*
* @since 0.1.0
* @todo #/DEV All#search - Add integration test
*/
public final class All implements Func<Folder, Iterable<Msg>> {

/**
* The function to map {@link javax.mail.Message} to {@link Msg}.
*/
private final Func<Message, Msg> fnc;
public final class All extends SearchOf {

/**
* Ctor.
*/
public All() {
this(new ToMsg());
super((query, folder) -> new IterableOf<>(folder.getMessages()), Mode.ALL);
}

/**
* Ctor.
* @param fnc The function to map {@link javax.mail.Message} to {@link Msg}.
*/
public All(final Func<Message, Msg> fnc) {
this.fnc = fnc;
}

@Override
public Iterable<Msg> apply(final Folder folder) throws Exception {
return new ArrayList<>(new Mapped<>(this.fnc, folder.getMessages()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
* OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.dgroup.mbox4j.inbox.javax.search.mode;
package io.github.dgroup.mbox4j.inbox.javax.search;

import io.github.dgroup.mbox4j.Msg;
import io.github.dgroup.mbox4j.query.mode.Mode;
import io.github.dgroup.mbox4j.query.Mode;
import java.util.Map;
import javax.mail.Folder;
import org.cactoos.Func;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapEnvelope;
import org.cactoos.map.MapOf;
Expand All @@ -38,24 +35,25 @@
*
* @since 0.1.0
*/
public final class Modes extends MapEnvelope<Mode, Func<Folder, Iterable<Msg>>> {
public final class Modes extends MapEnvelope<String, Search> {

/**
* Ctor.
*/
public Modes() {
this(
new MapOf<>(
new MapEntry<>(new io.github.dgroup.mbox4j.query.mode.All(), new All())
)
);
this(new MapOf<>(
new MapEntry<>(Mode.ALL, new All()),
new MapEntry<>(Mode.RECENT, new Recent()),
new MapEntry<>(Mode.RANGE, new Range()),
new MapEntry<>(Mode.UNREAD, new Unread())
));
}

/**
* Ctor.
* @param modes The search modes.
*/
public Modes(final Map<Mode, Func<Folder, Iterable<Msg>>> modes) {
public Modes(final Map<String, Search> modes) {
super(() -> modes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* MIT License
*
* Copyright (c) 2019 Yurii Dubinka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.dgroup.mbox4j.inbox.javax.search;

import io.github.dgroup.mbox4j.query.Mode;
import org.cactoos.iterable.IterableOf;

/**
* Search mode within the email folder which is fetching emails
* based on their indexes using {@link javax.mail}.
*
* The indexing starts from <em>1</em>.
*
* @since 0.1.0
* @todo #/DEV Range#search - add integration test
*/
public final class Range extends SearchOf {

/**
* Ctor.
*/
public Range() {
super(
(query, folder) -> {
final String zero = "0";
final int start = Integer.parseInt(query.mode().argument("start", zero));
final int end = Integer.parseInt(query.mode().argument("end", zero));
return new IterableOf<>(folder.getMessages(start, end));
},
Mode.RANGE
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* MIT License
*
* Copyright (c) 2019 Yurii Dubinka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.dgroup.mbox4j.inbox.javax.search;

import io.github.dgroup.mbox4j.query.Mode;
import java.util.concurrent.atomic.AtomicInteger;
import javax.mail.Flags.Flag;
import org.cactoos.iterable.Filtered;

/**
* Search mode within the email folder which is fetching all recent email.
* using {@link javax.mail}.
*
* @since 0.1.0
* @todo #/DEV Recent#search - add integration test
*/
public final class Recent extends SearchOf {

/**
* Ctor.
*/
public Recent() {
super(
(query, folder) -> {
final int quantity = folder.getNewMessageCount();
final AtomicInteger added = new AtomicInteger(0);
return new Filtered<>(
msg -> msg.isSet(Flag.RECENT) && added.incrementAndGet() < quantity,
folder.getMessages()
);
},
Mode.RECENT
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,42 @@
* OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.dgroup.mbox4j.inbox;
package io.github.dgroup.mbox4j.inbox.javax.search;

import io.github.dgroup.mbox4j.EmailException;
import io.github.dgroup.mbox4j.Msg;
import io.github.dgroup.mbox4j.Query;
import org.cactoos.Func;
import java.util.Collections;
import javax.mail.Folder;
import org.cactoos.BiFunc;

/**
* The email inbox.
*
* @see io.github.dgroup.mbox4j.Inbox
* @see io.github.dgroup.mbox4j.inbox.javax.func
* The search within email folder.
*
* @since 0.1.0
* @todo #/DEV Add documentation about {@link io.github.dgroup.mbox4j.inbox.javax.search}
* to the <em>readme.md</em>.
*/
public final class InboxOf extends InboxEnvelope {
public interface Search extends BiFunc<Query, Folder, Iterable<Msg>> {

/**
* Ctor.
* @param fnc The function to fetch email messages based on query.
* @see io.github.dgroup.mbox4j.inbox.javax.func
* Search the emails.
* @param query The search details.
* @param folder The email folder.
* @return The emails.
* @throws EmailException In the case of connectivity/transformation issues.
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public InboxOf(final Func<Query, Iterable<Msg>> fnc) {
super(query -> {
try {
return fnc.apply(query);
// @checkstyle IllegalCatchCheck (3 lines)
} catch (final Exception cause) {
throw new EmailException(cause);
}
});
Iterable<Msg> apply(Query query, Folder folder) throws EmailException;

/**
* The search which returns empty result.
* @since 0.1.0
*/
class Empty implements Search {

@Override
public Iterable<Msg> apply(final Query query, final Folder folder) {
return Collections.emptySet();
}
}
}
Loading

11 comments on commit 83f816c

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-eca9a629 disappeared from src/main/java/io/github/dgroup/mbox4j/inbox/func/javax/JavaxMailInbox.java, that's why I closed #4. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-647b5960 discovered in src/main/java/io/github/dgroup/mbox4j/query/mode/package-info.java and submitted as #27. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-743d9b92 discovered in src/main/java/io/github/dgroup/mbox4j/inbox/javax/search/All.java and submitted as #28. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-6736be7e discovered in src/main/java/io/github/dgroup/mbox4j/inbox/javax/search/Range.java and submitted as #29. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-89560df8 discovered in src/main/java/io/github/dgroup/mbox4j/inbox/javax/search/package-info.java and submitted as #30. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-8e29838e discovered in src/main/java/io/github/dgroup/mbox4j/inbox/javax/search/package-info.java and submitted as #31. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-5f34ad4c discovered in src/main/java/io/github/dgroup/mbox4j/inbox/javax/search/package-info.java and submitted as #32. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-d12d997b discovered in src/main/java/io/github/dgroup/mbox4j/inbox/javax/search/package-info.java and submitted as #33. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-2451da39 discovered in src/main/java/io/github/dgroup/mbox4j/inbox/javax/search/Recent.java and submitted as #34. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-9e3d01ad discovered in src/main/java/io/github/dgroup/mbox4j/inbox/javax/search/Search.java and submitted as #35. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 83f816c Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-75afffcf discovered in src/main/java/io/github/dgroup/mbox4j/inbox/javax/search/Unread.java and submitted as #36. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.