Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1cc7616

Browse files
committed
Fixed incorrect docs PersistentPropertyPath and optimized sublist
1 parent 34f212f commit 1cc7616

File tree

3 files changed

+91
-38
lines changed

3 files changed

+91
-38
lines changed

‎src/main/java/org/springframework/data/mapping/PersistentPropertyPath.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends
2929

3030
/**
3131
* Returns the dot based path notation using {@link PersistentProperty#getName()}.
32-
*
33-
* @return
3432
*/
3533
@Nullable
3634
String toDotPath();
@@ -40,7 +38,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends
4038
* {@link PersistentProperty}s to path segments.
4139
*
4240
* @param converter must not be {@literal null}.
43-
* @return
4441
*/
4542
@Nullable
4643
String toDotPath(Converter<? super P, String> converter);
@@ -49,7 +46,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends
4946
* Returns a {@link String} path with the given delimiter based on the {@link PersistentProperty#getName()}.
5047
*
5148
* @param delimiter must not be {@literal null}.
52-
* @return
5349
*/
5450
@Nullable
5551
String toPath(String delimiter);
@@ -60,7 +56,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends
6056
*
6157
* @param delimiter must not be {@literal null}.
6258
* @param converter must not be {@literal null}.
63-
* @return
6459
*/
6560
@Nullable
6661
String toPath(String delimiter, Converter<? super P, String> converter);
@@ -70,7 +65,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends
7065
* {@link PersistentProperty} for {@code bar}. For a simple {@code foo} it returns {@link PersistentProperty} for
7166
* {@code foo}.
7267
*
73-
* @return
7468
*/
7569
@Nullable
7670
P getLeafProperty();
@@ -90,44 +84,36 @@ default P getRequiredLeafProperty() {
9084
* Returns the first property in the {@link PersistentPropertyPath}. So for {@code foo.bar} it will return the
9185
* {@link PersistentProperty} for {@code foo}. For a simple {@code foo} it returns {@link PersistentProperty} for
9286
* {@code foo}.
93-
*
94-
* @return
9587
*/
9688
@Nullable
9789
P getBaseProperty();
9890

9991
/**
10092
* Returns whether the given {@link PersistentPropertyPath} is a base path of the current one. This means that the
101-
* current {@link PersistentPropertyPath} is basically an extension of the given one.
93+
* given {@link PersistentPropertyPath} is basically an extension of this {@link PersistentPropertyPath}.
10294
*
10395
* @param path must not be {@literal null}.
104-
* @return
10596
*/
10697
boolean isBasePathOf(PersistentPropertyPath<P> path);
10798

10899
/**
109100
* Returns the sub-path of the current one as if it was based on the given base path. So for a current path
110101
* {@code foo.bar} and a given base {@code foo} it would return {@code bar}. If the given path is not a base of the
111-
* the current one the current {@link PersistentPropertyPath} will be returned as is.
102+
* current one the current {@link PersistentPropertyPath} will be returned as is.
112103
*
113104
* @param base must not be {@literal null}.
114-
* @return
115105
*/
116106
PersistentPropertyPath<P> getExtensionForBaseOf(PersistentPropertyPath<P> base);
117107

118108
/**
119109
* Returns the parent path of the current {@link PersistentPropertyPath}, i.e. the path without the leaf property.
120110
* This happens up to the base property. So for a direct property reference calling this method will result in
121111
* returning the property.
122-
*
123-
* @return
124112
*/
125113
PersistentPropertyPath<P> getParentPath();
126114

127115
/**
128116
* Returns the length of the {@link PersistentPropertyPath}.
129-
*
130-
* @return
131117
*/
132118
int getLength();
133119
}

‎src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939
class DefaultPersistentPropertyPath<P extends PersistentProperty<P>> implements PersistentPropertyPath<P> {
4040

41-
private static final Converter<PersistentProperty<?>, String> DEFAULT_CONVERTER = (source) -> source.getName();
41+
private static final Converter<PersistentProperty<?>, String> DEFAULT_CONVERTER = PersistentProperty::getName;
4242
private static final String DEFAULT_DELIMITER = ".";
4343

4444
private final List<P> properties;
@@ -131,25 +131,43 @@ public P getBaseProperty() {
131131
}
132132

133133
public boolean isBasePathOf(PersistentPropertyPath<P> path) {
134+
return this.equals(getCommonBaseWith(path));
135+
}
134136

135-
Assert.notNull(path, "PersistentPropertyPath must not be null");
137+
/**
138+
* Return the common base path that this {@link PersistentPropertyPath} has with passed {@link PersistentPropertyPath}
139+
*
140+
* That is, for example, if this {@link PersistentPropertyPath} equals to one.two.three, and passed {@link PersistentPropertyPath}
141+
* is equals to one.two.four, that the return will be the {@link PersistentPropertyPath} containing one.two properties
142+
*/
143+
public PersistentPropertyPath<P> getCommonBaseWith(PersistentPropertyPath<P> anotherPath) {
144+
145+
Assert.notNull(anotherPath, "PersistentPropertyPath must not be null");
146+
147+
if (anotherPath.isEmpty()) {
148+
return DefaultPersistentPropertyPath.empty();
149+
}
136150

137-
Iterator<P> iterator = path.iterator();
151+
List<P> commonPart = newArrayList<>();
138152

139-
for (P property : this) {
153+
Iterator<P> iterator = anotherPath.iterator();
154+
155+
for (P property: this) {
140156

141157
if (!iterator.hasNext()) {
142-
returnfalse;
158+
break;
143159
}
144160

145-
P reference = iterator.next();
161+
P next = iterator.next();
146162

147-
if (!property.equals(reference)) {
148-
return false;
163+
if (property.equals(next)) {
164+
commonPart.add(property);
165+
} else {
166+
break;
149167
}
150168
}
151169

152-
return true;
170+
return newDefaultPersistentPropertyPath<>(commonPart);
153171
}
154172

155173
public PersistentPropertyPath<P> getExtensionForBaseOf(PersistentPropertyPath<P> base) {
@@ -158,18 +176,7 @@ public PersistentPropertyPath<P> getExtensionForBaseOf(PersistentPropertyPath<P>
158176
return this;
159177
}
160178

161-
List<P> result = new ArrayList<>();
162-
Iterator<P> iterator = iterator();
163-
164-
for (int i = 0; i < base.getLength(); i++) {
165-
iterator.next();
166-
}
167-
168-
while (iterator.hasNext()) {
169-
result.add(iterator.next());
170-
}
171-
172-
return new DefaultPersistentPropertyPath<>(result);
179+
return new DefaultPersistentPropertyPath<>(properties.subList(base.getLength(), properties.size()));
173180
}
174181

175182
public PersistentPropertyPath<P> getParentPath() {

‎src/test/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPathUnitTests.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.Arrays;
2323
import java.util.Collections;
24+
import java.util.List;
2425

2526
import org.junit.jupiter.api.BeforeEach;
2627
import org.junit.jupiter.api.Test;
@@ -40,7 +41,7 @@
4041
@ExtendWith(MockitoExtension.class)
4142
class DefaultPersistentPropertyPathUnitTests<P extends PersistentProperty<P>> {
4243

43-
@Mock P first, second;
44+
@Mock P first, second, third;
4445

4546
@Mock Converter<P, String> converter;
4647

@@ -58,6 +59,65 @@ void rejectsNullProperties() {
5859
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultPersistentPropertyPath<>(null));
5960
}
6061

62+
@Test
63+
void getCommonBaseTestEmpty() {
64+
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));
65+
66+
assertThat(propertyPath.getCommonBaseWith(DefaultPersistentPropertyPath.empty())).isEmpty();
67+
}
68+
69+
@Test
70+
void getCommonBaseTestNoCommonBase() {
71+
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first));
72+
73+
assertThat(propertyPath.getCommonBaseWith(new DefaultPersistentPropertyPath<>(List.of(second)))).isEmpty();
74+
}
75+
76+
@Test
77+
void getCommonBaseTestCommonBasePresentInPassed() {
78+
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first));
79+
80+
assertThat(propertyPath.getCommonBaseWith(new DefaultPersistentPropertyPath<>(List.of(first, second)))).isEqualTo(propertyPath);
81+
}
82+
83+
@Test
84+
void getCommonBaseTestCommonBasePresentInThis() {
85+
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));
86+
87+
DefaultPersistentPropertyPath<P> anotherPath = new DefaultPersistentPropertyPath<>(List.of(first));
88+
89+
assertThat(propertyPath.getCommonBaseWith(anotherPath)).isEqualTo(anotherPath);
90+
}
91+
92+
@Test
93+
void getCommonBaseTestTheSamePath() {
94+
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));
95+
96+
DefaultPersistentPropertyPath<P> anotherPath = new DefaultPersistentPropertyPath<>(List.of(first, second));
97+
98+
assertThat(propertyPath.getCommonBaseWith(anotherPath)).isEqualTo(anotherPath);
99+
}
100+
101+
@Test
102+
void getCommonBaseTestHasSamePropertiesInDifferentOrder() {
103+
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));
104+
105+
DefaultPersistentPropertyPath<P> anotherPath = new DefaultPersistentPropertyPath<>(List.of(second, first));
106+
107+
assertThat(propertyPath.getCommonBaseWith(anotherPath)).isEqualTo(DefaultPersistentPropertyPath.empty());
108+
}
109+
110+
111+
@Test
112+
void getCommonBaseTestHasSamePropertiesButNotInBase() {
113+
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));
114+
115+
DefaultPersistentPropertyPath<P> anotherPath = new DefaultPersistentPropertyPath<>(List.of(third, second));
116+
117+
assertThat(propertyPath.getCommonBaseWith(anotherPath)).isEqualTo(DefaultPersistentPropertyPath.empty());
118+
}
119+
120+
61121
@Test
62122
void usesPropertyNameForSimpleDotPath() {
63123

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /