-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for returning optional results
- Loading branch information
Showing
5 changed files
with
68 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author Milad Naseri ([email protected]) | ||
|
@@ -17,7 +18,7 @@ public interface MembershipRepository extends JpaRepository<Membership, String> | |
|
||
List<Membership> findByGroup(Group group); | ||
|
||
Membership findByUserAndGroup(User user, Group group); | ||
Optional<Membership> findByUserAndGroup(User user, Group group); | ||
|
||
List<Membership> findAllByUserAndActive(User user, boolean active); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...c/main/java/com/mmnaseri/utils/spring/data/proxy/impl/adapters/OptionalResultAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.mmnaseri.utils.spring.data.proxy.impl.adapters; | ||
|
||
import com.mmnaseri.utils.spring.data.domain.Invocation; | ||
import com.mmnaseri.utils.spring.data.error.ResultAdapterFailureException; | ||
|
||
import java.util.Iterator; | ||
import java.util.Optional; | ||
|
||
/** | ||
* This adapter accepts all invocations wherein the original value is an {@link Iterable} object and | ||
* the requested method type is an {@link Optional} value. | ||
* | ||
* <p>While adapting, the adapter will also check that the iterable yields only one item and that it | ||
* is of the same type or of a child type of the type requested by the invoked method. | ||
* | ||
* <p>This adapter runs at the priority {@literal -400}. | ||
* | ||
* @author Milad Naseri ([email protected]) | ||
* @since 2.1.1 (10/29/2020) | ||
*/ | ||
public class OptionalResultAdapter extends AbstractIterableResultAdapter<Object> { | ||
|
||
public OptionalResultAdapter() { | ||
super(-400); | ||
} | ||
|
||
@Override | ||
public boolean accepts(final Invocation invocation, final Object originalValue) { | ||
if (!(originalValue instanceof Iterable)) { | ||
return false; | ||
} | ||
final Class<?> returnType = invocation.getMethod().getReturnType(); | ||
return returnType.isAssignableFrom(Optional.class); | ||
} | ||
|
||
@Override | ||
protected Object doAdapt(final Invocation invocation, final Iterable iterable) { | ||
final Iterator iterator = iterable.iterator(); | ||
if (iterator.hasNext()) { | ||
final Object value = iterator.next(); | ||
if (iterator.hasNext()) { | ||
throw new ResultAdapterFailureException( | ||
iterable, | ||
invocation.getMethod().getReturnType(), | ||
"Expected only one item but found many"); | ||
} | ||
return Optional.of(value); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters