main.java.com.cloudant.client.api.views.ViewRequestBuilder Maven / Gradle / Ivy
Show all versions of cloudant-client Show documentation
/*
* Copyright (c) 2015 IBM Corp. All rights reserved.
*
* 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.cloudant.client.api.views;
import com.cloudant.client.api.CloudantClient;
import com.cloudant.client.api.Database;
import com.cloudant.client.internal.views.MultipleRequestBuilderImpl;
import com.cloudant.client.internal.views.PaginatedRequestBuilderImpl;
import com.cloudant.client.internal.views.UnpaginatedRequestBuilderImpl;
import com.cloudant.client.internal.views.ViewQueryParameters;
/**
* Provides methods for obtaining builders for view requests.
*
* The database, design document and view are specified when this class is instantiated.
*
*
* A view request builder instance should be obtained from a call to
* {@link Database#getViewRequestBuilder(String, String)}.
*
*/
public class ViewRequestBuilder {
private final CloudantClient client;
private final Database database;
private final String designDoc;
private final String viewName;
/**
*
* Create a new ViewRequestBuilder for the specified database, design document and view name.
*
*
* Rather than calling this constructor directly a view request builder instance should be
* obtained from a call to {@link Database#getViewRequestBuilder(String, String)}.
*
*
* @param client the cloudant client
* @param database the database
* @param designDoc the design doc containing the view (optionally prefixed with "_design/")
* @param viewName the view to build a query request for
*/
public ViewRequestBuilder(CloudantClient client, Database database, String designDoc, String
viewName) {
this.client = client;
this.database = database;
this.designDoc = designDoc;
this.viewName = viewName;
}
/**
* Create a new builder for an unpaginated request on the view.
*
* Note that the maximum number of rows returnable in an unpaginated request is
* {@link Integer#MAX_VALUE}. If you need to return more results than this then use
* {@link ViewRequestBuilder#newPaginatedRequest(Key.Type, Class)} to spread the rows across
* multiple pages.
*
*
* @param keyType {@link com.cloudant.client.api.views.Key.Type} of the key emitted by the
* view
* @param valueType class of the type of value emitted by the view
* @param type of key emitted by the view
* @param type of value emitted by the view
* @return a new {@link UnpaginatedRequestBuilder} for the database view specified by this
* ViewRequestBuilder
*/
public UnpaginatedRequestBuilder newRequest(Key.Type keyType, Class
valueType) {
return new UnpaginatedRequestBuilderImpl(newViewRequestParameters(keyType.getType(),
valueType));
}
/**
* Create a new builder for a paginated request on the view.
*
* Defaults to 20 rows per page if not specified by
* {@link PaginatedRequestBuilder#rowsPerPage(int)}
*
*
* @param keyType {@link com.cloudant.client.api.views.Key.Type} of the key emitted by the
* view
* @param valueType class of the type of value emitted by the view
* @param type of key emitted by the view
* @param type of value emitted by the view
* @return a new {@link PaginatedRequestBuilder} for the database view specified by this
* ViewRequestBuilder
*/
public PaginatedRequestBuilder newPaginatedRequest(Key.Type keyType,
Class valueType) {
return new PaginatedRequestBuilderImpl(newViewRequestParameters(keyType.getType(),
valueType));
}
/**
* Create a new builder for multiple unpaginated requests on the view.
*
* @param keyType {@link com.cloudant.client.api.views.Key.Type} of the key emitted by the
* view
* @param valueType class of the type of value emitted by the view
* @param type of key emitted by the view
* @param type of value emitted by the view
* @return a new {@link MultipleRequestBuilder} for the database view specified by this
* ViewRequestBuilder
*/
public MultipleRequestBuilder newMultipleRequest(Key.Type keyType,
Class valueType) {
return new MultipleRequestBuilderImpl(newViewRequestParameters(keyType.getType(),
valueType));
}
private ViewQueryParameters newViewRequestParameters(Class keyType, Class
valueType) {
return new ViewQueryParameters(client, database, designDoc, viewName, keyType,
valueType);
}
}