com.github.bloodshura.sparkium.mail.MailProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sparkium-mail Show documentation
Show all versions of sparkium-mail Show documentation
An API which aims to facilitate the usage of javax.mail, also including other mailing services.
The newest version!
package com.github.bloodshura.sparkium.mail;
import com.github.bloodshura.ignitium.collection.map.XMap;
import com.github.bloodshura.ignitium.collection.map.impl.XLinkedMap;
import com.github.bloodshura.ignitium.collection.tuple.Pair;
import com.github.bloodshura.ignitium.collection.view.XView;
import com.github.bloodshura.sparkium.mail.component.Email;
import com.github.bloodshura.sparkium.mail.impl.JavaMailImplementations;
import javax.annotation.Nonnull;
import java.util.function.Predicate;
public class MailProvider {
private static final MailProvider DEFAULT;
private final XMap, Predicate> map;
static {
// TODO IBestMail suffix?!
DEFAULT = new MailProvider();
DEFAULT.register(JavaMailImplementations.AolMail.class, suffix -> suffix.contains("aol"));
DEFAULT.register(JavaMailImplementations.BolMail.class, suffix -> suffix.contains("bol"));
DEFAULT.register(JavaMailImplementations.GloboMail.class, suffix -> suffix.contains("globo"));
DEFAULT.register(JavaMailImplementations.GoogleMail.class, suffix -> suffix.contains("gmail"));
DEFAULT.register(JavaMailImplementations.HotMail.class, suffix -> suffix.contains("hotmail"));
DEFAULT.register(JavaMailImplementations.HotMail.class, suffix -> suffix.contains("live"));
DEFAULT.register(JavaMailImplementations.IgMail.class, suffix -> suffix.contains("ig"));
DEFAULT.register(JavaMailImplementations.TerraMail.class, suffix -> suffix.contains("terra"));
DEFAULT.register(JavaMailImplementations.UolMail.class, suffix -> suffix.contains("uol"));
DEFAULT.register(JavaMailImplementations.YahooMail.class, suffix -> suffix.contains("yahoo"));
}
public MailProvider() {
this.map = new XLinkedMap<>();
}
@Nonnull
public Mail get(@Nonnull Email email) throws MailException {
Pair, Predicate> pair = map.selectFirst(p -> p.getRight().test(email.getSuffix()));
if (pair == null) {
throw new MailException("No implementation found for \"" + email.getSuffix() + '"');
}
try {
return pair.getLeft().newInstance();
} catch (IllegalAccessException | InstantiationException exception) {
throw new MailException("Could not create new instance of " + pair.getLeft(), exception);
}
}
@Nonnull
public XView> getImplementations() {
return map.keys();
}
public void register(@Nonnull Class extends Mail> implClass, @Nonnull Predicate implMatcher) {
map.add(implClass, implMatcher);
}
@Nonnull
public static Mail provide(@Nonnull Email email) throws MailException {
return DEFAULT.get(email);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy