Permalink
Browse files

Merge pull request #41 from gcheng/dev-bookmark

Making filter immutable, add filter to TableQuery, Rename RawStringFilte...
  • Loading branch information...
2 parents 640093e + 4d82414 commit 9cafac5a666e1384242602d5ff5a3fd3efe619c0 Albert Cheng committed Apr 9, 2012
@@ -75,13 +75,12 @@
import com.microsoft.windowsazure.services.table.models.GetServicePropertiesResult;
import com.microsoft.windowsazure.services.table.models.GetTableResult;
import com.microsoft.windowsazure.services.table.models.InsertEntityResult;
-import com.microsoft.windowsazure.services.table.models.LitteralFilter;
-import com.microsoft.windowsazure.services.table.models.Query;
+import com.microsoft.windowsazure.services.table.models.PropertyNameFilter;
import com.microsoft.windowsazure.services.table.models.QueryEntitiesOptions;
import com.microsoft.windowsazure.services.table.models.QueryEntitiesResult;
+import com.microsoft.windowsazure.services.table.models.QueryStringFilter;
import com.microsoft.windowsazure.services.table.models.QueryTablesOptions;
import com.microsoft.windowsazure.services.table.models.QueryTablesResult;
-import com.microsoft.windowsazure.services.table.models.RawStringFilter;
import com.microsoft.windowsazure.services.table.models.ServiceProperties;
import com.microsoft.windowsazure.services.table.models.TableServiceOptions;
import com.microsoft.windowsazure.services.table.models.UnaryFilter;
@@ -185,26 +184,29 @@ private WebResource addOptionalQueryParam(WebResource webResource, String key, O
return PipelineHelpers.addOptionalQueryParam(webResource, key, value);
}
- private WebResource addOptionalQuery(WebResource webResource, Query query) {
- if (query == null)
+ private WebResource addOptionalQueryEntitiesOptions(WebResource webResource,
+ QueryEntitiesOptions queryEntitiesOptions) {
+ if (queryEntitiesOptions == null)
return webResource;
- if (query.getSelectFields() != null && query.getSelectFields().size() > 0) {
+ if (queryEntitiesOptions.getSelectFields() != null && queryEntitiesOptions.getSelectFields().size() > 0) {
webResource = addOptionalQueryParam(webResource, "$select",
- CommaStringBuilder.join(encodeODataURIValues(query.getSelectFields())));
+ CommaStringBuilder.join(encodeODataURIValues(queryEntitiesOptions.getSelectFields())));
}
- if (query.getTop() != null) {
- webResource = addOptionalQueryParam(webResource, "$top", encodeODataURIValue(query.getTop().toString()));
+ if (queryEntitiesOptions.getTop() != null) {
+ webResource = addOptionalQueryParam(webResource, "$top", encodeODataURIValue(queryEntitiesOptions.getTop()
+ .toString()));
}
- if (query.getFilter() != null) {
- webResource = addOptionalQueryParam(webResource, "$filter", buildFilterExpression(query.getFilter()));
+ if (queryEntitiesOptions.getFilter() != null) {
+ webResource = addOptionalQueryParam(webResource, "$filter",
+ buildFilterExpression(queryEntitiesOptions.getFilter()));
}
- if (query.getOrderByFields() != null) {
+ if (queryEntitiesOptions.getOrderByFields() != null) {
webResource = addOptionalQueryParam(webResource, "$orderby",
- CommaStringBuilder.join(encodeODataURIValues(query.getOrderByFields())));
+ CommaStringBuilder.join(encodeODataURIValues(queryEntitiesOptions.getOrderByFields())));
}
return webResource;
@@ -220,8 +222,8 @@ private void buildFilterExpression(Filter filter, StringBuilder sb) {
if (filter == null)
return;
- if (filter instanceof LitteralFilter) {
- sb.append(((LitteralFilter) filter).getLitteral());
+ if (filter instanceof PropertyNameFilter) {
+ sb.append(((PropertyNameFilter) filter).getPropertyName());
}
else if (filter instanceof ConstantFilter) {
Object value = ((ConstantFilter) filter).getValue();
@@ -285,8 +287,8 @@ else if (filter instanceof BinaryFilter) {
buildFilterExpression(((BinaryFilter) filter).getRight(), sb);
sb.append(")");
}
- else if (filter instanceof RawStringFilter) {
- sb.append(((RawStringFilter) filter).getRawString());
+ else if (filter instanceof QueryStringFilter) {
+ sb.append(((QueryStringFilter) filter).getQueryString());
}
}
@@ -383,19 +385,25 @@ public QueryTablesResult queryTables() throws ServiceException {
@Override
public QueryTablesResult queryTables(QueryTablesOptions options) throws ServiceException {
- Query query = new Query();
+ Filter queryFilter = options.getFilter();
String nextTableName = options.getNextTableName();
String prefix = options.getPrefix();
if (prefix != null) {
// Append Max char to end '{' is 1 + 'z' in AsciiTable ==> upperBound is prefix + '{'
- Filter prefixFilter = Filter.and(Filter.ge(Filter.litteral("TableName"), Filter.constant(prefix)),
- Filter.le(Filter.litteral("TableName"), Filter.constant(prefix + "{")));
- query.setFilter(prefixFilter);
+ Filter prefixFilter = Filter.and(Filter.ge(Filter.propertyName("TableName"), Filter.constant(prefix)),
+ Filter.le(Filter.propertyName("TableName"), Filter.constant(prefix + "{")));
+
+ if (queryFilter == null) {
+ queryFilter = prefixFilter;
+ }
+ else {
+ queryFilter = Filter.and(queryFilter, prefixFilter);
+ }
}
WebResource webResource = getResource(options).path("Tables");
- webResource = addOptionalQuery(webResource, query);
+ webResource = addOptionalQueryParam(webResource, "$filter", buildFilterExpression(queryFilter));
webResource = addOptionalQueryParam(webResource, "NextTableName", nextTableName);
WebResource.Builder builder = webResource.getRequestBuilder();
@@ -613,7 +621,7 @@ public QueryEntitiesResult queryEntities(String table, QueryEntitiesOptions opti
options = new QueryEntitiesOptions();
WebResource webResource = getResource(options).path(table);
- webResource = addOptionalQuery(webResource, options.getQuery());
+ webResource = addOptionalQueryEntitiesOptions(webResource, options);
webResource = addOptionalQueryParam(webResource, "NextPartitionKey",
encodeODataURIValue(options.getNextPartitionKey()));
webResource = addOptionalQueryParam(webResource, "NextRowKey", encodeODataURIValue(options.getNextRowKey()));
@@ -15,34 +15,26 @@
package com.microsoft.windowsazure.services.table.models;
public class BinaryFilter extends Filter {
- private String operator;
- private Filter left;
- private Filter right;
+ private final String operator;
+ private final Filter left;
+ private final Filter right;
- public String getOperator() {
- return operator;
+ public BinaryFilter(Filter left, String operator, Filter right) {
+ this.left = left;
+ this.operator = operator;
+ this.right = right;
}
- public BinaryFilter setOperator(String operator) {
- this.operator = operator;
- return this;
+ public String getOperator() {
+ return operator;
}
public Filter getLeft() {
return left;
}
- public BinaryFilter setLeft(Filter left) {
- this.left = left;
- return this;
- }
-
public Filter getRight() {
return right;
}
- public BinaryFilter setRight(Filter right) {
- this.right = right;
- return this;
- }
}
@@ -15,14 +15,13 @@
package com.microsoft.windowsazure.services.table.models;
public class ConstantFilter extends Filter {
- private Object value;
+ private final Object value;
- public Object getValue() {
- return value;
+ public ConstantFilter(Object value) {
+ this.value = value;
}
- public ConstantFilter setValue(Object value) {
- this.value = value;
- return this;
+ public Object getValue() {
+ return value;
}
}
@@ -16,50 +16,50 @@
public class Filter {
public static UnaryFilter not(Filter operand) {
- return new UnaryFilter().setOperator("not").setOperand(operand);
+ return new UnaryFilter("not", operand);
}
public static BinaryFilter and(Filter left, Filter right) {
- return new BinaryFilter().setOperator("and").setLeft(left).setRight(right);
+ return new BinaryFilter(left, "and", right);
}
public static BinaryFilter or(Filter left, Filter right) {
- return new BinaryFilter().setOperator("or").setLeft(left).setRight(right);
+ return new BinaryFilter(left, "or", right);
}
public static BinaryFilter eq(Filter left, Filter right) {
- return new BinaryFilter().setOperator("eq").setLeft(left).setRight(right);
+ return new BinaryFilter(left, "eq", right);
}
public static BinaryFilter ne(Filter left, Filter right) {
- return new BinaryFilter().setOperator("ne").setLeft(left).setRight(right);
+ return new BinaryFilter(left, "ne", right);
}
public static BinaryFilter ge(Filter left, Filter right) {
- return new BinaryFilter().setOperator("ge").setLeft(left).setRight(right);
+ return new BinaryFilter(left, "ge", right);
}
public static BinaryFilter gt(Filter left, Filter right) {
- return new BinaryFilter().setOperator("gt").setLeft(left).setRight(right);
+ return new BinaryFilter(left, "gt", right);
}
public static BinaryFilter lt(Filter left, Filter right) {
- return new BinaryFilter().setOperator("lt").setLeft(left).setRight(right);
+ return new BinaryFilter(left, "lt", right);
}
public static BinaryFilter le(Filter left, Filter right) {
- return new BinaryFilter().setOperator("le").setLeft(left).setRight(right);
+ return new BinaryFilter(left, "le", right);
}
public static ConstantFilter constant(Object value) {
- return new ConstantFilter().setValue(value);
+ return new ConstantFilter(value);
}
- public static LitteralFilter litteral(String value) {
- return new LitteralFilter().setLitteral(value);
+ public static PropertyNameFilter propertyName(String value) {
+ return new PropertyNameFilter(value);
}
- public static RawStringFilter rawString(String value) {
- return new RawStringFilter().setRawString(value);
+ public static QueryStringFilter queryString(String value) {
+ return new QueryStringFilter(value);
}
}
@@ -14,15 +14,15 @@
*/
package com.microsoft.windowsazure.services.table.models;
-public class RawStringFilter extends Filter {
- private String rawString;
+public class PropertyNameFilter extends Filter {
+ private final String propertyName;
- public String getRawString() {
- return rawString;
+ public PropertyNameFilter(String propertyName) {
+ this.propertyName = propertyName;
}
- public RawStringFilter setRawString(String rawString) {
- this.rawString = rawString;
- return this;
+ public String getPropertyName() {
+ return propertyName;
}
+
}
@@ -1,81 +0,0 @@
-/**
- * Copyright 2012 Microsoft Corporation
- *
- * 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 com.microsoft.windowsazure.services.table.models;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class Query {
- private List<String> selectFields = new ArrayList<String>();
- private String from;
- private Filter filter;
- private List<String> orderByFields = new ArrayList<String>();
- private Integer top;
-
- public List<String> getSelectFields() {
- return selectFields;
- }
-
- public Query setSelectFields(List<String> selectFields) {
- this.selectFields = selectFields;
- return this;
- }
-
- public Query addSelectField(String selectField) {
- this.selectFields.add(selectField);
- return this;
- }
-
- public String getFrom() {
- return from;
- }
-
- public Query setFrom(String from) {
- this.from = from;
- return this;
- }
-
- public Filter getFilter() {
- return filter;
- }
-
- public Query setFilter(Filter filter) {
- this.filter = filter;
- return this;
- }
-
- public List<String> getOrderByFields() {
- return orderByFields;
- }
-
- public Query setOrderByFields(List<String> orderByFields) {
- this.orderByFields = orderByFields;
- return this;
- }
-
- public Query addOrderByField(String orderByField) {
- this.orderByFields.add(orderByField);
- return this;
- }
-
- public Integer getTop() {
- return top;
- }
-
- public Query setTop(Integer top) {
- this.top = top;
- return this;
- }
-}
Oops, something went wrong.

0 comments on commit 9cafac5

Please sign in to comment.