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

com.mysema.query.group.GMap Maven / Gradle / Ivy

There is a newer version: 3.7.4
Show newest version
/*
 * Copyright 2011, Mysema Ltd
 *
 * 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.mysema.query.group;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.mysema.commons.lang.Pair;

/**
 * @param 
 * @param 
 */
class GMap extends AbstractGroupExpression, Map> {

    private static final long serialVersionUID = 7106389414200843920L;

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

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

            private final Map map = new LinkedHashMap();

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

            @Override
            public Map 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>();

            public 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" })
        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