Example usage for com.google.common.collect FluentIterable index

List of usage examples for com.google.common.collect FluentIterable index

Introduction

In this page you can find the example usage for com.google.common.collect FluentIterable index.

Prototype

@CheckReturnValue
public final <K> ImmutableListMultimap<K, E> index(Function<? super E, K> keyFunction) 

Source Link

Document

Creates an index ImmutableListMultimap that contains the results of applying a specified function to each item in this FluentIterable of values.

Usage

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 w  ww. j  a  v a 2 s .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:edu.buaa.satla.analysis.cfa.CProgramScope.java

private static Map<String, CType> extractTypeDefs(FluentIterable<CTypeDeclaration> pTypeDcls,
        LogManager pLogger) {/*from   w w  w .  j  av  a  2  s.c o  m*/
    FluentIterable<CTypeDefDeclaration> plainTypeDefs = pTypeDcls.filter(CTypeDefDeclaration.class);

    // Construct multimap that may contain duplicates
    Multimap<String, CTypeDefDeclaration> typeDefDeclarationsMap = plainTypeDefs
            .index(new Function<CTypeDefDeclaration, String>() {

                @Override
                public String apply(CTypeDefDeclaration pArg0) {
                    return pArg0.getQualifiedName();
                }

            });

    // Get unique type defs
    Map<String, CType> uniqueTypeDefs = Maps.newHashMap();

    for (Map.Entry<String, Collection<CTypeDefDeclaration>> typeDefEntry : typeDefDeclarationsMap.asMap()
            .entrySet()) {
        String qualifiedName = typeDefEntry.getKey();
        FluentIterable<CType> types = from(typeDefEntry.getValue())
                .transform(new Function<CTypeDefDeclaration, CType>() {

                    @Override
                    public CType apply(CTypeDefDeclaration pArg0) {
                        return pArg0.getType();
                    }

                });
        putIfUnique(uniqueTypeDefs, qualifiedName, types, pLogger);
    }

    return Collections.unmodifiableMap(uniqueTypeDefs);
}

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

/**
 * Creates an object of this class./*from ww  w  .  j a  va 2  s. c o m*/
 *
 * When a single or a block of statements is supposed to be parsed, first a cfa for
 * the whole program has to be parsed to generate complex types for the variables.
 * These types and declarations are stored in this scope.
 *
 * @param cfa the cfa of the program, where single or block of statements are supposed to be parsed
 */
public CProgramScope(CFA cfa, LogManager pLogger) {

    assert cfa.getLanguage() == Language.C;

    functionName = null;

    /* Get all nodes, get all edges from nodes, get all declarations from edges,
     * assign every declaration its name.
     */
    Collection<CFANode> nodes = cfa.getAllNodes();

    FluentIterable<CSimpleDeclaration> dcls = FluentIterable.from(nodes)
            .transformAndConcat(TO_C_SIMPLE_DECLARATIONS).filter(HAS_NAME);

    FluentIterable<CFunctionDeclaration> functionDcls = dcls.filter(CFunctionDeclaration.class);
    FluentIterable<CSimpleDeclaration> nonFunctionDcls = dcls
            .filter(not(instanceOf(CFunctionDeclaration.class)));
    FluentIterable<CTypeDeclaration> typeDcls = dcls.filter(CTypeDeclaration.class);

    qualifiedTypes = extractTypes(nonFunctionDcls, pLogger);

    qualifiedTypeDefs = extractTypeDefs(typeDcls, pLogger);

    functionDeclarations = functionDcls.index(GET_ORIGINAL_QUALIFIED_NAME);

    variableNames = nonFunctionDcls.transform(GET_NAME).toSet();

    qualifiedDeclarations = extractQualifiedDeclarations(nonFunctionDcls, pLogger);

    uniqueSimpleDeclarations = extractUniqueSimpleDeclarations(qualifiedDeclarations);
}