Skip to content

Commit

Permalink
runtime-core: Use new FindFromNullableReference in Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed Jun 1, 2017
1 parent 52ab289 commit 6576b66
Showing 1 changed file with 73 additions and 43 deletions.
Expand Up @@ -16,13 +16,14 @@
*/ */
package com.speedment.runtime.core.manager; package com.speedment.runtime.core.manager;


import com.speedment.common.singletonstream.SingletonStream;
import com.speedment.runtime.config.identifier.TableIdentifier; import com.speedment.runtime.config.identifier.TableIdentifier;
import com.speedment.runtime.core.exception.SpeedmentException; import com.speedment.runtime.core.exception.SpeedmentException;
import com.speedment.runtime.field.Field; import com.speedment.runtime.field.Field;
import com.speedment.runtime.field.method.BackwardFinder; import com.speedment.runtime.field.method.BackwardFinder;
import com.speedment.runtime.field.method.FindFrom; import com.speedment.runtime.field.method.FindFrom;
import com.speedment.runtime.field.trait.HasFinder; import com.speedment.runtime.field.trait.HasFinder;
import com.speedment.runtime.field.trait.HasNullableFinder;

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


Expand All @@ -35,9 +36,9 @@
* *
* @param <ENTITY> entity type for this Manager * @param <ENTITY> entity type for this Manager
* *
* @author Per Minborg * @author Per Minborg
* @author Emil Forslund * @author Emil Forslund
* @since 2.0.0 * @since 2.0.0
*/ */
public interface Manager<ENTITY> { public interface Manager<ENTITY> {


Expand Down Expand Up @@ -157,8 +158,11 @@ public interface Manager<ENTITY> {
* *
* *
* @return a new stream over all entities in this table * @return a new stream over all entities in this table
*
* @throws SpeedmentException if an error occurs during a Terminal Operation * @throws SpeedmentException if an error occurs during a Terminal Operation
* (e.g. an SqlException is thrown by the underlying database) * (e.g. an SqlException is thrown by the
* underlying database)
*
* @see java.util.stream * @see java.util.stream
* @see Stream * @see Stream
*/ */
Expand All @@ -181,9 +185,10 @@ public interface Manager<ENTITY> {
* entity. * entity.
* *
* @param entity to persist * @param entity to persist
* @return an entity reflecting the result of the persisted entity * @return an entity reflecting the result of the persisted entity
*
* @throws SpeedmentException if the underlying database throws an exception * @throws SpeedmentException if the underlying database throws an exception
* (e.g. SQLException) * (e.g. SQLException)
*/ */
default ENTITY persist(ENTITY entity) throws SpeedmentException { default ENTITY persist(ENTITY entity) throws SpeedmentException {
return persister().apply(entity); return persister().apply(entity);
Expand Down Expand Up @@ -217,18 +222,18 @@ default ENTITY persist(ENTITY entity) throws SpeedmentException {
* Entities are uniquely identified by their primary key(s). * Entities are uniquely identified by their primary key(s).
* *
* @param entity to update * @param entity to update
* @return an entity reflecting the result of the updated entity * @return an entity reflecting the result of the updated entity
*
* @throws SpeedmentException if the underlying database throws an exception * @throws SpeedmentException if the underlying database throws an exception
* (e.g. SQLException) * (e.g. SQLException)
*/ */
default ENTITY update(ENTITY entity) throws SpeedmentException { default ENTITY update(ENTITY entity) throws SpeedmentException {
return updater().apply(entity); return updater().apply(entity);
} }


/** /**
* Returns an {@link Updater} that when its * Returns an {@link Updater} that when its {@link Persister#apply(Object)}
* {@link Persister#apply(java.lang.Object) } method is called, will produce * method is called, will produce the same result as {@link #update(Object)}
* the same result as {@link #update(java.lang.Object) }
* *
* @return an Updater * @return an Updater
*/ */
Expand All @@ -242,35 +247,39 @@ default ENTITY update(ENTITY entity) throws SpeedmentException {
* Entities are uniquely identified by their primary key(s). * Entities are uniquely identified by their primary key(s).
* *
* @param entity to remove * @param entity to remove
* @return the provided entity instance * @return the provided entity instance
*
* @throws SpeedmentException if the underlying database throws an exception * @throws SpeedmentException if the underlying database throws an exception
* (e.g. SQLException) * (e.g. SQLException)
*/ */
default ENTITY remove(ENTITY entity) throws SpeedmentException { default ENTITY remove(ENTITY entity) throws SpeedmentException {
return remover().apply(entity); return remover().apply(entity);
} }


/** /**
* Returns a {@link Remover} that when its * Returns a {@link Remover} that when its {@link Persister#apply(Object)}
* {@link Persister#apply(java.lang.Object) } method is called, will produce * method is called, will produce the same result as {@link #remove(Object)}
* the same result as {@link #remove(java.lang.Object) }
* *
* @return a Remover * @return a Remover
*/ */
Remover<ENTITY> remover(); Remover<ENTITY> remover();


/** /**
* Returns a Function that, when it is applied, will produce an equivalent * Returns a Function that, when it is applied, will produce an equivalent
* result as if {@link #finderByNullable(HasFinder)} was called. * result as if {@link #finderByNullable(HasNullableFinder)} was called.
* *
* @param <FK_ENTITY> the type of the foreign entity * @param <FK_ENTITY> the type of the foreign entity
*
* @param fkField the foreign key field * @param fkField the foreign key field
* @return a function that returns an Entity (if any) that matches the given * @return a function that returns an Entity (if any) that matches
* a foreign key relation (foreign field and entity) * the given a foreign key relation (foreign field and
* entity)
* *
* @see #finderByNullable(HasFinder) * @see #finderByNullable(HasNullableFinder)
*/ */
default <FK_ENTITY> FindFrom<FK_ENTITY, ENTITY> finderBy(HasFinder<FK_ENTITY, ENTITY> fkField) { default <FK_ENTITY> FindFrom<FK_ENTITY, ENTITY> finderBy(
HasFinder<FK_ENTITY, ENTITY> fkField) {

return fkField.finder(getTableIdentifier(), this::stream); return fkField.finder(getTableIdentifier(), this::stream);
} }


Expand All @@ -282,28 +291,37 @@ default <FK_ENTITY> FindFrom<FK_ENTITY, ENTITY> finderBy(HasFinder<FK_ENTITY, EN
* field is pointing to. * field is pointing to.
* *
* @param <FK_ENTITY> the type of the foreign entity * @param <FK_ENTITY> the type of the foreign entity
* @param fkField the foreign key field *
* @param fkField the foreign key field
* @param fkEntity the foreign key entity * @param fkEntity the foreign key entity
* @return an Entity (if any) that matches the given a foreign key relation * @return an Entity (if any) that matches the given a foreign key
* (foreign field and entity) * relation (foreign field and entity)
*/ */
default <FK_ENTITY> ENTITY findBy(HasFinder<FK_ENTITY, ENTITY> fkField, FK_ENTITY fkEntity) { default <FK_ENTITY> ENTITY findBy(
HasFinder<FK_ENTITY, ENTITY> fkField,
FK_ENTITY fkEntity) {

return finderBy(fkField).apply(fkEntity); return finderBy(fkField).apply(fkEntity);
} }


/** /**
* Returns a Function that, when it is applied, will produce an equivalent * Returns a Function that, when it is applied, will produce an equivalent
* result as if {@link #findByNullable(HasFinder, Object)} was called. * result as if {@link #findByNullable(HasNullableFinder, Object)} was
* called.
* *
* @param <FK_ENTITY> the type of the foreign entity * @param <FK_ENTITY> the type of the foreign entity
*
* @param fkField the foreign key field * @param fkField the foreign key field
* @return a function that returns an Entity (if any) that matches the given * @return a function that returns an Entity (if any) that matches
* a foreign key relation (foreign field and entity) * the given a foreign key relation (foreign field and
* entity)
* *
* @see #findByNullable(HasFinder, Object) * @see #findByNullable(HasNullableFinder, Object)
*/ */
default <FK_ENTITY> Function<FK_ENTITY, Stream<ENTITY>> finderByNullable(HasFinder<FK_ENTITY, ENTITY> fkField) { default <FK_ENTITY> Function<FK_ENTITY, Stream<ENTITY>> finderByNullable(
return fkEntity -> SingletonStream.ofNullable(finderBy(fkField).apply(fkEntity)); HasNullableFinder<FK_ENTITY, ENTITY> fkField) {

return fkField.nullableFinder(getTableIdentifier(), this::stream);
} }


/** /**
Expand All @@ -316,12 +334,16 @@ default <FK_ENTITY> Function<FK_ENTITY, Stream<ENTITY>> finderByNullable(HasFind
* contain either zero or one element. * contain either zero or one element.
* *
* @param <FK_ENTITY> the type of the foreign entity * @param <FK_ENTITY> the type of the foreign entity
* @param fkField the foreign key field *
* @param fkField the foreign key field
* @param fkEntity the foreign key entity * @param fkEntity the foreign key entity
* @return an Entity (if any) that matches the given a foreign key relation * @return an Entity (if any) that matches the given a foreign key
* (foreign field and entity) * relation (foreign field and entity)
*/ */
default <FK_ENTITY> Stream<ENTITY> findByNullable(HasFinder<FK_ENTITY, ENTITY> fkField, FK_ENTITY fkEntity) { default <FK_ENTITY> Stream<ENTITY> findByNullable(
HasNullableFinder<FK_ENTITY, ENTITY> fkField,
FK_ENTITY fkEntity) {

return finderByNullable(fkField).apply(fkEntity); return finderByNullable(fkField).apply(fkEntity);
} }


Expand All @@ -330,12 +352,16 @@ default <FK_ENTITY> Stream<ENTITY> findByNullable(HasFinder<FK_ENTITY, ENTITY> f
* result as if {@link #findBackwardsBy(HasFinder, Object)} was called. * result as if {@link #findBackwardsBy(HasFinder, Object)} was called.
* *
* @param <FK_ENTITY> the type of the foreign entity * @param <FK_ENTITY> the type of the foreign entity
*
* @param fkField the foreign key field * @param fkField the foreign key field
* @return an Entity (if any) that matches the given a foreign key relation * @return an Entity (if any) that matches the given a foreign key
* (foreign field and entity) * relation (foreign field and entity)
*
* @see #findBackwardsBy(HasFinder, Object) * @see #findBackwardsBy(HasFinder, Object)
*/ */
default <FK_ENTITY> BackwardFinder<FK_ENTITY, ENTITY> finderBackwardsBy(HasFinder<ENTITY, FK_ENTITY> fkField) { default <FK_ENTITY> BackwardFinder<FK_ENTITY, ENTITY> finderBackwardsBy(
HasFinder<ENTITY, FK_ENTITY> fkField) {

return fkField.backwardFinder(getTableIdentifier(), this::stream); return fkField.backwardFinder(getTableIdentifier(), this::stream);
} }


Expand All @@ -347,12 +373,16 @@ default <FK_ENTITY> BackwardFinder<FK_ENTITY, ENTITY> finderBackwardsBy(HasFinde
* the Carrots that points to the given hare using the Carrot.HARE column. * the Carrots that points to the given hare using the Carrot.HARE column.
* *
* @param <FK_ENTITY> the type of the foreign entity * @param <FK_ENTITY> the type of the foreign entity
* @param fkField the foreign key field *
* @param fkField the foreign key field
* @param fkEntity the foreign key entity * @param fkEntity the foreign key entity
* @return an Entity (if any) that matches the given a foreign key relation * @return an Entity (if any) that matches the given a foreign key
* (foreign field and entity) * relation (foreign field and entity)
*/ */
default <FK_ENTITY> Stream<ENTITY> findBackwardsBy(HasFinder<ENTITY, FK_ENTITY> fkField, FK_ENTITY fkEntity) { default <FK_ENTITY> Stream<ENTITY> findBackwardsBy(
HasFinder<ENTITY, FK_ENTITY> fkField,
FK_ENTITY fkEntity) {

return finderBackwardsBy(fkField).apply(fkEntity); return finderBackwardsBy(fkField).apply(fkEntity);
} }


Expand Down

0 comments on commit 6576b66

Please sign in to comment.