Example usage for com.google.common.collect ImmutableMap size

List of usage examples for com.google.common.collect ImmutableMap size

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMap size.

Prototype

int size();

Source Link

Document

Returns the number of key-value mappings in this map.

Usage

From source file:org.restlet.test.ext.apispark.conversion.swagger.v2_0.Swagger2TestCase.java

private void compareRwadefEndpoints(Definition savedDefinition, Definition translatedDefinition) {
    if (assertBothNull(savedDefinition.getEndpoints(), translatedDefinition.getEndpoints())) {
        return;//w w  w.  j a va2 s.c  om
    }

    ImmutableMap<String, Endpoint> savedEndpoints = Maps.uniqueIndex(savedDefinition.getEndpoints(),
            new Function<Endpoint, String>() {
                public String apply(Endpoint endpoint) {
                    return endpoint.computeUrl();
                }
            });
    ImmutableMap<String, Endpoint> translatedEndpoints = Maps.uniqueIndex(translatedDefinition.getEndpoints(),
            new Function<Endpoint, String>() {
                public String apply(Endpoint endpoint) {
                    return endpoint.computeUrl();
                }
            });
    assertEquals(savedEndpoints.size(), translatedEndpoints.size());
    for (String key : savedEndpoints.keySet()) {
        Endpoint savedEndpoint = savedEndpoints.get(key);
        Endpoint translatedEndpoint = translatedEndpoints.get(key);
        assertNotNull(savedEndpoint);
        assertNotNull(translatedEndpoint);
        assertEquals(savedEndpoint.getAuthenticationProtocol(), translatedEndpoint.getAuthenticationProtocol());
        assertEquals(savedEndpoint.getBasePath(), translatedEndpoint.getBasePath());
        assertEquals(savedEndpoint.getDomain(), translatedEndpoint.getDomain());
        assertEquals(savedEndpoint.getProtocol(), translatedEndpoint.getProtocol());
        assertEquals(savedEndpoint.getPort(), translatedEndpoint.getPort());
    }
}

From source file:org.restlet.test.ext.apispark.conversion.swagger.v2_0.Swagger2TestCase.java

private void compareRwadefResponses(Operation savedOperation, Operation translatedOperation) {
    if (assertBothNull(savedOperation.getResponses(), translatedOperation.getResponses())) {
        return;/*from   ww w .  j  a v  a2s  .co m*/
    }

    ImmutableMap<Integer, Response> savedResponses = Maps.uniqueIndex(savedOperation.getResponses(),
            new Function<Response, Integer>() {
                public Integer apply(Response response) {
                    return response.getCode();
                }
            });
    ImmutableMap<Integer, Response> translatedResponses = Maps.uniqueIndex(translatedOperation.getResponses(),
            new Function<Response, Integer>() {
                public Integer apply(Response response) {
                    return response.getCode();
                }
            });

    assertEquals(savedResponses.size(), translatedResponses.size());
    for (Integer key : savedResponses.keySet()) {
        Response savedResponse = savedResponses.get(key);
        Response translatedResponse = translatedResponses.get(key);
        assertNotNull(savedResponse);
        assertNotNull(translatedResponse);

        assertEquals(savedResponse.getDescription(), translatedResponse.getDescription());

        // both don't exist in Swagger => can't be retrieved
        // assertEquals(savedResponse.getMessage(), translatedResponse.getMessage());
        // assertEquals(savedResponse.getName(), translatedResponse.getName());

        compareRwadefHeaders(savedResponse.getHeaders(), translatedResponse.getHeaders());
        compareRwadefPayloads(savedResponse.getOutputPayLoad(), translatedResponse.getOutputPayLoad());
    }
}

From source file:org.restlet.test.ext.apispark.conversion.swagger.v2_0.Swagger2TestCase.java

private void compareRwadefResources(Contract savedContract, Contract translatedContract) {
    if (assertBothNull(savedContract.getResources(), translatedContract.getResources())) {
        return;/*  w w w.  j  a  v a 2 s.  com*/
    }

    ImmutableMap<String, Resource> savedResources = Maps.uniqueIndex(savedContract.getResources(),
            new Function<Resource, String>() {
                public String apply(Resource resource) {
                    return resource.getResourcePath();
                }
            });
    ImmutableMap<String, Resource> translatedResources = Maps.uniqueIndex(translatedContract.getResources(),
            new Function<Resource, String>() {
                public String apply(Resource resource) {
                    return resource.getResourcePath();
                }
            });

    assertEquals(savedResources.size(), translatedResources.size());
    for (String key : savedResources.keySet()) {
        Resource savedResource = savedResources.get(key);
        Resource translatedResource = translatedResources.get(key);
        assertNotNull(savedResource);
        assertNotNull(translatedResource);

        assertEquals(savedResource.getDescription(), translatedResource.getDescription());
        assertEquals(savedResource.getAuthenticationProtocol(), translatedResource.getAuthenticationProtocol());
        assertEquals(savedResource.getName(), translatedResource.getName());
        compareStringLists(savedResource.getSections(), translatedResource.getSections());

        compareRwadefPathVariables(savedResource, translatedResource);
        compareRwadefOperations(savedResource, translatedResource);
    }
}

From source file:org.restlet.test.ext.apispark.conversion.swagger.v2_0.Swagger2TestCase.java

private void compareRwadefOperations(Resource savedResource, Resource translatedResource) {
    if (assertBothNull(savedResource.getOperations(), translatedResource.getOperations())) {
        return;/*from   ww w.  j  ava2  s. c  o m*/
    }

    ImmutableMap<String, Operation> savedOperations = Maps.uniqueIndex(savedResource.getOperations(),
            new Function<Operation, String>() {
                public String apply(Operation operation) {
                    return operation.getName();
                }
            });
    ImmutableMap<String, Operation> translatedOperations = Maps.uniqueIndex(translatedResource.getOperations(),
            new Function<Operation, String>() {
                public String apply(Operation operation) {
                    return operation.getName();
                }
            });

    assertEquals(savedOperations.size(), translatedOperations.size());
    for (String key : savedOperations.keySet()) {
        Operation savedOperation = savedOperations.get(key);
        Operation translatedOperation = translatedOperations.get(key);
        assertNotNull(savedOperation);
        assertNotNull(translatedOperation);

        assertEquals(savedOperation.getDescription(), translatedOperation.getDescription());
        assertEquals(savedOperation.getMethod(), translatedOperation.getMethod());

        compareRwadefHeaders(savedOperation.getHeaders(), translatedOperation.getHeaders());
        compareRwadefQueryParameters(savedOperation, translatedOperation);
        compareRwadefPayloads(savedOperation.getInputPayLoad(), translatedOperation.getInputPayLoad());
        compareRwadefResponses(savedOperation, translatedOperation);

        compareStringLists(savedOperation.getProduces(), translatedOperation.getProduces());
        compareStringLists(savedOperation.getConsumes(), translatedOperation.getConsumes());
    }
}

From source file:org.restlet.test.ext.apispark.conversion.swagger.v2_0.Swagger2TestCase.java

private void compareRwadefPathVariables(Resource savedResource, Resource translatedResource) {
    if (assertBothNull(savedResource.getPathVariables(), translatedResource.getPathVariables())) {
        return;/*from   ww  w  . j  ava 2 s .  c  om*/
    }

    ImmutableMap<String, PathVariable> savedPathVariables = Maps.uniqueIndex(savedResource.getPathVariables(),
            new Function<PathVariable, String>() {
                public String apply(PathVariable pathVariable) {
                    return pathVariable.getName();
                }
            });
    ImmutableMap<String, PathVariable> translatedPathVariables = Maps
            .uniqueIndex(translatedResource.getPathVariables(), new Function<PathVariable, String>() {
                public String apply(PathVariable pathVariable) {
                    return pathVariable.getName();
                }
            });

    assertEquals(savedPathVariables.size(), translatedPathVariables.size());
    for (String key1 : savedPathVariables.keySet()) {
        PathVariable savedPathVariable = savedPathVariables.get(key1);
        PathVariable translatedPathVariable = translatedPathVariables.get(key1);
        assertNotNull(savedPathVariable);
        assertNotNull(translatedPathVariable);

        assertEquals(savedPathVariable.getDescription(), translatedPathVariable.getDescription());
        assertEquals(savedPathVariable.getExample(), translatedPathVariable.getExample());
        assertEquals(savedPathVariable.getType(), translatedPathVariable.getType());
        assertEquals(savedPathVariable.isRequired(), translatedPathVariable.isRequired());
    }
}

From source file:org.restlet.test.ext.apispark.conversion.swagger.v2_0.Swagger2TestCase.java

private void compareRwadefQueryParameters(Operation savedOperation, Operation translatedOperation) {
    if (assertBothNull(savedOperation.getQueryParameters(), translatedOperation.getQueryParameters())) {
        return;//from ww  w  .  ja  v  a  2 s . c om
    }

    ImmutableMap<String, QueryParameter> savedQueryParameters = Maps
            .uniqueIndex(savedOperation.getQueryParameters(), new Function<QueryParameter, String>() {
                public String apply(QueryParameter header) {
                    return header.getName();
                }
            });
    ImmutableMap<String, QueryParameter> translatedQueryParameters = Maps
            .uniqueIndex(translatedOperation.getQueryParameters(), new Function<QueryParameter, String>() {
                public String apply(QueryParameter header) {
                    return header.getName();
                }
            });

    assertEquals(savedQueryParameters.size(), translatedQueryParameters.size());
    for (String key : savedQueryParameters.keySet()) {
        QueryParameter savedQueryParameter = savedQueryParameters.get(key);
        QueryParameter translatedQueryParameter = translatedQueryParameters.get(key);
        assertNotNull(savedQueryParameter);
        assertNotNull(translatedQueryParameter);

        assertEquals(savedQueryParameter.getDefaultValue(), translatedQueryParameter.getDefaultValue());
        assertEquals(savedQueryParameter.getDescription(), translatedQueryParameter.getDescription());
        assertEquals(savedQueryParameter.getType(), translatedQueryParameter.getType());
        assertEquals(savedQueryParameter.getExample(), translatedQueryParameter.getExample());
        assertEquals(savedQueryParameter.getSeparator(), translatedQueryParameter.getSeparator());
        assertEquals(savedQueryParameter.isRequired(), translatedQueryParameter.isRequired());
        // TODO: not available in Swagger 2.0
        // assertEquals(savedQueryParameter.isAllowMultiple(), translatedQueryParameter.isAllowMultiple());
        compareStringLists(savedQueryParameter.getEnumeration(), translatedQueryParameter.getEnumeration());
    }
}

From source file:org.restlet.test.ext.apispark.conversion.swagger.v2_0.Swagger2TestCase.java

private void compareRwadefRepresentations(Contract savedContract, Contract translatedContract) {
    if (assertBothNull(savedContract.getRepresentations(), translatedContract.getRepresentations())) {
        return;//w  w w.  ja va 2s. c om
    }

    ImmutableMap<String, Representation> savedRepresentations = Maps
            .uniqueIndex(savedContract.getRepresentations(), new Function<Representation, String>() {
                public String apply(Representation representation) {
                    return representation.getName();
                }
            });
    ImmutableMap<String, Representation> translatedRepresentations = Maps
            .uniqueIndex(translatedContract.getRepresentations(), new Function<Representation, String>() {
                public String apply(Representation representation) {
                    return representation.getName();
                }
            });

    assertEquals(savedRepresentations.size(), translatedRepresentations.size());
    for (String key : savedRepresentations.keySet()) {
        Representation savedRepresentation = savedRepresentations.get(key);
        Representation translatedRepresentation = translatedRepresentations.get(key);
        assertNotNull(savedRepresentation);
        assertNotNull(translatedRepresentation);
        assertEquals(savedRepresentation.getDescription(), translatedRepresentation.getDescription());
        assertEquals(savedRepresentation.getExtendedType(), translatedRepresentation.getExtendedType());

        compareStringLists(savedRepresentation.getSections(), translatedRepresentation.getSections());
        compareRwadefProperties(savedRepresentation.getProperties(), translatedRepresentation.getProperties());
    }
}

From source file:com.google.devtools.build.lib.skyframe.PackageFunction.java

/**
 * Fetch the skylark loads for this BUILD file. If any of them haven't been computed yet,
 * returns null.//from  ww w.  j  a va 2s .  c  o m
 */
@Nullable
static SkylarkImportResult fetchImportsFromBuildFile(Path buildFilePath, PackageIdentifier packageId,
        BuildFileAST buildFileAST, Environment env,
        SkylarkImportLookupFunction skylarkImportLookupFunctionForInlining)
        throws PackageFunctionException, InterruptedException {
    Preconditions.checkArgument(!packageId.getRepository().isDefault());

    ImmutableList<SkylarkImport> imports = buildFileAST.getImports();
    Map<String, Extension> importMap = Maps.newHashMapWithExpectedSize(imports.size());
    ImmutableList.Builder<SkylarkFileDependency> fileDependencies = ImmutableList.builder();
    ImmutableMap<String, Label> importPathMap;

    // Find the labels corresponding to the load statements.
    Label labelForCurrBuildFile;
    try {
        labelForCurrBuildFile = Label.create(packageId, "BUILD");
    } catch (LabelSyntaxException e) {
        // Shouldn't happen; the Label is well-formed by construction.
        throw new IllegalStateException(e);
    }
    try {
        importPathMap = SkylarkImportLookupFunction.findLabelsForLoadStatements(imports, labelForCurrBuildFile,
                env);
        if (importPathMap == null) {
            return null;
        }
    } catch (SkylarkImportFailedException e) {
        throw new PackageFunctionException(new BuildFileContainsErrorsException(packageId, e.getMessage()),
                Transience.PERSISTENT);
    }

    // Look up and load the imports.
    ImmutableCollection<Label> importLabels = importPathMap.values();
    List<SkyKey> importLookupKeys = Lists.newArrayListWithExpectedSize(importLabels.size());
    boolean inWorkspace = buildFilePath.getBaseName().endsWith("WORKSPACE");
    for (Label importLabel : importLabels) {
        importLookupKeys.add(SkylarkImportLookupValue.key(importLabel, inWorkspace));
    }
    Map<SkyKey, SkyValue> skylarkImportMap = Maps.newHashMapWithExpectedSize(importPathMap.size());
    boolean valuesMissing = false;

    try {
        if (skylarkImportLookupFunctionForInlining == null) {
            // Not inlining
            Map<SkyKey, ValueOrException2<SkylarkImportFailedException, InconsistentFilesystemException>> skylarkLookupResults = env
                    .getValuesOrThrow(importLookupKeys, SkylarkImportFailedException.class,
                            InconsistentFilesystemException.class);
            valuesMissing = env.valuesMissing();
            for (Map.Entry<SkyKey, ValueOrException2<SkylarkImportFailedException, InconsistentFilesystemException>> entry : skylarkLookupResults
                    .entrySet()) {
                // Fetching the value will raise any deferred exceptions
                skylarkImportMap.put(entry.getKey(), entry.getValue().get());
            }
        } else {
            // Inlining calls to SkylarkImportLookupFunction
            LinkedHashMap<Label, SkylarkImportLookupValue> alreadyVisitedImports = Maps
                    .newLinkedHashMapWithExpectedSize(importLookupKeys.size());
            for (SkyKey importLookupKey : importLookupKeys) {
                SkyValue skyValue = skylarkImportLookupFunctionForInlining
                        .computeWithInlineCalls(importLookupKey, env, alreadyVisitedImports);
                if (skyValue == null) {
                    Preconditions.checkState(env.valuesMissing(), "no skylark import value for %s",
                            importLookupKey);
                    // We continue making inline calls even if some requested values are missing, to
                    // maximize the number of dependent (non-inlined) SkyFunctions that are requested, thus
                    // avoiding a quadratic number of restarts.
                    valuesMissing = true;
                } else {
                    skylarkImportMap.put(importLookupKey, skyValue);
                }
            }

        }
    } catch (SkylarkImportFailedException e) {
        throw new PackageFunctionException(new BuildFileContainsErrorsException(packageId, e.getMessage()),
                Transience.PERSISTENT);
    } catch (InconsistentFilesystemException e) {
        throw new PackageFunctionException(new NoSuchPackageException(packageId, e.getMessage(), e),
                Transience.PERSISTENT);
    }

    if (valuesMissing) {
        // Some imports are unavailable.
        return null;
    }

    // Process the loaded imports.
    for (Entry<String, Label> importEntry : importPathMap.entrySet()) {
        String importString = importEntry.getKey();
        Label importLabel = importEntry.getValue();
        SkyKey keyForLabel = SkylarkImportLookupValue.key(importLabel, inWorkspace);
        SkylarkImportLookupValue importLookupValue = (SkylarkImportLookupValue) skylarkImportMap
                .get(keyForLabel);
        importMap.put(importString, importLookupValue.getEnvironmentExtension());
        fileDependencies.add(importLookupValue.getDependency());
    }

    return new SkylarkImportResult(importMap, transitiveClosureOfLabels(fileDependencies.build()));
}

From source file:org.locationtech.geogig.storage.postgresql.PGObjectStore.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//w w  w .  j a  va  2  s  .  c  om
public <T extends RevObject> Iterator<T> getAll(Iterable<ObjectId> ids, BulkOpListener listener,
        Class<T> type) {

    checkNotNull(ids, "ids is null");
    checkNotNull(listener, "listener is null");
    checkNotNull(type, "type is null");
    checkState(isOpen(), "Database is closed");
    config.checkRepositoryExists();

    final Set<ObjectId> queryIds = ids instanceof Set ? (Set<ObjectId>) ids : Sets.newHashSet(ids);

    ImmutableMap<ObjectId, byte[]> cached = byteCache.getAllPresent(queryIds);

    Iterator<T> hits = Collections.emptyIterator();
    Iterator<T> stream = Collections.emptyIterator();

    if (!cached.isEmpty()) {

        Map<ObjectId, T> cachedObjects = Maps.transformEntries(cached, (id, bytes) -> {
            RevObject o = encoder.decode(id, bytes);
            if (type.isAssignableFrom(o.getClass())) {
                listener.found(id, Integer.valueOf(bytes.length));
                return type.cast(o);
            }
            listener.notFound(id);
            return null;
        });

        hits = Iterators.filter(cachedObjects.values().iterator(), Predicates.notNull());
    }
    if (queryIds.size() > cached.size()) {
        Set<ObjectId> misses = Sets.difference(queryIds, cached.keySet());
        stream = new GetAllIterator(dataSource, misses.iterator(), type, listener, this);
    }

    return Iterators.concat(hits, stream);
}

From source file:io.druid.db.DatabaseRuleManager.java

public void poll() {
    try {/*ww  w .ja  v a2  s  .c  om*/
        ImmutableMap<String, List<Rule>> newRules = ImmutableMap
                .copyOf(dbi.withHandle(new HandleCallback<Map<String, List<Rule>>>() {
                    @Override
                    public Map<String, List<Rule>> withHandle(Handle handle) throws Exception {
                        return handle.createQuery(
                                // Return latest version rule by dataSource
                                String.format("SELECT r.dataSource, r.payload " + "FROM %1$s r "
                                        + "INNER JOIN(SELECT dataSource, max(version) as version FROM %1$s GROUP BY dataSource) ds "
                                        + "ON r.datasource = ds.datasource and r.version = ds.version",
                                        getRulesTable()))
                                .fold(Maps.<String, List<Rule>>newHashMap(),
                                        new Folder3<Map<String, List<Rule>>, Map<String, Object>>() {
                                            @Override
                                            public Map<String, List<Rule>> fold(Map<String, List<Rule>> retVal,
                                                    Map<String, Object> stringObjectMap,
                                                    FoldController foldController,
                                                    StatementContext statementContext) throws SQLException {

                                                try {
                                                    String dataSource = MapUtils.getString(stringObjectMap,
                                                            "dataSource");
                                                    List<Rule> rules = jsonMapper.readValue(
                                                            MapUtils.getString(stringObjectMap, "payload"),
                                                            new TypeReference<List<Rule>>() {
                                                            });
                                                    retVal.put(dataSource, rules);
                                                    return retVal;
                                                } catch (Exception e) {
                                                    throw Throwables.propagate(e);
                                                }
                                            }
                                        });
                    }
                }));

        log.info("Polled and found rules for %,d datasource(s)", newRules.size());

        rules.set(newRules);
    } catch (Exception e) {
        log.error(e, "Exception while polling for rules");
    }
}