/*
 * Copyright 2017-2017 the original author or authors.
 *
 * 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 org.hellojavaer.ddal.ddr.shard;

import java.util.*;

/**
 *
 * @author <a href="mailto:hellojavaer@gmail.com">Kaiming Zou</a>,created on 20/06/2017.
 */
public class ShardRouteUtils {

    public static <T> Map<RouteInfo, List<T>> groupSdValuesByRouteInfo(ShardRouter shardRouter, String scName,
                                                                       String tbName, List<T> sdValues) {
        if (sdValues == null || sdValues.isEmpty()) {
            return Collections.EMPTY_MAP;
        }
        Map<RouteInfo, List<T>> map = new LinkedHashMap<>();
        for (Object item : sdValues) {
            RouteInfo routeInfo = shardRouter.getRouteInfo(scName, tbName, item);
            List list = map.get(routeInfo);
            if (list == null) {
                list = new ArrayList();
                map.put(routeInfo, list);
            }
            list.add(item);
        }
        return map;
    }

    public static <T> Map<RouteInfo, Set<T>> groupSdValuesByRouteInfo(ShardRouter shardRouter, String scName,
                                                                      String tbName, Set<T> sdValues) {
        if (sdValues == null || sdValues.isEmpty()) {
            return Collections.EMPTY_MAP;
        }
        Map<RouteInfo, Set<T>> map = null;
        if (sdValues instanceof LinkedHashSet) {
            map = new LinkedHashMap();
        } else {
            map = new HashMap<>();
        }
        for (Object item : sdValues) {
            RouteInfo routeInfo = shardRouter.getRouteInfo(scName, tbName, item);
            Set set = map.get(routeInfo);
            if (set == null) {
                set = new LinkedHashSet();
                map.put(routeInfo, set);
            }
            set.add(item);
        }
        return map;
    }

}

@@ -2419,10 +2419,1215 @@
 %0A    %7D%0A%0A
+    public static Map%3CString, List%3CRouteInfo%3E%3E groupRouteInfosByScName(List%3CRouteInfo%3E routeInfos) %7B%0A        if (routeInfos == null %7C%7C routeInfos.isEmpty()) %7B%0A            return Collections.EMPTY_MAP;%0A        %7D%0A        Map%3CString, List%3CRouteInfo%3E%3E map = new LinkedHashMap%3C%3E();%0A        for (RouteInfo routeInfo : routeInfos) %7B%0A            List%3CRouteInfo%3E list = map.get(routeInfo.getScName());%0A            if (list == null) %7B%0A                list = new ArrayList%3C%3E();%0A                map.put(routeInfo.getScName(), list);%0A            %7D%0A            list.add(routeInfo);%0A        %7D%0A        return map;%0A    %7D%0A%0A    public static Map%3CString, Set%3CRouteInfo%3E%3E groupRouteInfosByScName(Set%3CRouteInfo%3E routeInfos) %7B%0A        if (routeInfos == null %7C%7C routeInfos.isEmpty()) %7B%0A            return Collections.EMPTY_MAP;%0A        %7D%0A        Map%3CString, Set%3CRouteInfo%3E%3E map = new LinkedHashMap%3C%3E();%0A        for (RouteInfo routeInfo : routeInfos) %7B%0A            Set%3CRouteInfo%3E set = map.get(routeInfo.getScName());%0A            if (set == null) %7B%0A                set = new LinkedHashSet%3C%3E();%0A                map.put(routeInfo.getScName(), set);%0A            %7D%0A            set.add(routeInfo);%0A        %7D%0A        return map;%0A    %7D%0A%0A
 %7D%0A

add method groupRouteInfosByScName for ShardRouteUtils