All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.ui.PageList Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.way.ui;

import static com.google.common.collect.Lists.newArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import br.com.objectos.way.relational.Page;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

/**
 * @author [email protected] (Marcio Endo)
 */
public class PageList {

  private final boolean empty;

  private final List rows;

  private final Pager pager;

  public PageList(List rows) {
    this.empty = rows.isEmpty();
    this.rows = rows;
    this.pager = new PagerJson(rows);
  }

  public PageList(List rows, Pager pager) {
    this.empty = rows.isEmpty();
    this.rows = rows;
    this.pager = pager;
  }

  public static  PageList of() {
    List list = ImmutableList.of();
    return new PageList(list);
  }

  public static  PageList of(Iterable rows) {
    List list = ImmutableList.copyOf(rows);
    return new PageList(list);
  }

  public PageList filter(Predicate predicate) {
    List filtered = FluentIterable.from(rows)
        .filter(predicate)
        .toList();
    return PageList.of(filtered);
  }

  public PageList page(RequestWrapper wrapper) {
    Page page = wrapper.getPage();
    List limit = page.limit(rows);
    Pager nextPager = pager.to(page);
    return new PageList(limit, nextPager);
  }

  public PageList sort(Comparator comparator) {
    List rows = getRows();
    ArrayList mutable = newArrayList(rows);
    Collections.sort(mutable, comparator);
    return new PageList(mutable, pager);
  }

  public  PageList transform(Function function) {
    List lazy = Lists.transform(rows, function);
    List rows = ImmutableList.copyOf(lazy);
    return new PageList(rows, pager);
  }

  public Context toContext(String var) {
    return Context.of()
        .put(var, this);
  }

  public boolean isEmpty() {
    return empty;
  }

  public List getRows() {
    return rows;
  }

  public Pager getPager() {
    return pager;
  }

  public int size() {
    return rows.size();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy