Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011-2013 Brian Thomas Matthews
   *
   * 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.btmatthews.atlas.core.common;
 
 
 import java.util.List;

A builder for creating and modifying Paging objects.

Author(s):
Brian Thomas Matthews
Since:
1.0.0
 
 public final class PagingBuilder {

    
The page number used when constructing the Paging object.
 
     private int pageNumber = 0;
    
The page size used when constructing the Paging object.
 
     private int pageSize = 1;
    
The sort orderings used when constructing the Paging object.
 
     private List<OrderingsortOrderings = new ArrayList<Ordering>();

    
The constructor used when we need to create a new Paging object from scratch.
 
     public PagingBuilder() {
     }

    
The constructor used when we need to modify an existing Paging object.

Parameters:
paging The paging object.
 
     public PagingBuilder(final Paging paging) {
          = paging.getPageNumber();
          = paging.getPageSize();
         .addAll(paging.getSortOrderings());
     }

    
Construct a predicate that can be used to find a sort ordering by the field name.

Parameters:
name The sort field name.
Returns:
A com.google.common.base.Predicate object.
 
     private static Predicate<OrderingfindOrdering(final String name) {
         return new Predicate<Ordering>() {
             @Override
             public boolean apply(final Ordering ordering) {
                 return ordering.getSortField().equals(name);
             }
         };
     }

    
Set the page number.

Parameters:
number The page number.
Returns:
Always returns the object.
 
     public PagingBuilder setPageNumber(final int number) {
          = number;
         return this;
     }

    
Set the page size.

Parameters:
size The page size.
Returns:
Always returns the PagingBuilder object.
 
     public PagingBuilder setPageSize(final int size) {
          = size;
         return this;
     }

    
Promote a sort ordering moving it up one position.

Parameters:
name The sort field name.
Returns:
Always returns the PagingBuilder object.
    public PagingBuilder promoteOrdering(final String name) {
        final int index = Iterables.indexOf(findOrdering(name));
        if (index > 0) {
            final Ordering temp = .get(index - 1);
            .set(index - 1, .get(index));
            .set(indextemp);
        }
        return this;
    }

    
Demote a sort ordering moving it down one position.

Parameters:
name The sort field name.
Returns:
Always returns the PagingBuilder object.
    public PagingBuilder demoteOrdering(final String name) {
        final int index = Iterables.indexOf(findOrdering(name));
        if (index >= 0 && index < .size() - 1) {
            final Ordering temp = .get(index + 1);
            .set(index + 1, .get(index));
            .set(indextemp);
        }
        return this;
    }

    
Add a sort ordering.

Parameters:
name The sort field name.
direction The sort field direction.
Returns:
Always returns the PagingBuilder object.
    public PagingBuilder addOrdering(final String namefinal SortDirection direction) {
        final Ordering ordering = new Ordering(namedirection);
        .add(ordering);
        return this;
    }

    
Toggle the direction of the sort ordering.

Parameters:
field The field name.
Returns:
Always returns the PagingBuilder object.
    public PagingBuilder toggleOrdering(final String field) {
        final int index = Iterables.indexOf(findOrdering(field));
        if (index == -1) {
            addOrdering(field.);
        } else {
            if (.get(index).getSortDirection() == .) {
                .set(indexnew Ordering(field.));
            } else {
                .set(indexnew Ordering(field.));
            }
        }
        return this;
    }

    
Remove the sort ordering.

Parameters:
field The field name.
Returns:
Always returns the PagingBuilder object.
    public PagingBuilder removeOrdering(final String field) {
        Iterables.removeIf(findOrdering(field));
        return this;
    }

    
Construct the immutable Paging object from the builder configuration.

Returns:
The Paging object.
    public Paging build() {
        return new Paging(.toArray(new Ordering[0]));
    }
New to GrepCode? Check out our FAQ X