From 1f986797d483184ea189e0f036e489ecc62db9fc Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 6 Aug 2014 16:33:10 +0200 Subject: [PATCH] [New] OrderQuery and ResourceQuery now require a visitor This visitor is used to transform the query result. In cases where transformation is not required, use the NoStrategyOrderVisitor or NoStrategyResourceVisitor. For convenience new static helper methods were: ResourceQuery.resourceQuery(Navigation) ResourceQuery.resourceQuery(String) ResourceQuery.resourceQuery(String, ResourceVisitor) OrderQuery.orderQuery(Navigation) OrderQuery.orderQuery(String) OrderQuery.orderQuery(String, ResourceVisitor) --- .../li/strolch/model/query/OrderQuery.java | 62 ++++++++++++++++- .../li/strolch/model/query/ResourceQuery.java | 68 ++++++++++++++++++- .../model/query/StrolchTypeNavigation.java | 1 - .../model/visitor/NoStrategyOrderVisitor.java | 30 ++++++++ .../visitor/NoStrategyResourceVisitor.java | 30 ++++++++ .../model/visitor/NoStrategyVisitor.java | 29 ++++++++ 6 files changed, 215 insertions(+), 5 deletions(-) create mode 100644 src/main/java/li/strolch/model/visitor/NoStrategyOrderVisitor.java create mode 100644 src/main/java/li/strolch/model/visitor/NoStrategyResourceVisitor.java create mode 100644 src/main/java/li/strolch/model/visitor/NoStrategyVisitor.java diff --git a/src/main/java/li/strolch/model/query/OrderQuery.java b/src/main/java/li/strolch/model/query/OrderQuery.java index a98ca4f08..0863781e9 100644 --- a/src/main/java/li/strolch/model/query/OrderQuery.java +++ b/src/main/java/li/strolch/model/query/OrderQuery.java @@ -15,12 +15,70 @@ */ package li.strolch.model.query; +import li.strolch.model.Order; +import li.strolch.model.OrderVisitor; +import li.strolch.model.parameter.Parameter; +import li.strolch.model.visitor.NoStrategyOrderVisitor; + /** + * {@link OrderQuery} is the user API to query {@link Order Orders} in Strolch. The {@link Navigation} is used to + * navigate to a type of order on which any further {@link Selection Selections} will be performed. The + * {@link OrderVisitor} is used to transform the returned object into a domain specific object (if required). This + * mechanism allows you to query only the values of a {@link Parameter} instead of having to return all the elements and + * then performing this transformation. + * * @author Robert von Burg */ -public class OrderQuery extends StrolchQuery { +public class OrderQuery extends StrolchQuery { - public OrderQuery(Navigation navigation) { + private OrderVisitor elementVisitor; + + public OrderQuery(Navigation navigation, OrderVisitor elementVisitor) { super(navigation); + this.elementVisitor = elementVisitor; + } + + /** + * @return the elementVisitor + */ + public OrderVisitor getElementVisitor() { + return this.elementVisitor; + } + + /** + * Returns an instance of {@link OrderQuery} where the visitor used is the {@link NoStrategyOrderVisitor} thus + * returning the actual Order, i.e. no transformation is performed + * + * @param navigation + * @return + */ + public static OrderQuery orderQuery(Navigation navigation) { + return new OrderQuery(navigation, new NoStrategyOrderVisitor()); + } + + /** + * Returns an instance of {@link OrderQuery} where the visitor used is the {@link NoStrategyOrderVisitor} thus + * returning the actual Order, i.e. no transformation is performed + * + * @param type + * the type of Order to navigate to + * @return + */ + public static OrderQuery orderQuery(String type) { + return new OrderQuery(new StrolchTypeNavigation(type), new NoStrategyOrderVisitor()); + } + + /** + * Returns an instance of {@link OrderQuery} using the given {@link OrderVisitor} thus performing the given + * transformation + * + * @param type + * the type of Order to navigate to + * @param orderVisitor + * the visitor to use for transformation + * @return + */ + public static OrderQuery orderQuery(String type, OrderVisitor orderVisitor) { + return new OrderQuery(new StrolchTypeNavigation(type), orderVisitor); } } diff --git a/src/main/java/li/strolch/model/query/ResourceQuery.java b/src/main/java/li/strolch/model/query/ResourceQuery.java index fbde73be5..5176fe08c 100644 --- a/src/main/java/li/strolch/model/query/ResourceQuery.java +++ b/src/main/java/li/strolch/model/query/ResourceQuery.java @@ -15,12 +15,76 @@ */ package li.strolch.model.query; +import li.strolch.model.Resource; +import li.strolch.model.ResourceVisitor; +import li.strolch.model.parameter.Parameter; +import li.strolch.model.visitor.NoStrategyResourceVisitor; + /** + * {@link ResourceQuery} is the user API to query {@link Resource Resources} in Strolch. The {@link Navigation} is used + * to navigate to a type of resource on which any further {@link Selection Selections} will be performed. The + * {@link ResourceVisitor} is used to transform the returned object into a domain specific object (if required). This + * mechanism allows you to query only the values of a {@link Parameter} instead of having to return all the elements and + * then performing this transformation. + * * @author Robert von Burg */ -public class ResourceQuery extends StrolchQuery { +public class ResourceQuery extends StrolchQuery { - public ResourceQuery(Navigation navigation) { + private ResourceVisitor elementVisitor; + + /** + * Create a new + * + * @param navigation + * @param elementVisitor + */ + public ResourceQuery(Navigation navigation, ResourceVisitor elementVisitor) { super(navigation); + this.elementVisitor = elementVisitor; + } + + /** + * @return the elementVisitor + */ + public ResourceVisitor getElementVisitor() { + return this.elementVisitor; + } + + /** + * Returns an instance of {@link ResourceQuery} where the visitor used is the {@link NoStrategyResourceVisitor} thus + * returning the actual Resource, i.e. no transformation is performed + * + * @param navigation + * @return + */ + public static ResourceQuery resourceQuery(Navigation navigation) { + return new ResourceQuery(navigation, new NoStrategyResourceVisitor()); + } + + /** + * Returns an instance of {@link ResourceQuery} where the visitor used is the {@link NoStrategyResourceVisitor} thus + * returning the actual Resource, i.e. no transformation is performed + * + * @param type + * the type of {@link Resource} to navigate to + * @return + */ + public static ResourceQuery resourceQuery(String type) { + return new ResourceQuery(new StrolchTypeNavigation(type), new NoStrategyResourceVisitor()); + } + + /** + * Returns an instance of {@link ResourceQuery} using the given {@link ResourceVisitor} thus performing the given + * transformation + * + * @param type + * the type of Order to navigate to + * @param resourceVisitor + * the visitor to use for transformation + * @return + */ + public static ResourceQuery resourceQuery(String type, ResourceVisitor resourceVisitor) { + return new ResourceQuery(new StrolchTypeNavigation(type), resourceVisitor); } } diff --git a/src/main/java/li/strolch/model/query/StrolchTypeNavigation.java b/src/main/java/li/strolch/model/query/StrolchTypeNavigation.java index 32b23f84a..a4f7c9fa2 100644 --- a/src/main/java/li/strolch/model/query/StrolchTypeNavigation.java +++ b/src/main/java/li/strolch/model/query/StrolchTypeNavigation.java @@ -17,7 +17,6 @@ package li.strolch.model.query; /** * @author Robert von Burg - * */ public class StrolchTypeNavigation implements Navigation { diff --git a/src/main/java/li/strolch/model/visitor/NoStrategyOrderVisitor.java b/src/main/java/li/strolch/model/visitor/NoStrategyOrderVisitor.java new file mode 100644 index 000000000..df168fb8b --- /dev/null +++ b/src/main/java/li/strolch/model/visitor/NoStrategyOrderVisitor.java @@ -0,0 +1,30 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.visitor; + +import li.strolch.model.Order; +import li.strolch.model.OrderVisitor; + +/** + * @author Robert von Burg + */ +public class NoStrategyOrderVisitor implements OrderVisitor { + + @Override + public Order visit(Order element) { + return element; + } +} diff --git a/src/main/java/li/strolch/model/visitor/NoStrategyResourceVisitor.java b/src/main/java/li/strolch/model/visitor/NoStrategyResourceVisitor.java new file mode 100644 index 000000000..d319aa7b9 --- /dev/null +++ b/src/main/java/li/strolch/model/visitor/NoStrategyResourceVisitor.java @@ -0,0 +1,30 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.visitor; + +import li.strolch.model.Resource; +import li.strolch.model.ResourceVisitor; + +/** + * @author Robert von Burg + */ +public class NoStrategyResourceVisitor implements ResourceVisitor { + + @Override + public Resource visit(Resource element) { + return element; + } +} diff --git a/src/main/java/li/strolch/model/visitor/NoStrategyVisitor.java b/src/main/java/li/strolch/model/visitor/NoStrategyVisitor.java new file mode 100644 index 000000000..050cf634a --- /dev/null +++ b/src/main/java/li/strolch/model/visitor/NoStrategyVisitor.java @@ -0,0 +1,29 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.visitor; + +import li.strolch.model.StrolchElement; + +/** + * @author Robert von Burg + */ +public class NoStrategyVisitor implements StrolchElementVisitor { + + @Override + public T visit(T element) { + return element; + } +}