[New] implemented Parameter selection with test.

The ParameterizedElementSelector was still missing.
This commit is contained in:
Robert von Burg 2013-11-29 08:42:44 +01:00
parent a141744068
commit 925a25657a
2 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package li.strolch.runtime.query.inmemory;
import java.util.List;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.ParameterBag;
import li.strolch.model.ParameterizedElement;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class ParameterizedElementSelector<T extends GroupedParameterizedElement> implements Selector<T> {
private String key;
private List<Selector<ParameterizedElement>> selectors;
public ParameterizedElementSelector(String key, List<Selector<ParameterizedElement>> selectors) {
this.key = key;
this.selectors = selectors;
}
@Override
public boolean select(T element) {
ParameterBag parameterBag = element.getParameterBag(this.key);
if (parameterBag == null)
return false;
for (Selector<ParameterizedElement> selector : this.selectors) {
if (!selector.select(parameterBag))
return false;
}
return true;
}
}

View File

@ -28,8 +28,13 @@ import java.util.Date;
import java.util.List;
import li.strolch.model.Order;
import li.strolch.model.ParameterBag;
import li.strolch.model.ParameterizedElement;
import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.parameter.FloatParameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.runtime.ModelBuilder;
import org.junit.Test;
@ -121,6 +126,34 @@ public class InMemoryQueryTest {
assertEquals(0, result.size());
}
@Test
public void shouldQueryByParameter() {
List<Resource> resources = getResources();
resources.add(getBallResource());
InMemoryQuery<Resource> ballQuery = new InMemoryQuery<>();
ballQuery.setNavigator(new ListNavigator<>(resources));
List<Selector<ParameterizedElement>> ballDetailSelectors = new ArrayList<>();
ballDetailSelectors.add(ParameterSelector.stringSelector("color", "red"));
ballDetailSelectors.add(ParameterSelector.booleanSelector("forChildren", true));
ballDetailSelectors.add(ParameterSelector.floatSelector("diameter", 22.0));
ballQuery.addSelector(new ParameterizedElementSelector<Resource>("parameters", ballDetailSelectors));
List<Resource> result = ballQuery.doQuery();
assertEquals(1, result.size());
}
private Resource getBallResource() {
Resource res1 = new Resource("childrensBall", "Ball 1", "Ball");
ParameterBag bag = new ParameterBag("parameters", "Ball Details", "Parameters");
bag.addParameter(new StringParameter("color", "Color", "red"));
bag.addParameter(new BooleanParameter("forChildren", "Color", true));
bag.addParameter(new FloatParameter("diameter", "Color", 22.0));
res1.addParameterBag(bag);
return res1;
}
private List<Resource> getResources() {
Resource res1 = ModelBuilder.createResource("@1", "Res 1", "MyType1");
Resource res2 = ModelBuilder.createResource("@2", "Res 2", "MyType1");