Example usage for com.google.common.collect Maps filterKeys

List of usage examples for com.google.common.collect Maps filterKeys

Introduction

In this page you can find the example usage for com.google.common.collect Maps filterKeys.

Prototype

@CheckReturnValue
public static <K, V> BiMap<K, V> filterKeys(BiMap<K, V> unfiltered, final Predicate<? super K> keyPredicate) 

Source Link

Document

Returns a bimap containing the mappings in unfiltered whose keys satisfy a predicate.

Usage

From source file:org.apache.jackrabbit.oak.blob.cloud.aws.s3.S3Backend.java

public void init(CachingDataStore store, String homeDir, Properties prop) throws DataStoreException {

    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {/*  w ww  . j  a v a 2s.  c om*/
        startTime = new Date();
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        LOG.debug("init");
        this.store = store;
        s3ReqDecorator = new S3RequestDecorator(prop);
        s3service = Utils.openService(prop);
        if (bucket == null || "".equals(bucket.trim())) {
            bucket = prop.getProperty(S3Constants.S3_BUCKET);
        }
        String region = prop.getProperty(S3Constants.S3_REGION);
        Region s3Region = null;
        if (StringUtils.isNullOrEmpty(region)) {
            com.amazonaws.regions.Region ec2Region = Regions.getCurrentRegion();
            if (ec2Region != null) {
                s3Region = Region.fromValue(ec2Region.getName());
            } else {
                throw new AmazonClientException("parameter [" + S3Constants.S3_REGION
                        + "] not configured and cannot be derived from environment");
            }
        } else {
            if (Utils.DEFAULT_AWS_BUCKET_REGION.equals(region)) {
                s3Region = Region.US_Standard;
            } else if (Region.EU_Ireland.toString().equals(region)) {
                s3Region = Region.EU_Ireland;
            } else {
                s3Region = Region.fromValue(region);
            }
        }

        if (!s3service.doesBucketExist(bucket)) {
            s3service.createBucket(bucket, s3Region);
            LOG.info("Created bucket [{}] in [{}] ", bucket, region);
        } else {
            LOG.info("Using bucket [{}] in [{}] ", bucket, region);
        }

        int writeThreads = 10;
        String writeThreadsStr = prop.getProperty(S3Constants.S3_WRITE_THREADS);
        if (writeThreadsStr != null) {
            writeThreads = Integer.parseInt(writeThreadsStr);
        }
        LOG.info("Using thread pool of [{}] threads in S3 transfer manager.", writeThreads);
        tmx = new TransferManager(s3service, (ThreadPoolExecutor) Executors.newFixedThreadPool(writeThreads,
                new NamedThreadFactory("s3-transfer-manager-worker")));

        int asyncWritePoolSize = 10;
        String maxConnsStr = prop.getProperty(S3Constants.S3_MAX_CONNS);
        if (maxConnsStr != null) {
            asyncWritePoolSize = Integer.parseInt(maxConnsStr) - writeThreads;
        }

        asyncWriteExecuter = (ThreadPoolExecutor) Executors.newFixedThreadPool(asyncWritePoolSize,
                new NamedThreadFactory("s3-write-worker"));
        String renameKeyProp = prop.getProperty(S3Constants.S3_RENAME_KEYS);
        boolean renameKeyBool = (renameKeyProp == null || "".equals(renameKeyProp)) ? false
                : Boolean.parseBoolean(renameKeyProp);
        LOG.info("Rename keys [{}]", renameKeyBool);
        if (renameKeyBool) {
            renameKeys();
        }
        LOG.debug("S3 Backend initialized in [{}] ms", +(System.currentTimeMillis() - startTime.getTime()));
    } catch (Exception e) {
        LOG.debug("  error ", e);
        Map<String, String> filteredMap = Maps.newHashMap();
        if (prop != null) {
            filteredMap = Maps.filterKeys(Maps.fromProperties(prop), new Predicate<String>() {
                @Override
                public boolean apply(String input) {
                    return !input.equals(S3Constants.ACCESS_KEY) && !input.equals(S3Constants.SECRET_KEY);
                }
            });
        }
        throw new DataStoreException("Could not initialize S3 from " + filteredMap, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}

From source file:com.google.enterprise.connector.db.diffing.LobDocumentBuilder.java

/**
 * Returns a filtered Map of the row with the LOB field filtered out.
 *
 * @param row//from ww  w. j  a v  a  2s . c o  m
 * @return map representing a database table row.
 */
private Map<String, Object> getRowForXmlDoc(Map<String, Object> row) {
    return Maps.filterKeys(row, new Predicate<String>() {
        @Override
        public boolean apply(String key) {
            return !dbContext.getLobField().equals(key);
        }
    });
}

From source file:net.hillsdon.reviki.vc.impl.SVNPageStore.java

public VersionedPageInfo get(final PageReference ref, final long revision) throws PageStoreException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Map<String, String> properties = new HashMap<String, String>();
    SVNNodeKind kind = _operations.checkPath(ref.getPath(), revision);
    if (SVNNodeKind.FILE.equals(kind)) {
        _operations.getFile(ref.getPath(), revision, properties, baos);
        long actualRevision = SVNProperty.longValue(properties.get(SVNProperty.REVISION));
        long lastChangedRevision = SVNProperty.longValue(properties.get(SVNProperty.COMMITTED_REVISION));
        Date lastChangedDate = SVNDate.parseDate(properties.get(SVNProperty.COMMITTED_DATE));
        String lastChangedAuthor = properties.get(SVNProperty.LAST_AUTHOR);
        String lockOwner = null;//from  ww w  .  ja v  a 2  s  .  com
        String lockToken = null;
        Date lockedSince = null;
        final Map<String, String> filteredProperties = Maps.filterKeys(properties, new Predicate<String>() {
            public boolean apply(String key) {
                return key.startsWith(REVIKI_ATTRIBUTE_PREFIX);
            }
        });
        Map<String, String> attributes = stripPrefix(filteredProperties, REVIKI_ATTRIBUTE_PREFIX);
        try {
            if (revision == -1 || _operations.checkPath(ref.getPath(), -1) == SVNNodeKind.FILE) {
                SVNLock lock = _operations.getLock(ref.getPath());
                if (lock != null) {
                    lockOwner = lock.getOwner();
                    lockToken = lock.getID();
                    lockedSince = lock.getCreationDate();
                }
            }
        } catch (NotFoundException ex) {
            // It was a file at 'revision' but is now deleted so we can't get the lock information.
        }
        return new VersionedPageInfoImpl(_wiki, ref.getPath(), Strings.toUTF8(baos.toByteArray()),
                actualRevision, lastChangedRevision, lastChangedAuthor, lastChangedDate, lockOwner, lockToken,
                lockedSince, attributes);
    } else if (SVNNodeKind.NONE.equals(kind)) {
        long pseudoRevision = VersionedPageInfo.UNCOMMITTED;
        long lastChangedRevision = VersionedPageInfo.UNCOMMITTED;
        String lastChangedAuthor = null;
        Date lastChangedDate = null;
        final ChangeInfo deletingChange = getChangeThatDeleted(ref);
        PageLinkTarget renamedTo = null;
        if (deletingChange != null) {
            pseudoRevision = VersionedPageInfo.DELETED;
            lastChangedRevision = deletingChange.getRevision();
            lastChangedAuthor = deletingChange.getUser();
            lastChangedDate = deletingChange.getDate();
            renamedTo = deletingChange.getRenamedTo();
            if (renamedTo != null) {
                pseudoRevision = VersionedPageInfo.RENAMED;
            }
        }
        return new VersionedPageInfoImpl(_wiki, ref.getPath(), "", pseudoRevision, lastChangedRevision,
                lastChangedAuthor, lastChangedDate, null, null, null, renamedTo);
    } else {
        throw new PageStoreException(format("Unexpected node kind '%s' at '%s'", kind, ref));
    }
}

From source file:clocker.docker.location.DockerContainerLocation.java

@Override
public int execCommands(Map<String, ?> props, String summaryForLogging, List<String> commands,
        Map<String, ?> env) {
    Iterable<String> filtered = Iterables.filter(commands, DockerCallbacks.FILTER);
    for (String commandString : filtered) {
        parseDockerCallback(commandString);
    }/* w  ww.  j a  v a 2  s  . c o m*/
    boolean entitySsh = Boolean.TRUE.equals(entity.config().get(DockerContainer.DOCKER_USE_SSH));
    boolean dockerSsh = Boolean.TRUE.equals(getOwner().config().get(DockerContainer.DOCKER_USE_SSH));
    if (entitySsh && dockerSsh) {
        return super.execCommands(props, summaryForLogging, commands, env);
    } else {
        Map<String, ?> nonPortProps = Maps.filterKeys(props,
                Predicates.not(Predicates.containsPattern("port")));
        return hostMachine.execCommands(nonPortProps, summaryForLogging, getDockerExecCommand(commands, env));
    }
}

From source file:com.twitter.aurora.scheduler.async.TaskTimeout.java

private void exportStats(StatsProvider statsProvider) {
    statsProvider.makeGauge(TRANSIENT_COUNT_STAT_NAME, new Supplier<Number>() {
        @Override//from  w  w w .  j  a v  a 2 s  .c  o m
        public Number get() {
            return futures.size();
        }
    });

    for (final ScheduleStatus status : TRANSIENT_STATES) {
        statsProvider.makeGauge(waitingTimeStatName(status), new Supplier<Number>() {
            private final Predicate<TimeoutKey> statusMatcher = new Predicate<TimeoutKey>() {
                @Override
                public boolean apply(TimeoutKey key) {
                    return key.status == status;
                }
            };

            @Override
            public Number get() {
                Iterable<Context> matches = Maps.filterKeys(futures, statusMatcher).values();
                if (Iterables.isEmpty(matches)) {
                    return 0L;
                } else {
                    return clock.nowMillis() - TIMESTAMP_ORDER.min(matches).timestampMillis;
                }
            }
        });
    }
}

From source file:com.android.builder.internal.packaging.IncrementalPackager.java

/**
 * Updates resources in the archive./* w  ww .  j  a v a  2  s . c o m*/
 *
 * @param files the resources to update
 * @throws IOException failed to update the archive
 */
public void updateJavaResources(@NonNull ImmutableMap<RelativeFile, FileStatus> files) throws IOException {
    /*
     * There is a bug somewhere in the proguard build tasks that places .class files as
     * resources. These will be removed here, but this filtering code can -- and should -- be
     * removed once that bug is fixed.
     */
    Predicate<String> isNotClassFile = new Predicate<String>() {
        @Override
        public boolean apply(String input) {
            return !input.endsWith(SdkConstants.DOT_CLASS);
        }
    };

    updateFiles(PackagedFileUpdates.fromIncrementalRelativeFileSet(
            Maps.filterKeys(files, Predicates.compose(isNotClassFile, RelativeFile.EXTRACT_PATH))));
}

From source file:org.locationtech.geogig.porcelain.RemoveOp.java

private Map<String, NodeRef> getDeleteTrees(RevTree workTree) {

    Iterator<NodeRef> childTrees = command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_TREES)
            .setReference(workTree.getId().toString()).call();

    ImmutableMap<String, NodeRef> treesByPath = Maps.uniqueIndex(childTrees, (ref) -> ref.path());

    Set<String> requestedTrees = Sets.intersection(treesByPath.keySet(), new HashSet<>(pathsToRemove));
    Predicate<String> keyPredicate = Predicates.in(requestedTrees);
    Map<String, NodeRef> requestedTreesMap = Maps.filterKeys(treesByPath, keyPredicate);
    return requestedTreesMap;
}

From source file:org.apache.gobblin.broker.DefaultBrokerCache.java

/**
 * Invalidate all objects at scopes which are descendant of the input scope. Any such invalidated object that is a
 * {@link Closeable} will be closed, and any such object which is a {@link Service} will be shutdown.
 * @throws IOException// www .  ja  v  a  2 s.  c o m
 */
public void close(ScopeWrapper<S> scope) throws IOException {
    List<Throwable> exceptionsList = Lists.newArrayList();
    List<Service> awaitShutdown = Lists.newArrayList();

    for (Map.Entry<RawJobBrokerKey, Object> entry : Maps
            .filterKeys(this.sharedResourceCache.asMap(), new ScopeIsAncestorFilter(scope)).entrySet()) {

        this.sharedResourceCache.invalidate(entry.getKey());

        if (entry.getValue() instanceof ResourceInstance) {
            Object obj = ((ResourceInstance) entry.getValue()).getResource();
            // Catch unchecked exception while closing resources, make sure all resources managed by cache are closed.
            try {
                SharedResourcesBrokerUtils.shutdownObject(obj, log);
            } catch (Throwable t) {
                exceptionsList.add(t);
            }
            if (obj instanceof Service) {
                awaitShutdown.add((Service) obj);
            }
        }
    }

    for (Service service : awaitShutdown) {
        try {
            service.awaitTerminated(10, TimeUnit.SECONDS);
        } catch (TimeoutException te) {
            log.error("Failed to shutdown {}.", service);
        }
    }

    // log exceptions while closing resources up.
    if (exceptionsList.size() > 0) {
        log.error(exceptionsList.stream().map(Throwables::getStackTraceAsString)
                .collect(Collectors.joining("\n")));
    }
}

From source file:com.facebook.buck.parser.PythonDslProjectBuildFileParser.java

/** Initialize the parser, starting buck.py. */
private void init() throws IOException {
    try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(buckEventBus, PerfEventId.of("ParserInit"))) {

        ImmutableMap.Builder<String, String> pythonEnvironmentBuilder = ImmutableMap
                .builderWithExpectedSize(environment.size());
        // Strip out PYTHONPATH. buck.py manually sets this to include only nailgun. We don't want
        // to inject nailgun into the parser's PYTHONPATH, so strip that value out.
        // If we wanted to pass on some environmental PYTHONPATH, we would have to do some actual
        // merging of this and the BuckConfig's python module search path.
        // Also ignore PYTHONHASHSEED environment variable passed by clients since Buck manages it to
        // prevent non-determinism.
        pythonEnvironmentBuilder.putAll(Maps.filterKeys(environment,
                k -> !PYTHONPATH_ENV_VAR_NAME.equals(k) && !PYTHON_HASH_SEED_ENV_VAR_NAME.equals(k)));
        // set Python hash seed to a fixed number to make parsing reproducible
        pythonEnvironmentBuilder.put(PYTHON_HASH_SEED_ENV_VAR_NAME, PYTHON_HASH_SEED_VALUE);

        if (options.getPythonModuleSearchPath().isPresent()) {
            pythonEnvironmentBuilder.put(PYTHONPATH_ENV_VAR_NAME, options.getPythonModuleSearchPath().get());
        }//www.j  a va 2s. c om

        ImmutableMap<String, String> pythonEnvironment = pythonEnvironmentBuilder.build();

        ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(buildArgs())
                .setEnvironment(pythonEnvironment).build();

        LOG.debug("Starting buck.py command: %s environment: %s", params.getCommand(), params.getEnvironment());
        buckPyProcess = processExecutor.launchProcess(params);
        LOG.debug("Started process %s successfully", buckPyProcess);
        buckPyProcessInput = createParserInputStream(Objects.requireNonNull(buckPyProcess).getInputStream(),
                processedBytes.isPresent());
        buckPyProcessJsonGenerator = ObjectMappers.createGenerator(buckPyProcess.getOutputStream());
        // We have to wait to create the JsonParser until after we write our
        // first request, because Jackson "helpfully" synchronously reads
        // from the InputStream trying to detect whether the encoding is
        // UTF-8 or UTF-16 as soon as you create a JsonParser:
        //
        // https://git.io/vSgnA
        //
        // Since buck.py doesn't write any data until after it receives
        // a query, creating the JsonParser here would hang indefinitely.

        InputStream stderr = buckPyProcess.getErrorStream();

        AtomicInteger numberOfLines = new AtomicInteger(0);
        AtomicReference<Path> lastPath = new AtomicReference<Path>();
        InputStreamConsumer stderrConsumer = new InputStreamConsumer(stderr,
                (InputStreamConsumer.Handler) line -> {
                    Path path = currentBuildFile.get();
                    if (!Objects.equals(path, lastPath.get())) {
                        numberOfLines.set(0);
                        lastPath.set(path);
                    }
                    int count = numberOfLines.getAndIncrement();
                    if (count == 0) {
                        buckEventBus.post(ConsoleEvent.warning("WARNING: Output when parsing %s:", path));
                    }
                    buckEventBus.post(ConsoleEvent.warning("| %s", line));
                });
        stderrConsumerTerminationFuture = new FutureTask<>(stderrConsumer);
        stderrConsumerThread = Threads.namedThread(PythonDslProjectBuildFileParser.class.getSimpleName(),
                stderrConsumerTerminationFuture);
        stderrConsumerThread.start();
    }
}

From source file:com.isotrol.impe3.web20.impl.MigrationServiceImpl.java

private MemberEntity fillMember(MemberEntity entity, MemberDTO dto) {
    final Calendar date = Calendar.getInstance();
    date.setTime(dto.getDate());//from w  w  w.ja v a  2  s .c om
    entity.setDate(date);
    entity.setDisplayName(dto.getDisplayName());
    entity.setEmail(dto.getEmail());
    entity.setMemberCode(dto.getCode());
    entity.setName(dto.getName());
    entity.setBlocked(dto.isBlocked());

    final Set<String> profiles = entity.getProfiles();
    profiles.clear();
    final Set<String> dtopf = dto.getProfiles();
    if (dtopf != null) {
        profiles.addAll(Sets.filter(dtopf, notNull()));
    }
    final Map<String, String> properties = entity.getProperties();
    properties.clear();
    final Map<String, String> dtopr = dto.getProperties();
    if (dtopr != null) {
        properties.putAll(Maps.filterKeys(Maps.filterValues(dtopr, notNull()), notNull()));
    }

    return entity;
}