diff --git a/src/main/java/org/springframework/data/mapping/PropertyPath.java b/src/main/java/org/springframework/data/mapping/PropertyPath.java index 02f502154..debffac2c 100644 --- a/src/main/java/org/springframework/data/mapping/PropertyPath.java +++ b/src/main/java/org/springframework/data/mapping/PropertyPath.java @@ -108,15 +108,13 @@ public class PropertyPath implements Streamable { } /** - * Returns the first part of the {@link PropertyPath}. + * Returns the first part of the {@link PropertyPath}. For example: * - *
-	 * {@code
+	 * 
 	 * PropertyPath.from("a.b.c", Some.class).getSegment();
-	 * }
 	 * 
* - * will result in {@code "a"} + * results in {@code a}. * * @return the name will never be {@literal null}. */ @@ -150,10 +148,10 @@ public class PropertyPath implements Streamable { } /** - * Returns the type of the property will return the plain resolved type for simple properties, the component type for - * any {@link Iterable} or the value type of a {@link java.util.Map} if the property is one. + * Returns the actual type of the property. Will return the plain resolved type for simple properties, the component + * type for any {@link Iterable} or the value type of a {@link java.util.Map}. * - * @return + * @return the actual type of the property. */ public Class getType() { return this.actualTypeInformation.getType(); @@ -164,15 +162,13 @@ public class PropertyPath implements Streamable { } /** - * Returns the {@link PropertyPath} path that results from removing the first element of the current one. + * Returns the {@link PropertyPath} path that results from removing the first element of the current one. For example: * - *
-	 * {@code
-	 * System.out.println(PropertyPath.from("a.b.c", Some.class).next().toDotPath());
-	 * }
+	 * 
+	 * PropertyPath.from("a.b.c", Some.class).next().toDotPath();
 	 * 
* - * Will result in the output: {@code b.c} + * results in the output: {@code b.c} * * @return the next nested {@link PropertyPath} or {@literal null} if no nested {@link PropertyPath} available. * @see #hasNext() @@ -195,7 +191,7 @@ public class PropertyPath implements Streamable { /** * Returns the {@link PropertyPath} in dot notation. * - * @return + * @return the {@link PropertyPath} in dot notation. */ public String toDotPath() { @@ -209,7 +205,7 @@ public class PropertyPath implements Streamable { /** * Returns whether the {@link PropertyPath} is actually a collection. * - * @return + * @return {@literal true} whether the {@link PropertyPath} is actually a collection. */ public boolean isCollection() { return isCollection; @@ -232,37 +228,34 @@ public class PropertyPath implements Streamable { /** * Returns an {@link Iterator} that iterates over all the partial property paths with the same leaf type - * but decreasing length. + * but decreasing length. For example: * - *
-	 * {@code
+	 * 
 	 * PropertyPath propertyPath = PropertyPath.from("a.b.c", Some.class);
-	 * for (p : propertyPath.iterator()) {
-	 *     System.out.println(p.toDotPath());
-	 * };
-	 * }
+	 * propertyPath.forEach(p -> p.toDotPath());
 	 * 
* - * Results in the output: + * results in the dot paths: * * - *
-	 * {@code
+	 * 
 	 * a.b.c
 	 * b.c
 	 * c
-	 * }
 	 * 
*/ + @Override public Iterator iterator() { return new Iterator() { private @Nullable PropertyPath current = PropertyPath.this; + @Override public boolean hasNext() { return current != null; } + @Override @Nullable public PropertyPath next() { @@ -275,10 +268,6 @@ public class PropertyPath implements Streamable { this.current = result.next(); return result; } - - public void remove() { - throw new UnsupportedOperationException(); - } }; } @@ -297,11 +286,9 @@ public class PropertyPath implements Streamable { return false; } - return Objects.equals(this.owningType, that.owningType) - && Objects.equals(this.name, that.name) + return Objects.equals(this.owningType, that.owningType) && Objects.equals(this.name, that.name) && Objects.equals(this.typeInformation, that.typeInformation) - && Objects.equals(this.actualTypeInformation, that.actualTypeInformation) - && Objects.equals(next, that.next); + && Objects.equals(this.actualTypeInformation, that.actualTypeInformation) && Objects.equals(next, that.next); } @Override @@ -312,7 +299,7 @@ public class PropertyPath implements Streamable { /** * Returns the next {@link PropertyPath}. * - * @return + * @return the next {@link PropertyPath}. * @throws IllegalStateException it there's no next one. */ private PropertyPath requiredNext() { @@ -337,8 +324,8 @@ public class PropertyPath implements Streamable { * "userAddress.city" is preferred over "user.address.city". *

* - * @param source a String denoting the property path. Must not be {@literal null}. - * @param type the owning type of the property path. Must not be {@literal null}. + * @param source a String denoting the property path, must not be {@literal null}. + * @param type the owning type of the property path, must not be {@literal null}. * @return a new {@link PropertyPath} guaranteed to be not {@literal null}. */ public static PropertyPath from(String source, Class type) { @@ -355,8 +342,8 @@ public class PropertyPath implements Streamable { * "userAddress.city" is preferred over "user.address.city". *

* - * @param source a String denoting the property path. Must not be {@literal null}. - * @param type the owning type of the property path. Must not be {@literal null}. + * @param source a String denoting the property path, must not be {@literal null}. + * @param type the owning type of the property path, must not be {@literal null}. * @return a new {@link PropertyPath} guaranteed to be not {@literal null}. */ public static PropertyPath from(String source, TypeInformation type) { diff --git a/src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java b/src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java index ebb159b92..39003fe89 100755 --- a/src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java @@ -18,12 +18,13 @@ package org.springframework.data.mapping; import static org.assertj.core.api.Assertions.*; import static org.springframework.data.mapping.PropertyPath.from; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; import org.junit.jupiter.api.Test; -import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.TypeInformation; /** @@ -174,7 +175,7 @@ class PropertyPathUnitTests { assertThat(iterator.hasNext()).isFalse(); } - @Test + @Test // GH-2491 void returnsCorrectIteratorForMultipleElement() { var propertyPath = PropertyPath.from("user.name", Bar.class); @@ -185,14 +186,19 @@ class PropertyPathUnitTests { assertThat(iterator.hasNext()).isTrue(); assertThat(iterator.next()).isEqualTo(propertyPath.next()); assertThat(iterator.hasNext()).isFalse(); + + List paths = new ArrayList<>(); + propertyPath.forEach(it -> paths.add(it.toDotPath())); + + assertThat(paths).containsExactly("user.name", "name"); } @Test // GH-2491 - public void nextReturnsPathWithoutFirstElement() { + void nextReturnsPathWithoutFirstElement() { PropertyPath propertyPath = PropertyPath.from("bar.user.name", Sample.class); - final PropertyPath next = propertyPath.next(); + PropertyPath next = propertyPath.next(); assertThat(next).isNotNull(); assertThat(next.toDotPath()).isEqualTo("user.name"); }