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

com.querydsl.core.group.GroupImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015, 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;

import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Operation;
import com.querydsl.core.types.Ops;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;

/**
 * Default implementation of the Group interface
 *
 * @author sasa
 * @author tiwe
 */
public class GroupImpl implements Group {

  private final Map, GroupCollector> groupCollectorMap = new LinkedHashMap<>();

  private final List> groupExpressions;

  private final List> groupCollectors = new ArrayList<>();

  private final List> maps;

  public GroupImpl(List> columnDefinitions, List> maps) {
    this.groupExpressions = columnDefinitions;
    this.maps = maps;
    for (GroupExpression coldef : columnDefinitions) {
      GroupCollector collector = groupCollectorMap.get(coldef.getExpression());
      if (collector == null) {
        collector = coldef.createGroupCollector();
        Expression coldefExpr = coldef.getExpression();
        groupCollectorMap.put(coldefExpr, collector);
        if (coldefExpr instanceof Operation
            && ((Operation) coldefExpr).getOperator() == Ops.ALIAS) {
          groupCollectorMap.put(((Operation) coldefExpr).getArg(1), collector);
        }
      }
      groupCollectors.add(collector);
    }
  }

  @SuppressWarnings("unchecked")
  public void add(Object[] row) {
    var i = 0;
    for (GroupCollector groupCollector : groupCollectors) {
      groupCollector.add(row[i]);
      i++;
    }
  }

  @SuppressWarnings("unchecked")
  private  R get(Expression expr) {
    var col = (GroupCollector) groupCollectorMap.get(expr);
    if (col != null) {
      return col.get();
    }
    throw new NoSuchElementException(expr.toString());
  }

  @Override
  @SuppressWarnings("unchecked")
  public  R getGroup(GroupExpression definition) {
    for (GroupExpression def : groupExpressions) {
      if (def.equals(definition)) {
        return (R) groupCollectorMap.get(def.getExpression()).get();
      }
    }
    throw new NoSuchElementException(definition.toString());
  }

  @Override
  public  List getList(Expression expr) {
    return this.get(expr);
  }

  @Override
  @SuppressWarnings("unchecked")
  public  Map getMap(Expression key, Expression value) {
    for (QPair pair : maps) {
      if (pair.equals(key, value)) {
        return (Map) groupCollectorMap.get(pair).get();
      }
    }
    throw new NoSuchElementException("GMap(" + key + ", " + value + ")");
  }

  @SuppressWarnings("unchecked")
  @Override
  public  SortedMap getSortedMap(Expression key, Expression value) {
    for (QPair pair : maps) {
      if (pair.equals(key, value)) {
        return (SortedMap) groupCollectorMap.get(pair).get();
      }
    }
    throw new NoSuchElementException("GMap(" + key + ", " + value + ")");
  }

  @Override
  public  T getOne(Expression expr) {
    return this.get(expr);
  }

  @Override
  public  Set getSet(Expression expr) {
    return this.get(expr);
  }

  @Override
  public  SortedSet getSortedSet(Expression expr) {
    return this.get(expr);
  }

  @Override
  public Object[] toArray() {
    List arr = new ArrayList<>(groupCollectors.size());
    for (GroupCollector col : groupCollectors) {
      arr.add(col.get());
    }
    return arr.toArray();
  }
}