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

com.querydsl.core.group.GMap 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.mysema.commons.lang.Pair;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * GMap
 *
 * @param  k
 * @param  v
 * @param  m
 */
public 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<>() {

      private final M map = createMap();

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

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

  /**
   * Mixin
   *
   * @param  k
   * @param  v
   * @param  t
   * @param  u
   * @param  r
   */
  public 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<>();

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

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

      @Override
      public R get() {
        for (GroupCollector keyCollector : keyCollectors.values()) {
          var key = keyCollector.get();
          var valueCollector = valueCollectors.remove(keyCollector);
          var 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"})
    public 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