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

com.querydsl.core.group.guava.GuavaGroupByBuilder Maven / Gradle / Ivy

Go to download

Utilities for creating group by factory expressions for Guava collection types

There is a newer version: 6.10
Show newest version
/*
 * Copyright 2020, The Querydsl Team (http://www.querydsl.com/team)
 *
 * 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.querydsl.core.group.guava;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Table;
import com.google.common.collect.Table.Cell;
import com.google.common.collect.TreeBasedTable;
import com.google.common.collect.TreeMultimap;
import com.querydsl.core.ResultTransformer;
import com.querydsl.core.group.Group;
import com.querydsl.core.group.GroupByBuilder;
import com.querydsl.core.types.Expression;
import java.util.Comparator;
import java.util.Map;

/**
 * {@code GroupByBuilder} is a fluent builder for GroupBy transformer instances. This class is not
 * to be used directly, but via {@link GuavaGroupBy}.
 *
 * @param 
 * @author Jan-Willem Gmelig Melying
 */
public class GuavaGroupByBuilder extends GroupByBuilder {

  /**
   * Create a new GroupByBuilder for the given key expression
   *
   * @param key key for aggregating
   */
  public GuavaGroupByBuilder(Expression key) {
    super(key);
  }

  /**
   * Get the results as multi map
   *
   * @param expression value expression
   * @param  Value type
   * @return new result transformer
   */
  public  ResultTransformer> asMultimap(Expression expression) {
    final Expression lookup = getLookup(expression);
    return new GroupByMultimap<>(key, expression) {
      @Override
      protected Multimap transform(Multimap groups) {
        Multimap results = LinkedHashMultimap.create();
        for (Map.Entry entry : groups.entries()) {
          results.put(entry.getKey(), entry.getValue().getOne(lookup));
        }
        return results;
      }
    };
  }

  /**
   * Get the results as multi map
   *
   * @param expression value expression
   * @param  Value type
   * @return new result transformer
   */
  public >
      ResultTransformer> asSortedSetMultimap(Expression expression) {
    final Expression lookup = getLookup(expression);
    return new GroupByMultimap<>(key, expression) {
      @Override
      protected TreeMultimap transform(Multimap groups) {
        TreeMultimap results = (TreeMultimap) TreeMultimap.create();
        for (Map.Entry entry : groups.entries()) {
          results.put(entry.getKey(), entry.getValue().getOne(lookup));
        }
        return results;
      }
    };
  }

  /**
   * Get the results as multi map
   *
   * @param expression value expression
   * @param comparator key comparator
   * @param valueComparator value comparator
   * @param  Value type
   * @return new result transformer
   */
  public  ResultTransformer> asSortedSetMultimap(
      Expression expression,
      final Comparator comparator,
      final Comparator valueComparator) {
    final Expression lookup = getLookup(expression);
    return new GroupByMultimap<>(key, expression) {
      @Override
      protected TreeMultimap transform(Multimap groups) {
        TreeMultimap results = TreeMultimap.create(comparator, valueComparator);
        for (Map.Entry entry : groups.entries()) {
          results.put(entry.getKey(), entry.getValue().getOne(lookup));
        }
        return results;
      }
    };
  }

  /**
   * Get the results as sorted table
   *
   * @param column column expression
   * @param expression value expression
   * @param  Column type
   * @param  Value type
   * @return new result transformer
   */
  public  ResultTransformer> asTable(
      final Expression column, final Expression expression) {
    final Expression columnKeyLookup = getLookup(column);
    final Expression lookup = getLookup(expression);
    return new GroupByTable<>(key, column, expression) {
      @Override
      protected Table transform(Table groups) {
        Table results = HashBasedTable.create();
        for (Cell cell : groups.cellSet()) {
          var rowKey = cell.getRowKey();
          var columnKey = cell.getValue().getOne(columnKeyLookup);
          var value = cell.getValue().getOne(lookup);
          results.put(rowKey, columnKey, value);
        }
        return results;
      }
    };
  }

  /**
   * Get the results as sorted table
   *
   * @param column column expression
   * @param expression value expression
   * @param  Column type
   * @param  Value type
   * @return new result transformer
   */
  public , V>
      ResultTransformer> asSortedTable(
          final Expression column, final Expression expression) {
    final Expression columnKeyLookup = getLookup(column);
    final Expression lookup = getLookup(expression);
    return new GroupByTable<>(key, column, expression) {
      @Override
      protected TreeBasedTable transform(Table groups) {
        TreeBasedTable results = (TreeBasedTable) TreeBasedTable.create();
        for (Cell cell : groups.cellSet()) {
          var rowKey = cell.getRowKey();
          var columnKey = cell.getValue().getOne(columnKeyLookup);
          var value = cell.getValue().getOne(lookup);
          results.put(rowKey, columnKey, value);
        }
        return results;
      }
    };
  }

  /**
   * Get the results as sorted table
   *
   * @param column column expression
   * @param expression value expression
   * @param rowComparator row comparator
   * @param columnComparator column comparator
   * @param  Column type
   * @param  Value type
   * @return new result transformer
   */
  public  ResultTransformer> asSortedTable(
      final Expression column,
      final Expression expression,
      final Comparator rowComparator,
      final Comparator columnComparator) {
    final Expression columnKeyLookup = getLookup(column);
    final Expression lookup = getLookup(expression);
    return new GroupByTable<>(key, column, expression) {
      @Override
      protected TreeBasedTable transform(Table groups) {
        TreeBasedTable results = TreeBasedTable.create(rowComparator, columnComparator);
        for (Cell cell : groups.cellSet()) {
          var rowKey = cell.getRowKey();
          var columnKey = cell.getValue().getOne(columnKeyLookup);
          var value = cell.getValue().getOne(lookup);
          results.put(rowKey, columnKey, value);
        }
        return results;
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy