Example usage for java.util Collection removeIf

List of usage examples for java.util Collection removeIf

Introduction

In this page you can find the example usage for java.util Collection removeIf.

Prototype

default boolean removeIf(Predicate<? super E> filter) 

Source Link

Document

Removes all of the elements of this collection that satisfy the given predicate.

Usage

From source file:com.baifendian.swordfish.common.mail.MailSendUtil.java

/**
 * ??/*from w w  w  .  j  a  v a 2  s .  c  o  m*/
 *
 * @param receivers
 * @param title
 * @param content
 * @return
 */
public static boolean sendMails(Collection<String> receivers, String title, String content) {
    if (receivers == null) {
        LOGGER.error("Mail receivers is null.");
        return false;
    }

    receivers.removeIf((from) -> (StringUtils.isEmpty(from)));

    if (receivers.isEmpty()) {
        LOGGER.error("Mail receivers is empty.");
        return false;
    }

    // ?? email
    HtmlEmail email = new HtmlEmail();

    try {
        //  SMTP ?????, 163 "smtp.163.com"
        email.setHostName(mailServerHost);

        email.setSmtpPort(mailServerPort);

        // ?
        email.setCharset("UTF-8");
        // 
        for (String receiver : receivers) {
            email.addTo(receiver);
        }
        // ??
        email.setFrom(mailSender, mailSender);
        // ???????-??????
        email.setAuthentication(mailSender, mailPasswd);
        // ???
        email.setSubject(title);
        // ???? HtmlEmail? HTML 
        email.setMsg(content);
        // ??
        email.send();

        return true;
    } catch (Throwable e) {
        LOGGER.error("Send email to {} failed", StringUtils.join(",", receivers), e);
    }

    return false;
}

From source file:org.trustedanalytics.cloud.cc.FeignClient.java

@Override
public Collection<CcOrgPermission> getUserPermissions(UUID user, Collection<UUID> orgsFilter) {
    Collection<CcOrg> orgs = userResource.getUserOrganizations(user).getOrgs();

    if (!orgsFilter.isEmpty()) {
        orgs.removeIf(ccOrg -> !orgsFilter.contains(ccOrg.getGuid()));
    }//from www  . j a va  2 s  .  c o  m

    Collection<CcOrg> managedOrganizations = getManagedOrganizations(user);
    Collection<CcOrg> auditedOrganizations = getAuditedOrganizations(user);
    Collection<CcOrg> billingManagedOrganizations = getBillingManagedOrganizations(user);

    Collection<CcOrgPermission> permissions = new ArrayList<>();
    orgs.forEach(org -> {
        boolean isManager = managedOrganizations.contains(org);
        boolean isAuditor = auditedOrganizations.contains(org);
        boolean isBillingManager = billingManagedOrganizations.contains(org);
        permissions.add(new CcOrgPermission(org, isManager, isAuditor, isBillingManager));
    });

    return permissions;
}

From source file:org.trustedanalytics.cloud.cc.CcClient.java

@Override
public Collection<CcOrgPermission> getUserPermissions(UUID user, Collection<UUID> orgsFilter) {

    Collection<CcOrg> orgs = getUserOrgs(user);
    if (!orgsFilter.isEmpty()) {
        orgs.removeIf(ccOrg -> !orgsFilter.contains(ccOrg.getGuid()));
    }/*from   w  w w . ja va 2 s .  c om*/

    Collection<CcOrg> managedOrganizations = getManagedOrganizations(user);
    Collection<CcOrg> auditedOrganizations = getAuditedOrganizations(user);
    Collection<CcOrg> billingManagedOrganizations = getBillingManagedOrganizations(user);

    Collection<CcOrgPermission> permissions = new ArrayList<>();
    orgs.forEach(org -> {
        boolean isManager = managedOrganizations.contains(org);
        boolean isAuditor = auditedOrganizations.contains(org);
        boolean isBillingManager = billingManagedOrganizations.contains(org);
        permissions.add(new CcOrgPermission(org, isManager, isAuditor, isBillingManager));
    });

    return permissions;
}

From source file:ch.wisv.areafiftylan.integration.TeamRestIntegrationTest.java

@Test
public void testDeclineInviteAsUser() {
    User captain = createUser();//  ww  w .j av a2s  . com
    User user = createUser();
    Team team = createTeamWithCaptain(captain);

    TeamInviteToken token = teamInviteTokenRepository.save(new TeamInviteToken(user, team));

    //@formatter:off
    given().header(getXAuthTokenHeaderForUser(user)).when().body(token.getToken())
            .delete(TEAM_ENDPOINT + "invites").then().statusCode(HttpStatus.SC_OK);
    //@formatter:on

    Collection<TeamInviteToken> tokens = teamInviteTokenRepository.findByUserEmailIgnoreCase(user.getEmail());
    tokens.removeIf(t -> !t.isValid());

    assertTrue(tokens.isEmpty());
}

From source file:ch.wisv.areafiftylan.integration.TeamRestIntegrationTest.java

@Test
public void testRevokeInviteAsCaptain() {
    User captain = createUser();/*w w w  .ja v a  2s. c o m*/
    User user = createUser();
    Team team = createTeamWithCaptain(captain);

    TeamInviteToken token = teamInviteTokenRepository.save(new TeamInviteToken(user, team));

    //@formatter:off
    given().header(getXAuthTokenHeaderForUser(captain)).when().body(token.getToken())
            .delete(TEAM_ENDPOINT + "invites").then().statusCode(HttpStatus.SC_OK);
    //@formatter:on

    Collection<TeamInviteToken> tokens = teamInviteTokenRepository.findByUserEmailIgnoreCase(user.getEmail());
    tokens.removeIf(t -> !t.isValid());

    assertTrue(tokens.isEmpty());
}

From source file:ch.wisv.areafiftylan.TeamRestIntegrationTest.java

@Test
public void testDeclineInviteAsUser() {
    String location = createTeamWithCaptain();

    inviteUserAsCaptain(location, user);

    TeamInviteToken token = teamInviteTokenRepository.findByUserUsernameIgnoreCase(user.getUsername()).stream()
            .findFirst().get();// w w w  .j  a v  a  2 s  .  c  o m

    SessionData login = login("user", userCleartextPassword);

    //@formatter:off
    given().filter(sessionFilter).header(login.getCsrfHeader()).when().content(token.getToken())
            .delete("/teams/invites").then().statusCode(HttpStatus.SC_OK);
    //@formatter:on

    Collection<TeamInviteToken> tokens = teamInviteTokenRepository
            .findByUserUsernameIgnoreCase(user.getUsername());
    tokens.removeIf(t -> !t.isValid());

    Assert.assertTrue(tokens.isEmpty());
}

From source file:com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.agent.KubernetesV2OnDemandCachingAgent.java

protected void mergeCacheResults(Map<String, Collection<CacheData>> current,
        Map<String, Collection<CacheData>> added) {
    for (String group : added.keySet()) {
        Collection<CacheData> currentByGroup = current.get(group);
        Collection<CacheData> addedByGroup = added.get(group);

        currentByGroup = currentByGroup == null ? new ArrayList<>() : currentByGroup;
        addedByGroup = addedByGroup == null ? new ArrayList<>() : addedByGroup;

        for (CacheData addedCacheData : addedByGroup) {
            CacheData mergedEntry = currentByGroup.stream()
                    .filter(cd -> cd.getId().equals(addedCacheData.getId())).findFirst()
                    .flatMap(cd -> Optional.of(KubernetesCacheDataConverter.mergeCacheData(cd, addedCacheData)))
                    .orElse(addedCacheData);

            currentByGroup.removeIf(cd -> cd.getId().equals(addedCacheData.getId()));
            currentByGroup.add(mergedEntry);
        }/*from w w  w . j a  va 2s . c om*/

        current.put(group, currentByGroup);
    }
}

From source file:com.codelanx.codelanxlib.command.CommandNode.java

/**
 * Traverses the {@link CommandNode} tree and returns all child objects
 * found that satisfy the following:/*from w  w  w .j  a  v a 2  s .  com*/
 * <br><br>
 * <ul><li> The child's parent is the class that maps to it (aliases will be
 * mapped to, but will not have the mapping parent class as an actual
 * parent).
 * <li> The node is not a {@link HelpCommand}
 * <li> The value of the node's {@link CommandNode#isExecutable()} is
 * {@code true}
 * </ul>
 *
 * @since 0.1.0
 * @version 0.1.0
 *
 * @return All found {@link CommandNode} children for this node
 */
public Collection<CommandNode<? extends Plugin>> traverse() {
    Collection<CommandNode<? extends Plugin>> back = new ArrayList<>();
    back.add(this);
    back = this.subcommands.values().stream().filter(c -> this == c.getParent())
            .filter(c -> c.getClass() != HelpCommand.class).map(c -> c.traverse()).reduce(back, (u, r) -> {
                u.addAll(r);
                return u;
            });
    back.removeIf(c -> !c.isExecutable());
    return back;
}

From source file:org.hyperledger.fabric.sdk.Channel.java

/**
 * Send a transaction  proposal./*from ww  w . ja  va  2 s  . c  o  m*/
 *
 * @param transactionProposalRequest The transaction proposal to be sent to all the required peers needed for endorsing.
 * @param discoveryOptions
 * @return responses from peers.
 * @throws InvalidArgumentException
 * @throws ProposalException
 */
public Collection<ProposalResponse> sendTransactionProposalToEndorsers(
        TransactionProposalRequest transactionProposalRequest, DiscoveryOptions discoveryOptions)
        throws ProposalException, InvalidArgumentException, ServiceDiscoveryException {
    final String chaincodeName = transactionProposalRequest.getChaincodeID().getName();
    checkChannelState();
    if (null == transactionProposalRequest) {
        throw new InvalidArgumentException("The proposalRequest is null");
    }
    if (isNullOrEmpty(transactionProposalRequest.getFcn())) {
        throw new InvalidArgumentException("The proposalRequest's fcn is null or empty.");
    }
    if (transactionProposalRequest.getChaincodeID() == null) {
        throw new InvalidArgumentException("The proposalRequest's chaincode ID is null");
    }
    logger.debug(
            format("Channel %s sendTransactionProposalToEndorsers chaincode name: %s", name, chaincodeName));

    TransactionContext transactionContext = getTransactionContext(transactionProposalRequest.getUserContext());
    transactionContext.verify(transactionProposalRequest.doVerify());
    transactionContext.setProposalWaitTime(transactionProposalRequest.getProposalWaitTime());

    // Protobuf message builder
    ProposalBuilder proposalBuilder = ProposalBuilder.newBuilder();
    proposalBuilder.context(transactionContext);
    proposalBuilder.request(transactionProposalRequest);

    SignedProposal invokeProposal = null;
    try {
        invokeProposal = getSignedProposal(transactionContext, proposalBuilder.build());
    } catch (CryptoException e) {
        throw new InvalidArgumentException(e);
    }
    SDChaindcode sdChaindcode;
    final List<ServiceDiscoveryChaincodeCalls> serviceDiscoveryChaincodeInterests = discoveryOptions
            .getServiceDiscoveryChaincodeInterests();

    if (null != serviceDiscoveryChaincodeInterests && !serviceDiscoveryChaincodeInterests.isEmpty()) {
        final String firstname = serviceDiscoveryChaincodeInterests.get(0).getName();
        if (!firstname.equals(chaincodeName)) {
            serviceDiscoveryChaincodeInterests.add(0, new ServiceDiscoveryChaincodeCalls(chaincodeName));
        }
        List<List<ServiceDiscoveryChaincodeCalls>> ccl = new LinkedList<>();
        ccl.add(serviceDiscoveryChaincodeInterests);
        final Map<String, SDChaindcode> sdChaindcodeMap = serviceDiscovery
                .discoverEndorserEndpoints(transactionContext, ccl);
        if (sdChaindcodeMap == null) {
            throw new ServiceDiscoveryException(
                    format("Channel %s failed doing service discovery for chaincode %s ", name, chaincodeName));
        }
        sdChaindcode = sdChaindcodeMap.get(chaincodeName);

    } else {
        if (discoveryOptions.forceDiscovery) {
            logger.trace("Forcing discovery.");
            serviceDiscovery.networkDiscovery(transactionContext, true);
        }
        sdChaindcode = serviceDiscovery.discoverEndorserEndpoint(transactionContext, chaincodeName);
    }
    logger.trace(format("Channel %s chaincode %s discovered: %s", name, chaincodeName, "" + sdChaindcode));

    if (null == sdChaindcode) {
        throw new ServiceDiscoveryException(
                format("Channel %s failed to find and endorsers for chaincode %s", name, chaincodeName));
    }

    if (sdChaindcode.getLayouts() == null || sdChaindcode.getLayouts().isEmpty()) {
        throw new ServiceDiscoveryException(
                format("Channel %s failed to find and endorsers for chaincode %s no layouts found.", name,
                        chaincodeName));
    }

    SDChaindcode sdChaindcodeEndorsementCopy = new SDChaindcode(sdChaindcode); //copy. no ignored.

    final boolean inspectResults = discoveryOptions.inspectResults;

    if (sdChaindcodeEndorsementCopy.ignoreList(discoveryOptions.getIgnoreList()) < 1) { // apply ignore list
        throw new ServiceDiscoveryException("Applying ignore list reduced to no available endorser options.");
    }

    if (IS_TRACE_LEVEL && null != discoveryOptions.getIgnoreList()
            && !discoveryOptions.getIgnoreList().isEmpty()) {
        logger.trace(format("SDchaincode after ignore list: %s", sdChaindcodeEndorsementCopy));
    }
    final ServiceDiscovery.EndorsementSelector lendorsementSelector = discoveryOptions.endorsementSelector != null
            ? discoveryOptions.endorsementSelector
            : this.endorsementSelector;
    try {

        final Map<SDEndorser, ProposalResponse> goodResponses = new HashMap<>(); // all good endorsements by endpoint
        final Map<SDEndorser, ProposalResponse> allTried = new HashMap<>(); // all tried by endpoint

        boolean done = false;
        int attempts = 1; //safety valve

        do {
            if (IS_TRACE_LEVEL) {
                logger.trace(format("Attempts: %d,  chaincode discovery state: %s", attempts,
                        sdChaindcodeEndorsementCopy));
            }
            final SDEndorserState sdEndorserState = lendorsementSelector
                    .endorserSelector(sdChaindcodeEndorsementCopy);

            if (IS_TRACE_LEVEL) {

                StringBuilder sb = new StringBuilder(1000);
                String sep = "";
                for (SDEndorser sdEndorser : sdEndorserState.getSdEndorsers()) {
                    sb.append(sep).append(sdEndorser);
                    sep = ", ";
                }

                logger.trace(format(
                        "Attempts: %d,  chaincode discovery state: %s. Endorser selector picked: %s. With selected endorsers: %s",
                        attempts, sdChaindcodeEndorsementCopy.name, sdEndorserState.getPickedLayout(),
                        sb.toString()));

            }

            Collection<SDEndorser> ep = sdEndorserState.getSdEndorsers();
            ep = new ArrayList<>(ep); // just in case it's not already a copy

            if (IS_TRACE_LEVEL) {

                StringBuilder sb = new StringBuilder(1000);
                String sep = "";
                for (SDEndorser sdEndorser : ep) {
                    sb.append(sep).append(sdEndorser);
                }
                logger.trace(format("Channel %s, chaincode %s attempts: %d requested endorsements: %s", name,
                        chaincodeName, attempts, sb.toString()));
            }

            //Safety check make sure the selector isn't giving back endpoints to retry
            ep.removeIf(sdEndorser -> goodResponses.keySet().contains(sdEndorser));

            if (ep.isEmpty()) { // this would be odd but lets go with it.
                logger.debug(format(
                        "Channel %s, chaincode %s attempts: %d endorser selector returned no additional endorements needed.",
                        name, chaincodeName, attempts));

                Collection<SDEndorser> needed = sdChaindcode.meetsEndorsmentPolicy(goodResponses.keySet());
                if (needed != null) { // means endorsment meet with those in the needed.
                    ArrayList<ProposalResponse> ret = new ArrayList<>(needed.size());
                    needed.forEach(s -> ret.add(goodResponses.get(s)));

                    if (IS_DEBUG_LEVEL) {

                        StringBuilder sb = new StringBuilder(1000);
                        String sep = "";
                        for (ProposalResponse proposalResponse : ret) {
                            sb.append(sep).append(proposalResponse.getPeer());
                            sep = ", ";
                        }
                        logger.debug(
                                format("Channel %s, chaincode %s attempts: %d got all needed endorsements: %s",
                                        name, chaincodeName, attempts, sb.toString()));
                    }

                    return ret; // the happy path :)!

                } else { //still don't have the needed endorsements.

                    logger.debug(format("Channel %s, chaincode %s attempts: %d missing needed endorsements",
                            name, chaincodeName, attempts));

                    if (inspectResults) {
                        return allTried.values();
                    } else {
                        throw new ServiceDiscoveryException(
                                format("Could not meet endorsement policy for chaincode %s", chaincodeName));
                    }
                }
            }

            Map<String, Peer> lpeerEndpointMap = new HashMap<>(peerEndpointMap);
            Map<SDEndorser, Peer> endorsers = new HashMap<>(ep.size());
            Map<PeerExactMatch, SDEndorser> peer2sdEndorser = new HashMap<>(ep.size());
            for (SDEndorser sdEndorser : ep) {

                Peer epeer = lpeerEndpointMap.get(sdEndorser.getEndpoint());
                if (epeer != null && !epeer.hasConnected()) {
                    // mostly because gossip may have malicious data so if we've not connected update TLS props from chaincode discovery.
                    final Properties properties = epeer.getProperties();

                    final byte[] bytes = combineCerts(sdEndorser.getTLSCerts(),
                            sdEndorser.getTLSIntermediateCerts());
                    properties.put("pemBytes", bytes);
                    epeer.setProperties(properties);

                } else if (null == epeer) {
                    epeer = sdPeerAddition.addPeer(new SDPeerAdditionInfo() {

                        @Override
                        public String getMspId() {
                            return sdEndorser.getMspid();
                        }

                        @Override
                        public String getEndpoint() {
                            return sdEndorser.getEndpoint();
                        }

                        @Override
                        public Channel getChannel() {
                            return Channel.this;
                        }

                        @Override
                        public HFClient getClient() {
                            return Channel.this.client;
                        }

                        @Override
                        public byte[][] getTLSCerts() {

                            return sdEndorser.getTLSCerts()
                                    .toArray(new byte[sdEndorser.getTLSCerts().size()][]);
                        }

                        @Override
                        public byte[][] getTLSIntermediateCerts() {
                            return sdEndorser.getTLSIntermediateCerts()
                                    .toArray(new byte[sdEndorser.getTLSIntermediateCerts().size()][]);
                        }

                        @Override
                        public Map<String, Peer> getEndpointMap() {
                            return Collections.unmodifiableMap(Channel.this.peerEndpointMap);
                        }
                    });
                }
                endorsers.put(sdEndorser, epeer);
                peer2sdEndorser.put(new PeerExactMatch(epeer), sdEndorser); // reverse
            }

            final Collection<ProposalResponse> proposalResponses = sendProposalToPeers(endorsers.values(),
                    invokeProposal, transactionContext);
            HashSet<SDEndorser> loopGood = new HashSet<>();
            HashSet<SDEndorser> loopBad = new HashSet<>();

            for (ProposalResponse proposalResponse : proposalResponses) {
                final SDEndorser sdEndorser = peer2sdEndorser
                        .get(new PeerExactMatch(proposalResponse.getPeer()));
                allTried.put(sdEndorser, proposalResponse);

                final ChaincodeResponse.Status status = proposalResponse.getStatus();

                if (ChaincodeResponse.Status.SUCCESS.equals(status)) {

                    goodResponses.put(sdEndorser, proposalResponse);
                    logger.trace(format("Channel %s, chaincode %s attempts %d good endorsements: %s", name,
                            chaincodeName, attempts, sdEndorser));
                    loopGood.add(sdEndorser);

                } else {
                    logger.debug(format("Channel %s, chaincode %s attempts %d bad endorsements: %s", name,
                            chaincodeName, attempts, sdEndorser));
                    loopBad.add(sdEndorser);
                }
            }

            //Always check on original
            Collection<SDEndorser> required = sdChaindcode.meetsEndorsmentPolicy(goodResponses.keySet());
            if (required != null) {
                ArrayList<ProposalResponse> ret = new ArrayList<>(required.size());
                required.forEach(s -> ret.add(goodResponses.get(s)));

                if (IS_DEBUG_LEVEL) {

                    StringBuilder sb = new StringBuilder(1000);
                    String sep = "";
                    for (ProposalResponse proposalResponse : ret) {
                        sb.append(sep).append(proposalResponse.getPeer());
                        sep = ", ";
                    }
                    logger.debug(format("Channel %s, chaincode %s got all needed endorsements: %s", name,
                            chaincodeName, sb.toString()));
                }
                return ret; // the happy path :)!

            } else { //still don't have the needed endorsements.

                sdChaindcodeEndorsementCopy.endorsedList(loopGood); // mark the good ones in the working copy.

                if (sdChaindcodeEndorsementCopy.ignoreListSDEndorser(loopBad) < 1) { // apply ignore list
                    done = true; // no more layouts
                }
            }

        } while (!done && ++attempts <= 5);
        logger.debug(format("Endorsements not achieved chaincode: %s, done: %b, attempts: %d", chaincodeName,
                done, attempts));
        if (inspectResults) {
            return allTried.values();
        } else {
            throw new ServiceDiscoveryException(
                    format("Could not meet endorsement policy for chaincode %s", chaincodeName));
        }
    } catch (ProposalException e) {
        throw e;

    } catch (Exception e) {
        ProposalException exp = new ProposalException(e);
        logger.error(exp.getMessage(), exp);
        throw exp;
    }
}

From source file:org.opennms.web.svclayer.support.DefaultManualProvisioningService.java

/**
 * <p>getAssetFieldNames</p>
 *
 * @return a {@link java.util.Collection} object.
 *///from  ww  w.j  ava 2  s.  c om
@Override
public Collection<String> getAssetFieldNames() {
    m_readLock.lock();

    try {
        final Collection<String> assets = PropertyUtils.getProperties(new OnmsAssetRecord());
        assets.removeIf(a -> ASSETS_BLACKLIST.contains(a));
        return assets;
    } finally {
        m_readLock.unlock();
    }
}