Skip to content

Commit

Permalink
Fixed incorrect docs PersistentPropertyPath and optimized sublist
Browse files Browse the repository at this point in the history
  • Loading branch information
mipo256 committed Feb 23, 2023
1 parent 34f212f commit 1cc7616
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends

/**
* Returns the dot based path notation using {@link PersistentProperty#getName()}.
*
* @return
*/
@Nullable
String toDotPath();
Expand All @@ -40,7 +38,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends
* {@link PersistentProperty}s to path segments.
*
* @param converter must not be {@literal null}.
* @return
*/
@Nullable
String toDotPath(Converter<? super P, String> converter);
Expand All @@ -49,7 +46,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends
* Returns a {@link String} path with the given delimiter based on the {@link PersistentProperty#getName()}.
*
* @param delimiter must not be {@literal null}.
* @return
*/
@Nullable
String toPath(String delimiter);
Expand All @@ -60,7 +56,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends
*
* @param delimiter must not be {@literal null}.
* @param converter must not be {@literal null}.
* @return
*/
@Nullable
String toPath(String delimiter, Converter<? super P, String> converter);
Expand All @@ -70,7 +65,6 @@ public interface PersistentPropertyPath<P extends PersistentProperty<P>> extends
* {@link PersistentProperty} for {@code bar}. For a simple {@code foo} it returns {@link PersistentProperty} for
* {@code foo}.
*
* @return
*/
@Nullable
P getLeafProperty();
Expand All @@ -90,44 +84,36 @@ default P getRequiredLeafProperty() {
* Returns the first property in the {@link PersistentPropertyPath}. So for {@code foo.bar} it will return the
* {@link PersistentProperty} for {@code foo}. For a simple {@code foo} it returns {@link PersistentProperty} for
* {@code foo}.
*
* @return
*/
@Nullable
P getBaseProperty();

/**
* Returns whether the given {@link PersistentPropertyPath} is a base path of the current one. This means that the
* current {@link PersistentPropertyPath} is basically an extension of the given one.
* given {@link PersistentPropertyPath} is basically an extension of this {@link PersistentPropertyPath}.
*
* @param path must not be {@literal null}.
* @return
*/
boolean isBasePathOf(PersistentPropertyPath<P> path);

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

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

/**
* Returns the length of the {@link PersistentPropertyPath}.
*
* @return
*/
int getLength();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
class DefaultPersistentPropertyPath<P extends PersistentProperty<P>> implements PersistentPropertyPath<P> {

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

private final List<P> properties;
Expand Down Expand Up @@ -131,25 +131,43 @@ public P getBaseProperty() {
}

public boolean isBasePathOf(PersistentPropertyPath<P> path) {
return this.equals(getCommonBaseWith(path));
}

Assert.notNull(path, "PersistentPropertyPath must not be null");
/**
* Return the common base path that this {@link PersistentPropertyPath} has with passed {@link PersistentPropertyPath}
*
* That is, for example, if this {@link PersistentPropertyPath} equals to one.two.three, and passed {@link PersistentPropertyPath}
* is equals to one.two.four, that the return will be the {@link PersistentPropertyPath} containing one.two properties
*/
public PersistentPropertyPath<P> getCommonBaseWith(PersistentPropertyPath<P> anotherPath) {

Assert.notNull(anotherPath, "PersistentPropertyPath must not be null");

if (anotherPath.isEmpty()) {
return DefaultPersistentPropertyPath.empty();
}

Iterator<P> iterator = path.iterator();
List<P> commonPart = new ArrayList<>();

for (P property : this) {
Iterator<P> iterator = anotherPath.iterator();

for (P property: this) {

if (!iterator.hasNext()) {
return false;
break;
}

P reference = iterator.next();
P next = iterator.next();

if (!property.equals(reference)) {
return false;
if (property.equals(next)) {
commonPart.add(property);
} else {
break;
}
}

return true;
return new DefaultPersistentPropertyPath<>(commonPart);
}

public PersistentPropertyPath<P> getExtensionForBaseOf(PersistentPropertyPath<P> base) {
Expand All @@ -158,18 +176,7 @@ public PersistentPropertyPath<P> getExtensionForBaseOf(PersistentPropertyPath<P>
return this;
}

List<P> result = new ArrayList<>();
Iterator<P> iterator = iterator();

for (int i = 0; i < base.getLength(); i++) {
iterator.next();
}

while (iterator.hasNext()) {
result.add(iterator.next());
}

return new DefaultPersistentPropertyPath<>(result);
return new DefaultPersistentPropertyPath<>(properties.subList(base.getLength(), properties.size()));
}

public PersistentPropertyPath<P> getParentPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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

@Mock P first, second;
@Mock P first, second, third;

@Mock Converter<P, String> converter;

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

@Test
void getCommonBaseTestEmpty() {
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));

assertThat(propertyPath.getCommonBaseWith(DefaultPersistentPropertyPath.empty())).isEmpty();
}

@Test
void getCommonBaseTestNoCommonBase() {
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first));

assertThat(propertyPath.getCommonBaseWith(new DefaultPersistentPropertyPath<>(List.of(second)))).isEmpty();
}

@Test
void getCommonBaseTestCommonBasePresentInPassed() {
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first));

assertThat(propertyPath.getCommonBaseWith(new DefaultPersistentPropertyPath<>(List.of(first, second)))).isEqualTo(propertyPath);
}

@Test
void getCommonBaseTestCommonBasePresentInThis() {
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));

DefaultPersistentPropertyPath<P> anotherPath = new DefaultPersistentPropertyPath<>(List.of(first));

assertThat(propertyPath.getCommonBaseWith(anotherPath)).isEqualTo(anotherPath);
}

@Test
void getCommonBaseTestTheSamePath() {
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));

DefaultPersistentPropertyPath<P> anotherPath = new DefaultPersistentPropertyPath<>(List.of(first, second));

assertThat(propertyPath.getCommonBaseWith(anotherPath)).isEqualTo(anotherPath);
}

@Test
void getCommonBaseTestHasSamePropertiesInDifferentOrder() {
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));

DefaultPersistentPropertyPath<P> anotherPath = new DefaultPersistentPropertyPath<>(List.of(second, first));

assertThat(propertyPath.getCommonBaseWith(anotherPath)).isEqualTo(DefaultPersistentPropertyPath.empty());
}


@Test
void getCommonBaseTestHasSamePropertiesButNotInBase() {
DefaultPersistentPropertyPath<P> propertyPath = new DefaultPersistentPropertyPath<>(List.of(first, second));

DefaultPersistentPropertyPath<P> anotherPath = new DefaultPersistentPropertyPath<>(List.of(third, second));

assertThat(propertyPath.getCommonBaseWith(anotherPath)).isEqualTo(DefaultPersistentPropertyPath.empty());
}


@Test
void usesPropertyNameForSimpleDotPath() {

Expand Down

0 comments on commit 1cc7616

Please sign in to comment.