From 904578d034b404230b88abffbda6c4e5534ea78e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 21 Nov 2011 16:02:43 +0100 Subject: [PATCH] DATACMNS-97 - Added PartTree.getParts(Type type). Added method to allow iterating over Parts of a given type. --- .../repository/query/parser/PartTree.java | 20 ++++++++++ .../query/parser/PartTreeUnitTests.java | 37 +++++++++++++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java index 018045958..37efc4c65 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java @@ -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 { } return result; } + + /** + * Returns all {@link Part}s of the {@link PartTree} of the given {@link Type}. + * + * @param type + * @return + */ + public Iterable getParts(Type type) { + + List result = new ArrayList(); + + for (Part part : getParts()) { + if (part.getType().equals(type)) { + result.add(part); + } + } + + return result; + } @Override public String toString() { diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java index 2e50e5c9b..d33245943 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java @@ -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 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 Collection toCollection(Iterable iterable) { + + List result = new ArrayList(); + for (T element : iterable) { + result.add(element); + } + return result; + } + class User { String firstname; String lastname;