DATACMNS-97 - Added PartTree.getParts(Type type).
Added method to allow iterating over Parts of a given type.
This commit is contained in:
@@ -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> {
|
||||
}
|
||||
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() {
|
||||
|
||||
@@ -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 {
|
||||
}
|
||||
}
|
||||
|
||||
@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 {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user