Example usage for com.google.common.collect Multimap asMap

List of usage examples for com.google.common.collect Multimap asMap

Introduction

In this page you can find the example usage for com.google.common.collect Multimap asMap.

Prototype

Map<K, Collection<V>> asMap();

Source Link

Document

Returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key's associated values.

Usage

From source file:org.modeshape.graph.query.process.QueryResultColumns.java

protected static Set<Column> findColumnsWithSameNames(List<Column> columns) {
    Multimap<String, Column> columnNames = ArrayListMultimap.create();
    for (Column column : columns) {
        String columnName = column.columnName() != null ? column.columnName() : column.propertyName();
        columnNames.put(columnName, column);
    }//from w w w  .j av a  2s .co  m
    Set<Column> results = new HashSet<Column>();
    for (Map.Entry<String, Collection<Column>> entry : columnNames.asMap().entrySet()) {
        if (entry.getValue().size() > 1) {
            results.addAll(entry.getValue());
        }
    }
    return results;
}

From source file:moa2014.MOAH12014.java

public static int principal(int[][] matrizatual) {
    long startTime = System.currentTimeMillis();
    Multimap<Integer, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    int difmatrizatual = diferencaMatriz(matrizatual);

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int arvoresgeradas = 0;
    int arvoresprocessadas = 0;

    while (!open_list.isEmpty()) {

        Iterator iterator = open_list.keySet().iterator();
        Integer key = (Integer) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();
        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = diferencaMatriz(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            long endTime = System.currentTimeMillis();
            System.out.println("---------------------------------------");
            System.out.println("Arvores Geradas: " + arvoresgeradas);
            System.out.println("Arvores Processadas: " + arvoresprocessadas);
            System.out.println("Quantidade de Movimentos: " + estadomenor.getCusto());
            System.out.println("Tempo de processamento " + (endTime - startTime) + " ms");
            System.out.println("---------------------------------------\n\n");
            return 0;
        }/*from   w w  w. ja  va2  s. c o  m*/
        arvoresprocessadas++;
        int[][] matrizatualx = estadomenor.getMatriz();
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            int custoateaqui = diferencaMatriz(matriz) + altura + 1;
            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }
    return 0;

}

From source file:org.eigenbase.rel.WindowRel.java

/**
 * Creates a WindowRel.//from  w  w  w  .  j  a v  a2  s.  c  om
 */
public static RelNode create(RelOptCluster cluster, RelTraitSet traitSet, RelNode child,
        final RexProgram program, RelDataType outRowType) {
    // Build a list of distinct windows, partitions and aggregate
    // functions.
    final Multimap<WindowKey, RexOver> windowMap = LinkedListMultimap.create();

    // Build a list of windows, partitions, and aggregate functions. Each
    // aggregate function will add its arguments as outputs of the input
    // program.
    for (RexNode agg : program.getExprList()) {
        if (agg instanceof RexOver) {
            addWindows(windowMap, (RexOver) agg);
        }
    }

    final Map<RexOver, WindowRelBase.RexWinAggCall> aggMap = new HashMap<RexOver, WindowRelBase.RexWinAggCall>();
    List<Window> windowList = new ArrayList<Window>();
    for (Map.Entry<WindowKey, Collection<RexOver>> entry : windowMap.asMap().entrySet()) {
        final WindowKey windowKey = entry.getKey();
        final List<RexWinAggCall> aggCalls = new ArrayList<RexWinAggCall>();
        for (RexOver over : entry.getValue()) {
            final RexWinAggCall aggCall = new RexWinAggCall(over.getAggOperator(), over.getType(),
                    toInputRefs(over.operands), aggMap.size());
            aggCalls.add(aggCall);
            aggMap.put(over, aggCall);
        }
        windowList.add(new Window(windowKey.groupSet, windowKey.isRows, windowKey.lowerBound,
                windowKey.upperBound, windowKey.orderKeys, aggCalls));
    }

    // Figure out the type of the inputs to the output program.
    // They are: the inputs to this rel, followed by the outputs of
    // each window.
    final List<WindowRelBase.RexWinAggCall> flattenedAggCallList = new ArrayList<WindowRelBase.RexWinAggCall>();
    List<Map.Entry<String, RelDataType>> fieldList = new ArrayList<Map.Entry<String, RelDataType>>(
            child.getRowType().getFieldList());
    final int offset = fieldList.size();

    // Use better field names for agg calls that are projected.
    Map<Integer, String> fieldNames = new HashMap<Integer, String>();
    for (Ord<RexLocalRef> ref : Ord.zip(program.getProjectList())) {
        final int index = ref.e.getIndex();
        if (index >= offset) {
            fieldNames.put(index - offset, outRowType.getFieldNames().get(ref.i));
        }
    }

    for (Ord<Window> window : Ord.zip(windowList)) {
        for (Ord<RexWinAggCall> over : Ord.zip(window.e.aggCalls)) {
            // Add the k-th over expression of
            // the i-th window to the output of the program.
            String name = fieldNames.get(over.i);
            if (name == null || name.startsWith("$")) {
                name = "w" + window.i + "$o" + over.i;
            }
            fieldList.add(Pair.of(name, over.e.getType()));
            flattenedAggCallList.add(over.e);
        }
    }
    final RelDataType intermediateRowType = cluster.getTypeFactory().createStructType(fieldList);

    final int inputFieldCount = child.getRowType().getFieldCount();

    // The output program is the windowed agg's program, combined with
    // the output calc (if it exists).
    RexShuttle shuttle = new RexShuttle() {
        public RexNode visitOver(RexOver over) {
            // Look up the aggCall which this expr was translated to.
            final WindowRelBase.RexWinAggCall aggCall = aggMap.get(over);
            assert aggCall != null;
            assert RelOptUtil.eq("over", over.getType(), "aggCall", aggCall.getType(), true);

            // Find the index of the aggCall among all partitions of all
            // windows.
            final int aggCallIndex = flattenedAggCallList.indexOf(aggCall);
            assert aggCallIndex >= 0;

            // Replace expression with a reference to the window slot.
            final int index = inputFieldCount + aggCallIndex;
            assert RelOptUtil.eq("over", over.getType(), "intermed",
                    intermediateRowType.getFieldList().get(index).getType(), true);
            return new RexInputRef(index, over.getType());
        }

        public RexNode visitLocalRef(RexLocalRef localRef) {
            final int index = localRef.getIndex();
            if (index < inputFieldCount) {
                // Reference to input field.
                return localRef;
            }
            return new RexLocalRef(flattenedAggCallList.size() + index, localRef.getType());
        }
    };
    // TODO: The order that the "over" calls occur in the windows and
    // partitions may not match the order in which they occurred in the
    // original expression. We should add a project to permute them.

    WindowRel window = new WindowRel(cluster, traitSet, child, intermediateRowType, windowList);

    return CalcRel.createProject(window, new AbstractList<RexNode>() {
        public RexNode get(int index) {
            final RexLocalRef ref = program.getProjectList().get(index);
            return new RexInputRef(ref.getIndex(), ref.getType());
        }

        public int size() {
            return program.getProjectList().size();
        }
    }, outRowType.getFieldNames());
}

From source file:es.usc.citius.composit.core.composition.search.CompositSearch.java

private static <E, T extends Comparable<T>> Set<Set<Operation<E>>> nodeEquivalence(
        Set<Set<Operation<E>>> successors, ServiceMatchNetwork<E, T> network) {
    // Group candidates
    if (successors.size() < 2)
        return successors;
    Multimap<Set<Set<Operation<E>>>, Set<Operation<E>>> mmap = HashMultimap.create();
    for (Set<Operation<E>> group : successors) {
        Set<E> inputs = Operations.inputs(group);
        // Find providers
        Set<Set<Operation<E>>> successorGroup = new HashSet<Set<Operation<E>>>();
        for (E input : inputs) {
            for (E source : network.getSourceElementsThatMatch(input).keySet()) {
                successorGroup.add(Sets.newHashSet(network.getOperationsWithOutput(source)));
            }/*  w  w w . ja v  a2 s .c  o m*/
        }
        mmap.get(successorGroup).add(group);
    }
    // Select one from each equivalent group
    Set<Set<Operation<E>>> nonEquivalentGroups = new HashSet<Set<Operation<E>>>();
    for (Collection<Set<Operation<E>>> group : mmap.asMap().values()) {
        nonEquivalentGroups.add(group.iterator().next());
    }
    return nonEquivalentGroups;
}

From source file:moa2014.MOAH32014.java

public static int principal(int[][] matrizatual) {
    long startTime = System.currentTimeMillis();
    Multimap<Integer, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    int difmatrizatual = diferencaMatriz(matrizatual);

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int arvoresgeradas = 0;
    int arvoresprocessadas = 0;

    while (!open_list.isEmpty()) {
        Iterator iterator = open_list.keySet().iterator();
        Integer key = (Integer) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();
        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = diferencaMatriz(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            long endTime = System.currentTimeMillis();
            System.out.println("---------------------------------------");
            System.out.println("Arvores Geradas: " + arvoresgeradas);
            System.out.println("Arvores Processadas: " + arvoresprocessadas);
            System.out.println("Quantidade de Movimentos: " + estadomenor.getCusto());
            System.out.println("Tempo de processamento " + (endTime - startTime) + " ms");
            System.out.println("---------------------------------------\n\n");
            return 0;
        }/*from  w  ww  .  jav  a  2s .co  m*/
        arvoresprocessadas++;
        int[][] matrizatualx = estadomenor.getMatriz();
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            int custoateaqui = diferencaMatriz(matriz) + altura + 1;
            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }
    return 0;

}

From source file:moa2014.MOAH22014.java

public static int principal(int[][] matrizatual) {
    long startTime = System.currentTimeMillis();
    Multimap<Integer, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    int difmatrizatual = diferencaMatriz(matrizatual);

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int arvoresgeradas = 0;
    int arvoresprocessadas = 0;

    while (!open_list.isEmpty()) {
        Iterator iterator = open_list.keySet().iterator();

        Integer key = (Integer) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();
        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = diferencaMatriz(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            long endTime = System.currentTimeMillis();
            System.out.println("---------------------------------------");
            System.out.println("Arvores Geradas: " + arvoresgeradas);
            System.out.println("Arvores Processadas: " + arvoresprocessadas);
            System.out.println("Quantidade de Movimentos: " + estadomenor.getCusto());
            System.out.println("Tempo de processamento " + (endTime - startTime) + " ms");
            System.out.println("---------------------------------------\n\n");
            return 0;
        }//from w  w w . j  a v a  2s  . c om
        arvoresprocessadas++;
        int[][] matrizatualx = estadomenor.getMatriz();
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            int custoateaqui = diferencaMatriz(matriz) + altura + 1;
            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {
                arvoresgeradas++;
                int diferencamatriz = diferencaMatriz(matriz);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }
    return 0;

}

From source file:edu.buaa.satla.analysis.cfa.CProgramScope.java

private static Map<String, CSimpleDeclaration> extractQualifiedDeclarations(
        FluentIterable<CSimpleDeclaration> pNonFunctionDcls, LogManager pLogger) {
    Multimap<String, CSimpleDeclaration> qualifiedDeclarationsMultiMap = pNonFunctionDcls
            .index(GET_ORIGINAL_QUALIFIED_NAME);

    Map<String, CSimpleDeclaration> qualifiedDeclarations = Maps.newHashMap();
    for (Map.Entry<String, Collection<CSimpleDeclaration>> declarationsEntry : qualifiedDeclarationsMultiMap
            .asMap().entrySet()) {/*from ww w  .  j ava 2s  .c  om*/
        String qualifiedName = declarationsEntry.getKey();
        Collection<CSimpleDeclaration> declarations = declarationsEntry.getValue();
        Set<CSimpleDeclaration> duplicateFreeDeclarations = from(declarations)
                .transform(new Function<CSimpleDeclaration, CSimpleDeclaration>() {

                    @Override
                    public CSimpleDeclaration apply(CSimpleDeclaration pArg0) {
                        if (pArg0 instanceof CVariableDeclaration) {
                            CVariableDeclaration original = (CVariableDeclaration) pArg0;
                            if (original.getInitializer() == null) {
                                return pArg0;
                            }
                            return new CVariableDeclaration(original.getFileLocation(), original.isGlobal(),
                                    original.getCStorageClass(), original.getType(), original.getName(),
                                    original.getOrigName(), original.getQualifiedName(), null);
                        }
                        return pArg0;
                    }

                }).toSet();
        if (!duplicateFreeDeclarations.isEmpty()) {
            if (duplicateFreeDeclarations.size() == 1) {
                qualifiedDeclarations.put(qualifiedName, declarations.iterator().next());
            } else {
                pLogger.log(Level.FINEST, "Ignoring declaration for", qualifiedName,
                        " for creation of program-wide scope because it is not unique.");
            }
        }
    }
    return Collections.unmodifiableMap(qualifiedDeclarations);
}

From source file:org.jetbrains.jet.buildergen.EntityBuilder.java

private static void bindOverriddenRelations(Entity entity, Set<Entity> alreadyBound) {
    if (!alreadyBound.add(entity))
        return;/*from  www.jav  a 2  s  . c o  m*/
    Multimap<String, Relation<?>> superRelations = HashMultimap.create();
    for (Entity superEntity : entity.getSuperEntities()) {
        bindOverriddenRelations(superEntity, alreadyBound);
        for (Relation<?> relation : superEntity.getRelations()) {
            superRelations.put(relation.getName(), relation);
        }
    }
    Set<String> explicitlyOverridden = Sets.newHashSet();
    for (Relation<?> relation : Lists.newArrayList(entity.getRelations())) {
        relation.getOverriddenRelations().addAll(superRelations.get(relation.getName()));
        explicitlyOverridden.add(relation.getName());
    }

    // "fake overrides"
    for (Map.Entry<String, Collection<Relation<?>>> entry : superRelations.asMap().entrySet()) {
        String relationName = entry.getKey();
        Collection<Relation<?>> overriddenRelations = entry.getValue();
        Relation<?> someOverridden = ContainerUtil.getFirstItem(overriddenRelations);
        RelationWithTarget<Object> fakeOverride = new RelationWithTarget<Object>(
                someOverridden.getMultiplicity(), relationName, someOverridden.getTarget());
        fakeOverride.getOverriddenRelations().addAll(overriddenRelations);
        entity.getRelations().add(fakeOverride);
    }
}

From source file:com.ikanow.aleph2.analytics.spark.utils.RddDependencyUtils.java

/** Builds an RDD pipeline
 * @param inputs/*from  w ww .j a v  a 2 s  . c om*/
 * @param enrichment_pipeline_config
 * @return a validation, if successful containing (all generated rdds, output rdds only) - normally only the second tuple is needed
 */
public static Validation<String, //(error)
        Tuple2<Map<String, Either<JavaRDD<Tuple2<Long, IBatchRecord>>, JavaRDD<Tuple2<IBatchRecord, Tuple2<Long, IBatchRecord>>>>>, //(list of all RDDs)
                Map<String, JavaRDD<Tuple2<Long, IBatchRecord>>> //(just outputs
>> buildEnrichmentPipeline(final IAnalyticsContext context, final JavaSparkContext jsc,
        final Multimap<String, JavaPairRDD<Object, Tuple2<Long, IBatchRecord>>> inputs,
        final Collection<EnrichmentControlMetadataBean> enrichment_pipeline_config) {
    // Build the pipeline
    final Validation<String, LinkedHashMap<String, Tuple2<Set<String>, List<EnrichmentControlMetadataBean>>>> maybe_enrichment_pipeline = DependencyUtils
            .buildPipelineOfContainers(inputs.keySet(), enrichment_pipeline_config);

    return maybe_enrichment_pipeline.bind(enrichment_pipeline -> {

        // (2 types of RDD - before and after...)
        final HashMap<String, Either<JavaRDD<Tuple2<Long, IBatchRecord>>, JavaRDD<Tuple2<IBatchRecord, Tuple2<Long, IBatchRecord>>>>> mutable_rdds = new HashMap<>();

        // Insert all the inputs:
        inputs.asMap().entrySet().stream().forEach(kv -> mutable_rdds.put(kv.getKey(), Either.left(
                kv.getValue().stream().reduce((acc1, acc2) -> acc1.union(acc2)).get().map(t2 -> t2._2()))));

        // First pass, find all the groupings:
        // (if _any_ immediately downstream element needs grouping then treat as all do and map the extra element away)
        final Map<String, Collection<String>> jobs_that_need_to_group = enrichment_pipeline.values().stream()
                .distinct().<Tuple2<String, Collection<String>>>flatMap(t2 -> {
                    return t2._2().stream().findFirst().map(e -> Optionals.ofNullable(e.grouping_fields()))
                            .<Stream<Tuple2<String, Collection<String>>>>map(
                                    groupings -> t2._1().stream().map(input -> Tuples._2T(input, groupings)))
                            .orElseGet(Stream::empty);
                }).collect(Collectors.toMap(t2 -> t2._1(), t2 -> t2._2()));

        // Second pass, do we need $inputs:
        if (enrichment_pipeline.values().stream().distinct()
                .anyMatch(t2 -> t2._1().contains(EnrichmentControlMetadataBean.PREVIOUS_STEP_ALL_INPUTS))) {
            inputs.put(EnrichmentControlMetadataBean.PREVIOUS_STEP_ALL_INPUTS, inputs.values().stream()
                    .reduce((acc1, acc2) -> acc1.union(acc2)).orElse(jsc.emptyRDD().flatMapToPair(__ -> null)));
        }

        // Third/forth pass, create another mutable state that tells us which enrichers are the furthest downstream
        final Set<String> mutable_enricher_set = new HashSet<>(enrichment_pipeline.values().stream().distinct()
                .<EnrichmentControlMetadataBean>flatMap(t2 -> StreamUtils.stream(t2._2().stream().findFirst())) // (discard inputs)
                .map(e -> e.name()).collect(Collectors.toSet()));
        enrichment_pipeline.values().stream().distinct().forEach(t2 -> {
            final EnrichmentControlMetadataBean control = t2._2().stream().findFirst().get();
            mutable_enricher_set.removeAll(control.dependencies());
        });

        // Fifth (!) pass actually does all the work:

        enrichment_pipeline.values().stream().distinct().filter(t2 -> t2._2().stream().findFirst().isPresent()) // (discard inputs)
                .forEach(t2 -> {
                    final EnrichmentControlMetadataBean control = t2._2().stream().findFirst().get();
                    final boolean upstream_is_grouped = !Optionals.ofNullable(control.grouping_fields())
                            .isEmpty();
                    final Collection<String> downstream_grouping = jobs_that_need_to_group.get(control.name());
                    final boolean downstream_is_grouped = null != downstream_grouping;

                    final boolean to_emit = mutable_enricher_set.contains(control.name());

                    // Get all the inputs:
                    // 4 cases depending on whether upstream/downstream are grouped

                    if (upstream_is_grouped) {
                        // (ignore any inputs that haven't been grouped)
                        final JavaRDD<Tuple2<IBatchRecord, Tuple2<Long, IBatchRecord>>> rdd_inputs = t2._1()
                                .stream().map(dep -> mutable_rdds.get(dep))
                                .filter(rdd_choice -> rdd_choice.isRight())
                                .map(rdd_choice -> rdd_choice.right().value())
                                .reduce((acc1, acc2) -> acc1.union(acc2)).orElseGet(() -> jsc.emptyRDD());

                        if (!downstream_is_grouped) {
                            mutable_rdds.put(control.name(),
                                    Either.left(EnrichmentPipelineService.javaGroupOf(rdd_inputs).mapPartitions(
                                            EnrichmentPipelineService.create(context, to_emit, t2._2())
                                                    .javaInMapPartitionsPostGroup())));
                        } else {
                            mutable_rdds.put(control.name(), Either.right(EnrichmentPipelineService
                                    .javaGroupOf(rdd_inputs)
                                    .mapPartitions(EnrichmentPipelineService.create(context, to_emit, t2._2())
                                            .javaInMapPartitionsPrePostGroup(
                                                    new ArrayList<>(downstream_grouping)))));
                        }
                    } else {
                        // (convert any grouped inputs to ungrouped)
                        final JavaRDD<Tuple2<Long, IBatchRecord>> rdd_inputs = t2._1().stream()
                                .map(dep -> mutable_rdds.get(dep))
                                .map(rdd_choice -> rdd_choice.<JavaRDD<Tuple2<Long, IBatchRecord>>>either(
                                        ungrouped -> ungrouped, grouped -> grouped.map(tt2 -> tt2._2())))
                                .reduce((acc1, acc2) -> acc1.union(acc2)).orElseGet(() -> jsc.emptyRDD());

                        if (!downstream_is_grouped) {
                            mutable_rdds.put(control.name(),
                                    Either.left(rdd_inputs.mapPartitions(EnrichmentPipelineService
                                            .create(context, to_emit, t2._2()).javaInMapPartitions())));
                        } else {
                            mutable_rdds.put(control.name(),
                                    Either.right(rdd_inputs.mapPartitions(EnrichmentPipelineService
                                            .create(context, to_emit, t2._2()).javaInMapPartitionsPreGroup(
                                                    new ArrayList<>(downstream_grouping)))));
                        }
                    }
                });

        return Validation.success(Tuples._2T(mutable_rdds,
                mutable_enricher_set.stream().map(e_name -> Tuples._2T(e_name, mutable_rdds.get(e_name)))
                        .filter(name_rdd -> null != name_rdd._2())
                        .<Tuple2<String, JavaRDD<Tuple2<Long, IBatchRecord>>>>map(
                                name__rdd_choice -> Tuples._2T(
                                        name__rdd_choice._1(),
                                        name__rdd_choice
                                                ._2().either(
                                                        ungrouped -> ungrouped,
                                                        grouped -> grouped.map(t2 -> t2._2))))
                        .collect(Collectors
                                .<Tuple2<String, JavaRDD<Tuple2<Long, IBatchRecord>>>, String, JavaRDD<Tuple2<Long, IBatchRecord>>>toMap(
                                        t2 -> t2._1(), t2 -> t2._2()))));
    });
}

From source file:com.foundationdb.util.Strings.java

public static List<String> stringAndSort(Map<?, ?> inputs) {
    // step 1: get the key-value pairs into a multimap. We need a multimap because multiple keys may have the
    // same toString. For instance, { 1 : "int", 1L : "long" } would become a multimap { "1" : ["int", "long"] }
    Multimap<String, String> multiMap = ArrayListMultimap.create();
    for (Map.Entry<?, ?> inputEntry : inputs.entrySet()) {
        String keyString = stringAndSort(inputEntry.getKey());
        String valueString = stringAndSort(inputEntry.getValue());
        multiMap.put(keyString, valueString);
    }//from   w  w w .j a va 2  s. c  om
    // step 2: Flatten the multimap into a Map<String,String>, sorting by keys as you go.
    Map<String, String> sortedAndFlattened = new TreeMap<>();
    for (Entry<String, Collection<String>> multiMapEntry : multiMap.asMap().entrySet()) {
        String keyString = multiMapEntry.getKey();
        String valueString = stringAndSort(multiMapEntry.getValue()).toString();
        String duplicate = sortedAndFlattened.put(keyString, valueString);
        assert duplicate == null : duplicate;
    }

    // step 3: Flatten the map into a List<String>
    List<String> results = new ArrayList<>(inputs.size());
    for (Entry<String, String> entry : sortedAndFlattened.entrySet()) {
        results.add(entry.toString());
    }
    return results;
}