Browse Source

DATACMNS-97 - Added PartTree.getParts(Type type).

Added method to allow iterating over Parts of a given type.
pull/13/merge
Oliver Gierke 14 years ago
parent
commit
904578d034
  1. 20
      spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java
  2. 37
      spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java

20
spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java

@ -22,6 +22,7 @@ import java.util.regex.Matcher; @@ -22,6 +22,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.parser.Part.Type;
import org.springframework.data.repository.query.parser.PartTree.OrPart;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@ -115,6 +116,25 @@ public class PartTree implements Iterable<OrPart> { @@ -115,6 +116,25 @@ public class PartTree implements Iterable<OrPart> {
}
return result;
}
/**
* Returns all {@link Part}s of the {@link PartTree} of the given {@link Type}.
*
* @param type
* @return
*/
public Iterable<Part> getParts(Type type) {
List<Part> result = new ArrayList<Part>();
for (Part part : getParts()) {
if (part.getType().equals(type)) {
result.add(part);
}
}
return result;
}
@Override
public String toString() {

37
spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java

@ -15,12 +15,13 @@ @@ -15,12 +15,13 @@
*/
package org.springframework.data.repository.query.parser;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import org.springframework.data.domain.Sort;
@ -211,6 +212,27 @@ public class PartTreeUnitTests { @@ -211,6 +212,27 @@ public class PartTreeUnitTests {
}
}
@Test
public void returnsAllParts() {
PartTree tree = partTree("findByLastnameAndFirstname");
assertPart(tree, parts("lastname", "firstname"));
}
@Test
public void returnsAllPartsOfType() {
PartTree tree = partTree("findByLastnameAndFirstnameGreaterThan");
Collection<Part> parts = toCollection(tree.getParts(Type.SIMPLE_PROPERTY));
assertThat(parts, hasItem(part("lastname")));
assertThat(parts, is(hasSize(1)));
parts = toCollection(tree.getParts(Type.GREATER_THAN));
assertThat(parts, hasItem(new Part("FirstnameGreaterThan", User.class)));
assertThat(parts, is(hasSize(1)));
}
private PartTree partTree(String source) {
return new PartTree(source, User.class);
}
@ -246,6 +268,15 @@ public class PartTreeUnitTests { @@ -246,6 +268,15 @@ public class PartTreeUnitTests {
assertThat("Too many or parts!", iterator.hasNext(), is(false));
}
private static <T> Collection<T> toCollection(Iterable<T> iterable) {
List<T> result = new ArrayList<T>();
for (T element : iterable) {
result.add(element);
}
return result;
}
class User {
String firstname;
String lastname;

Loading…
Cancel
Save