Skip to content

Commit

Permalink
Make rootfactory run under the module system, Fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Jun 25, 2020
1 parent cf5ca4f commit f09cc8e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.speedment.jpastreamer.rootfactory.RootFactory;

import javax.persistence.EntityManagerFactory;
import java.util.ServiceLoader;
import java.util.stream.Stream;

public interface JpaStreamer {
Expand All @@ -13,13 +14,13 @@ public interface JpaStreamer {

static JpaStreamerBuilder createJpaStreamerBuilder(final String persistenceUnitName) {
return RootFactory
.getOrThrow(JpaStreamerBuilderFactory.class)
.getOrThrow(JpaStreamerBuilderFactory.class, ServiceLoader::load)
.create(persistenceUnitName);
}

static JpaStreamerBuilder createJpaStreamerBuilder(final EntityManagerFactory entityManagerFactory) {
return RootFactory
.getOrThrow(JpaStreamerBuilderFactory.class)
.getOrThrow(JpaStreamerBuilderFactory.class, ServiceLoader::load)
.create(entityManagerFactory);
}

Expand Down
1 change: 1 addition & 0 deletions application/src/main/java9/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@

requires transitive java.persistence;
requires jpastreamer.rootfactory;
uses com.speedment.jpastreamer.application.JpaStreamerBuilderFactory;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.speedment.jpastreamer.application.standard.internal;

import javax.persistence.EntityManagerFactory;
import java.util.ServiceLoader;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;
Expand All @@ -19,8 +20,8 @@ final class StandardStreamer<E> implements Streamer<E> {

StandardStreamer(final Class<E> entityClass, final EntityManagerFactory entityManagerFactory) {
this.entityClass = requireNonNull(entityClass);
this.builderFactory = RootFactory.getOrThrow(BuilderFactory.class);
this.renderer = RootFactory.getOrThrow(RendererFactory.class)
this.builderFactory = RootFactory.getOrThrow(BuilderFactory.class, ServiceLoader::load);
this.renderer = RootFactory.getOrThrow(RendererFactory.class, ServiceLoader::load)
.createRenderer(entityManagerFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.speedment.jpastreamer.renderer.RendererFactory;
import com.speedment.jpastreamer.rootfactory.RootFactory;

import java.util.ServiceLoader;

enum InjectedFactories implements Factories {
INSTANCE;

Expand All @@ -17,10 +19,10 @@ enum InjectedFactories implements Factories {
private final AutoCloseFactory autoClose;

InjectedFactories() {
pipeline = RootFactory.getOrThrow(PipelineFactory.class);
intermediate = RootFactory.getOrThrow(IntermediateOperationFactory.class);
terminal = RootFactory.getOrThrow(TerminalOperationFactory.class);
autoClose = RootFactory.getOrThrow(AutoCloseFactory.class);
pipeline = RootFactory.getOrThrow(PipelineFactory.class, ServiceLoader::load);
intermediate = RootFactory.getOrThrow(IntermediateOperationFactory.class, ServiceLoader::load);
terminal = RootFactory.getOrThrow(TerminalOperationFactory.class, ServiceLoader::load);
autoClose = RootFactory.getOrThrow(AutoCloseFactory.class, ServiceLoader::load);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.persistence.criteria.Predicate;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;

public enum FilterCriteriaMerger implements CriteriaMerger {

Expand All @@ -40,7 +41,7 @@ public enum FilterCriteriaMerger implements CriteriaMerger {
private final PredicateFactory predicateFactory;

FilterCriteriaMerger() {
this.predicateFactory = RootFactory.getOrThrow(PredicateFactory.class);
this.predicateFactory = RootFactory.getOrThrow(PredicateFactory.class, ServiceLoader::load);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;
import java.util.ServiceLoader;
import java.util.stream.Stream;

public final class StandardRenderer implements Renderer {
Expand All @@ -30,10 +31,10 @@ public final class StandardRenderer implements Renderer {

StandardRenderer(final EntityManagerFactory entityManagerFactory) {
this.entityManager = requireNonNull(entityManagerFactory).createEntityManager();
this.criteriaFactory = RootFactory.getOrThrow(CriteriaFactory.class);
this.criteriaMerger = RootFactory.getOrThrow(CriteriaMerger.class);
this.queryMerger = RootFactory.getOrThrow(QueryMerger.class);
this.preOptimizerFactory = RootFactory.getOrThrow(PreOptimizerFactory.class);
this.criteriaFactory = RootFactory.getOrThrow(CriteriaFactory.class, ServiceLoader::load);
this.criteriaMerger = RootFactory.getOrThrow(CriteriaMerger.class, ServiceLoader::load);
this.queryMerger = RootFactory.getOrThrow(QueryMerger.class, ServiceLoader::load);
this.preOptimizerFactory = RootFactory.getOrThrow(PreOptimizerFactory.class, ServiceLoader::load);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
import com.speedment.jpastreamer.rootfactory.internal.InternalRootFactory;

import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Function;
import java.util.stream.Stream;

public final class RootFactory {
private RootFactory() {}

public static <T> Optional<T> get(final Class<T> classToken) {
return InternalRootFactory.get(classToken);
public static <S> Optional<S> get(final Class<S> classToken, final Function<Class<S>, ServiceLoader<S>> loader) {
return InternalRootFactory.get(classToken, loader);
}

public static <T> T getOrThrow(final Class<T> classToken) {
return InternalRootFactory.getOrThrow(classToken);
public static <S> S getOrThrow(final Class<S> classToken, final Function<Class<S>, ServiceLoader<S>> loader) {
return InternalRootFactory.getOrThrow(classToken, loader);
}

public static <S> Stream<S> stream(final Class<S> service) {
return InternalRootFactory.stream(service);
public static <S> Stream<S> stream(final Class<S> service, final Function<Class<S>, ServiceLoader<S>> loader) {
return InternalRootFactory.stream(service, loader);
}

}
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
package com.speedment.jpastreamer.rootfactory.internal;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.Objects.requireNonNull;

public final class InternalRootFactory {

private InternalRootFactory() {
}

public static <S> Optional<S> get(final Class<S> service) {
return Optional.ofNullable(getHelper(service));
public static <S> Optional<S> get(final Class<S> service, final Function<Class<S>, ServiceLoader<S>> loader) {
requireNonNull(service);
requireNonNull(loader);
return Optional.ofNullable(getHelper(service, loader));
}

public static <S> S getOrThrow(final Class<S> service) {
final S selectedService = getHelper(service);
public static <S> S getOrThrow(final Class<S> service, Function<Class<S>, ServiceLoader<S>> loader) {
requireNonNull(service);
requireNonNull(loader);
final S selectedService = getHelper(service, loader);
if (selectedService == null) {
throw new NoSuchElementException("Unable to get the service " + service.getName());
} else {
return selectedService;
}
}

public static <S> Stream<S> stream(final Class<S> service) {
final Iterator<S> iterator = ServiceLoader.load(service).iterator();
public static <S> Stream<S> stream(final Class<S> service, final Function<Class<S>, ServiceLoader<S>> loader ) {
requireNonNull(service);
requireNonNull(loader);
final Iterator<S> iterator = loader.apply(service).iterator();
if (iterator.hasNext()) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.NONNULL), false);
} else {
Expand All @@ -38,8 +47,8 @@ public static <S> Stream<S> stream(final Class<S> service) {

}

private static <S> S getHelper(final Class<S> service) {
final Iterator<S> iterator = ServiceLoader.load(service).iterator();
private static <S> S getHelper(final Class<S> service, Function<Class<S>, ServiceLoader<S>> loader) {
final Iterator<S> iterator = loader.apply(service).iterator();
if (iterator.hasNext()) {
return iterator.next();
} else {
Expand Down

0 comments on commit f09cc8e

Please sign in to comment.