-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renames classes DynamicReturnConverter
- Loading branch information
1 parent
e6876ff
commit ae8f85c
Showing
2 changed files
with
90 additions
and
18 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
mapping/artemis-core/src/main/java/org/jnosql/artemis/reflection/DynamicReturnConverter.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,90 @@ | ||
/* | ||
* Copyright (c) 2017 Otávio Santana and others | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
*/ | ||
package org.jnosql.artemis.reflection; | ||
|
||
import org.jnosql.diana.api.NonUniqueResultException; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.PriorityQueue; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
public enum DynamicReturnConverter { | ||
|
||
INSTANCE; | ||
|
||
public Object returnObject(ColumnQuery query, ColumnTemplate template, Class typeClass, Method method) { | ||
Class<?> returnType = method.getReturnType(); | ||
|
||
if (typeClass.equals(returnType)) { | ||
Optional<Object> optional = template.singleResult(query); | ||
return optional.orElse(null); | ||
|
||
} else if (Optional.class.equals(returnType)) { | ||
return template.singleResult(query); | ||
} else if (List.class.equals(returnType) | ||
|| Iterable.class.equals(returnType) | ||
|| Collection.class.equals(returnType)) { | ||
return template.select(query); | ||
} else if (Set.class.equals(returnType)) { | ||
return new HashSet<>(template.select(query)); | ||
} else if (Queue.class.equals(returnType)) { | ||
return new PriorityQueue<>(template.select(query)); | ||
} else if (Stream.class.equals(returnType)) { | ||
return template.select(query).stream(); | ||
} | ||
|
||
return template.select(query); | ||
} | ||
|
||
<T> Object returnObject(List<T> entities, Class<?> typeClass, Method method) { | ||
Class<?> returnType = method.getReturnType(); | ||
|
||
if (typeClass.equals(returnType)) { | ||
return getObject(entities, method); | ||
|
||
} else if (Optional.class.equals(returnType)) { | ||
return Optional.ofNullable(getObject(entities, method)); | ||
} else if (List.class.equals(returnType) | ||
|| Iterable.class.equals(returnType) | ||
|| Collection.class.equals(returnType)) { | ||
return entities; | ||
} else if (Set.class.equals(returnType)) { | ||
return new HashSet<>(entities); | ||
} else if (Queue.class.equals(returnType)) { | ||
return new PriorityQueue<>(entities); | ||
} else if (Stream.class.equals(returnType)) { | ||
return entities.stream(); | ||
} | ||
|
||
return entities; | ||
} | ||
|
||
private <T> Object getObject(List<T> entities, Method method) { | ||
if (entities.isEmpty()) { | ||
return null; | ||
} | ||
if (entities.size() == 1) { | ||
return entities.get(0); | ||
} | ||
throw new NonUniqueResultException("No unique result to the method: " + method); | ||
} | ||
|
||
} |
18 changes: 0 additions & 18 deletions
18
...ing/artemis-core/src/main/java/org/jnosql/artemis/reflection/ReturnTypeConverterUtil.java
This file was deleted.
Oops, something went wrong.