Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
324792f
commit 3a8ed37
Showing
16 changed files
with
583 additions
and
148 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/main/java/org/springframework/data/neo4j/core/convert/ConvertWith.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,79 @@ | ||
/* | ||
* Copyright 2011-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.neo4j.core.convert; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.function.Function; | ||
|
||
import org.apiguardian.api.API; | ||
import org.neo4j.driver.Value; | ||
|
||
/** | ||
* This annotation can be used to define either custom conversions for single attributes by specifying both a writing | ||
* and a reading converter class and if needed, a custom factory for those or it can be used to build custom meta-annotated | ||
* annotations like {@code @org.springframework.data.neo4j.core.support.DateLong}. | ||
* | ||
* <p>Custom conversions are applied to both attributes of entities and parameters of repository methods that map to those | ||
* attributes (which does apply to all derived queries and queries by example but not to string based queries). | ||
* | ||
* <p>Converter functions that have a default constructor don't need a dedicated factory. A dedicated factory will | ||
* be provided with either this annotation and its values or with the meta annotated annotation, including all configuration | ||
* available. | ||
* | ||
* @author Michael J. Simons | ||
* @soundtrack Antilopen Gang - Abwasser | ||
* @since 6.0 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.ANNOTATION_TYPE, ElementType.FIELD }) | ||
@Inherited | ||
@Documented | ||
@API(status = API.Status.STABLE, since = "6.0") | ||
public @interface ConvertWith { | ||
|
||
Class<? extends Function<?, Value>> writingConverter() default WritingPlaceholder.class; | ||
|
||
Class<? extends Function<Value, ?>> readingConverter() default ReadingPlaceholder.class; | ||
|
||
Class<? extends CustomConversionFactory> converterFactory() default DefaultCustomConversionFactory.class; | ||
|
||
/** | ||
* Indicates an unset writing converter. | ||
*/ | ||
final class WritingPlaceholder implements Function<Object, Value> { | ||
|
||
@Override | ||
public Value apply(Object o) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
|
||
/** | ||
* Indicates an unset reading converter. | ||
*/ | ||
final class ReadingPlaceholder implements Function<Value, Object> { | ||
|
||
@Override | ||
public Object apply(Value value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/org/springframework/data/neo4j/core/convert/CustomConversion.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,72 @@ | ||
/* | ||
* Copyright 2011-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.neo4j.core.convert; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
import org.apiguardian.api.API; | ||
import org.neo4j.driver.Value; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* This class presents the pair of writing and reading converters for a custom conversion. Bot directions are required. | ||
* | ||
* @author Michael J. Simons | ||
* @soundtrack Antilopen Gang - Adrenochrom | ||
* @since 6.0 | ||
*/ | ||
@API(status = API.Status.STABLE, since = "6.0") | ||
public final class CustomConversion { | ||
|
||
private final Function<?, Value> writingConverter; | ||
|
||
private final Function<Value, ?> readingConverter; | ||
|
||
public CustomConversion(Function<?, Value> writingConverter, Function<Value, ?> readingConverter) { | ||
|
||
Assert.notNull(writingConverter, "A writing converter is required."); | ||
this.writingConverter = writingConverter; | ||
Assert.notNull(readingConverter, "A reading converter is required."); | ||
this.readingConverter = readingConverter; | ||
} | ||
|
||
public Function<?, Value> getWritingConverter() { | ||
return writingConverter; | ||
} | ||
|
||
public Function<Value, ?> getReadingConverter() { | ||
return readingConverter; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
CustomConversion that = (CustomConversion) o; | ||
return writingConverter.equals(that.writingConverter) && | ||
readingConverter.equals(that.readingConverter); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(writingConverter, readingConverter); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/springframework/data/neo4j/core/convert/CustomConversionFactory.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,42 @@ | ||
/* | ||
* Copyright 2011-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.neo4j.core.convert; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.AnnotatedElement; | ||
|
||
import org.springframework.core.annotation.MergedAnnotation; | ||
import org.springframework.core.annotation.MergedAnnotations; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
/** | ||
* This interface needs to be implemented to provide custom configuration for a {@link CustomConversion}. Use cases may | ||
* be specific date formats or the like. The build method will receive the whole annotation, including all attributes. | ||
* Classes implementing this interface <strong>must</strong> have a default constructor. | ||
* | ||
* @param <A> The type of the annotation | ||
* @author Michael J. Simons | ||
* @soundtrack Antilopen Gang - Abwasser | ||
* @since 6.0 | ||
*/ | ||
public interface CustomConversionFactory<A extends Annotation> { | ||
|
||
/** | ||
* @param config The configuration for building the custom conversion | ||
* @return The actual conversion | ||
*/ | ||
CustomConversion buildConversion(A config, Class<?> typeOfAnnotatedElement); | ||
} |
45 changes: 45 additions & 0 deletions
45
...main/java/org/springframework/data/neo4j/core/convert/DefaultCustomConversionFactory.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,45 @@ | ||
/* | ||
* Copyright 2011-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.neo4j.core.convert; | ||
|
||
import java.util.function.Function; | ||
|
||
import org.neo4j.driver.Value; | ||
import org.springframework.beans.BeanUtils; | ||
|
||
/** | ||
* @author Michael J. Simons | ||
* @soundtrack Metallica - S&M2 | ||
* @since 6.0 | ||
*/ | ||
final class DefaultCustomConversionFactory implements CustomConversionFactory<ConvertWith> { | ||
|
||
@Override | ||
public CustomConversion buildConversion(ConvertWith config, Class<?> typeOfAnnotatedElement) { | ||
|
||
if (config.writingConverter() == ConvertWith.WritingPlaceholder.class) { | ||
throw new IllegalArgumentException("The default custom conversion factory cannot be used with a placeholder"); | ||
} | ||
|
||
if (config.readingConverter() == ConvertWith.ReadingPlaceholder.class) { | ||
throw new IllegalArgumentException("The default custom conversion factory cannot be used with a placeholder"); | ||
} | ||
|
||
Function<?, Value> writingConverter = BeanUtils.instantiateClass(config.writingConverter()); | ||
Function<Value, ?> readingConverter = BeanUtils.instantiateClass(config.readingConverter()); | ||
return new CustomConversion(writingConverter, readingConverter); | ||
} | ||
} |
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
Oops, something went wrong.