org.apache.juneau.utils.SearchArgs Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you 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 org.apache.juneau.utils;
import static java.util.Collections.*;
import static org.apache.juneau.internal.StringUtils.*;
import java.util.*;
import org.apache.juneau.*;
import org.apache.juneau.internal.*;
/**
* Encapsulates arguments for basic search/view/sort/position/limit functionality.
*/
public class SearchArgs {
private final Map search;
private final List view;
private final Map sort;
private final int position, limit;
private final boolean ignoreCase;
private SearchArgs(Builder b) {
this.search = unmodifiableMap(new LinkedHashMap(b.search));
this.view = unmodifiableList(new ArrayList(b.view));
this.sort = unmodifiableMap(new LinkedHashMap(b.sort));
this.position = b.position;
this.limit = b.limit;
this.ignoreCase = b.ignoreCase;
}
/**
* Creates a new builder for {@link SearchArgs}
*
* @return A new builder for {@link SearchArgs}
*/
public static Builder builder() {
return new Builder();
}
/**
* Builder for {@link SearchArgs} class.
*/
public static class Builder {
Map search = new LinkedHashMap();
List view = new ArrayList();
Map sort = new LinkedHashMap();
int position, limit;
boolean ignoreCase;
/**
* Adds search terms to this builder.
*
*
* The search terms are a comma-delimited list of key/value pairs of column-names and search tokens.
*
*
* For example:
*
* builder.search("column1=foo*, column2=bar baz" );
*
*
*
* It's up to implementers to decide the syntax and meaning of the search terms.
*
*
* Whitespace is trimmed from column names and search tokens.
*
* @param searchTerms
* The search terms string.
* Can be null .
* @return This object (for method chaining).
*/
public Builder search(String searchTerms) {
if (searchTerms != null) {
for (String s : StringUtils.split(searchTerms)) {
int i = StringUtils.indexOf(s, '=', '>', '<');
if (i == -1)
throw new FormattedRuntimeException("Invalid search terms: ''{0}''", searchTerms);
char c = s.charAt(i);
search(s.substring(0, i).trim(), s.substring(c == '=' ? i+1 : i).trim());
}
}
return this;
}
/**
* Adds a search term to this builder.
*
*
* It's up to implementers to decide the syntax and meaning of the search term.
*
* @param column The column being searched.
* @param searchTerm The search term.
* @return This object (for method chaining).
*/
public Builder search(String column, String searchTerm) {
this.search.put(column, searchTerm);
return this;
}
/**
* Specifies the list of columns to view.
*
*
* The columns argument is a simple comma-delimited list of column names.
*
*
* For example:
*
* builder.view("column1, column2" );
*
*
*
* Whitespace is trimmed from column names.
*
*
* Empty view columns imply view all columns.
*
* @param columns
* The columns being viewed.
* Can be null .
* @return This object (for method chaining).
*/
public Builder view(String columns) {
if (columns != null)
return view(Arrays.asList(StringUtils.split(columns)));
return this;
}
/**
* Specifies the list of columns to view.
*
*
* Empty view columns imply view all columns.
*
* @param columns The columns being viewed.
* @return This object (for method chaining).
*/
public Builder view(Collection columns) {
this.view.addAll(columns);
return this;
}
/**
* Specifies the sort arguments.
*
*
* The sort argument is a simple comma-delimited list of column names.
*
Column names can be suffixed with '+' or '-' to indicate ascending or descending order.
*
No suffix implies ascending order.
*
*
* For example:
*
* // Order by column1 ascending, then column2 descending.
* builder.sort("column1, column2-" );
*
*
*
* Note that the order of the order arguments is important.
*
*
* Whitespace is trimmed from column names.
*
* @param sortArgs
* The columns to sort by.
* Can be null .
* @return This object (for method chaining).
*/
public Builder sort(String sortArgs) {
if (sortArgs != null)
sort(Arrays.asList(StringUtils.split(sortArgs)));
return this;
}
/**
* Specifies the sort arguments.
*
*
* Column names can be suffixed with '+' or '-' to indicate ascending or descending order.
*
No suffix implies ascending order.
*
*
* Note that the order of the sort is important.
*
* @param sortArgs
* The columns to sort by.
* Can be null .
* @return This object (for method chaining).
*/
public Builder sort(Collection sortArgs) {
for (String s : sortArgs) {
boolean isDesc = false;
if (endsWith(s, '-', '+')) {
isDesc = endsWith(s, '-');
s = s.substring(0, s.length()-1);
}
this.sort.put(s, isDesc);
}
return this;
}
/**
* Specifies the starting line number.
*
* @param position The zero-indexed position.
* @return This object (for method chaining).
*/
public Builder position(int position) {
this.position = position;
return this;
}
/**
* Specifies the number of rows to return.
*
* @param limit
* The number of rows to return.
* If <=0
, all rows should be returned.
* @return This object (for method chaining).
*/
public Builder limit(int limit) {
this.limit = limit;
return this;
}
/**
* Specifies whether case-insensitive search should be used.
*
*
* The default is false .
*
* @param value The ignore-case flag value.
* @return This object (for method chaining).
*/
public Builder ignoreCase(boolean value) {
this.ignoreCase = value;
return this;
}
/**
* Construct the {@link SearchArgs} object.
*
*
* This method can be called multiple times to construct new objects.
*
* @return A new {@link SearchArgs} object initialized with values in this builder.
*/
public SearchArgs build() {
return new SearchArgs(this);
}
}
/**
* The query search terms.
*
*
* The search terms are key/value pairs consisting of column-names and search tokens.
*
*
* It's up to implementers to decide the syntax and meaning of the search term.
*
* @return An unmodifiable map of query search terms.
*/
public Map getSearch() {
return search;
}
/**
* The view columns.
*
*
* The view columns are the list of columns that should be displayed.
* An empty list implies all columns should be displayed.
*
* @return An unmodifiable list of columns to view.
*/
public List getView() {
return view;
}
/**
* The sort columns.
*
*
* The sort columns are key/value pairs consisting of column-names and direction flags
* (false = ascending, true = descending).
*
* @return An unmodifiable ordered map of sort columns and directions.
*/
public Map getSort() {
return sort;
}
/**
* The first-row position.
*
* @return
* The zero-indexed row number of the first row to display.
* Default is 0
*/
public int getPosition() {
return position;
}
/**
* The number of rows to return.
*
* @return
* The number of rows to return in the result.
* Default is 0
which means return all rows.
*/
public int getLimit() {
return limit;
}
/**
* The ignore-case flag.
*
*
* Used in conjunction with {@link #getSearch()} to specify whether case-insensitive searches should be performed.
*
* @return
* The number of rows to return in the result.
* Default is false .
*/
public boolean isIgnoreCase() {
return ignoreCase;
}
}