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

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

There is a newer version: 6.8
Show 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.mysema.commons.lang.Pair;
import java.util.*;

abstract class GMap> extends AbstractGroupExpression, M> {

  private static final long serialVersionUID = 7106389414200843920L;

  GMap(QPair qpair) {
    super(Map.class, qpair);
  }

  protected abstract M createMap();

  public static  GMap> createLinked(QPair expr) {
    return new GMap>(expr) {
      @Override
      protected Map createMap() {
        return new LinkedHashMap();
      }
    };
  }

  public static , U> GMap> createSorted(
      QPair expr) {
    return new GMap>(expr) {
      @Override
      protected SortedMap createMap() {
        return new TreeMap();
      }
    };
  }

  public static  GMap> createSorted(
      QPair expr, final Comparator comparator) {
    return new GMap>(expr) {
      @Override
      protected SortedMap createMap() {
        return new TreeMap(comparator);
      }
    };
  }

  @Override
  public GroupCollector, M> createGroupCollector() {
    return new GroupCollector, M>() {

      private final M map = createMap();

      @Override
      public void add(Pair pair) {
        map.put(pair.getFirst(), pair.getSecond());
      }

      @Override
      public M get() {
        return map;
      }
    };
  }

  static class Mixin>
      extends AbstractGroupExpression, R> {

    private static final long serialVersionUID = 1939989270493531116L;

    private class GroupCollectorImpl implements GroupCollector, R> {

      private final GroupCollector, R> groupCollector;

      private final Map> keyCollectors =
          new LinkedHashMap>();

      private final Map, GroupCollector> valueCollectors =
          new HashMap, GroupCollector>();

      GroupCollectorImpl() {
        this.groupCollector = mixin.createGroupCollector();
      }

      @Override
      public void add(Pair pair) {
        K first = pair.getFirst();
        GroupCollector keyCollector = keyCollectors.get(first);
        if (keyCollector == null) {
          keyCollector = keyExpression.createGroupCollector();
          keyCollectors.put(first, keyCollector);
        }
        keyCollector.add(first);
        GroupCollector valueCollector = valueCollectors.get(keyCollector);
        if (valueCollector == null) {
          valueCollector = valueExpression.createGroupCollector();
          valueCollectors.put(keyCollector, valueCollector);
        }
        V second = pair.getSecond();
        valueCollector.add(second);
      }

      @Override
      public R get() {
        for (GroupCollector keyCollector : keyCollectors.values()) {
          T key = keyCollector.get();
          GroupCollector valueCollector = valueCollectors.remove(keyCollector);
          U value = valueCollector.get();
          groupCollector.add(Pair.of(key, value));
        }
        keyCollectors.clear();
        return groupCollector.get();
      }
    }

    private final GroupExpression, R> mixin;

    private final GroupExpression keyExpression;
    private final GroupExpression valueExpression;

    @SuppressWarnings({"rawtypes", "unchecked"})
    Mixin(
        GroupExpression keyExpression,
        GroupExpression valueExpression,
        AbstractGroupExpression, R> mixin) {
      super(
          (Class) mixin.getType(),
          QPair.create(keyExpression.getExpression(), valueExpression.getExpression()));
      this.keyExpression = keyExpression;
      this.valueExpression = valueExpression;
      this.mixin = mixin;
    }

    @Override
    public GroupCollector, R> createGroupCollector() {
      return new GroupCollectorImpl();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy