Java Code Examples for com.google.common.primitives.Booleans#countTrue()

The following examples show how to use com.google.common.primitives.Booleans#countTrue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Message.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
private Message(Builder builder) {
  this.data = builder.data.isEmpty() ? null : ImmutableMap.copyOf(builder.data);
  this.notification = builder.notification;
  this.androidConfig = builder.androidConfig;
  this.webpushConfig = builder.webpushConfig;
  this.apnsConfig = builder.apnsConfig;
  int count = Booleans.countTrue(
      !Strings.isNullOrEmpty(builder.token),
      !Strings.isNullOrEmpty(builder.topic),
      !Strings.isNullOrEmpty(builder.condition)
  );
  checkArgument(count == 1, "Exactly one of token, topic or condition must be specified");
  this.token = builder.token;
  this.topic = stripPrefix(builder.topic);
  this.condition = builder.condition;
  this.fcmOptions = builder.fcmOptions;
}
 
Example 2
Source File: MailQueueRoutes.java    From james-project with Apache License 2.0 6 votes vote down vote up
private Task deleteMailsTask(MailQueueName queueName, Optional<MailAddress> maybeSender, Optional<String> maybeName, Optional<MailAddress> maybeRecipient) {
    int paramCount = Booleans.countTrue(maybeSender.isPresent(), maybeName.isPresent(), maybeRecipient.isPresent());
    switch (paramCount) {
        case 0:
            return new ClearMailQueueTask(queueName, this::getQueue);
        case 1:
            return new DeleteMailsFromMailQueueTask(queueName, this::getQueue, maybeSender, maybeName, maybeRecipient);
        default:
            throw ErrorResponder.builder()
                .statusCode(HttpStatus.BAD_REQUEST_400)
                .type(ErrorType.INVALID_ARGUMENT)
                .message("You should provide only one of the query parameters 'sender', 'name', 'recipient' " +
                        "for deleting mails by condition or no parameter for deleting all mails in the mail queue.")
                .haltError();
    }
}
 
Example 3
Source File: Message.java    From MixPush with Apache License 2.0 5 votes vote down vote up
/**
 * check message's parameters
 */
public void check() {

    int count = Booleans.countTrue(
            !CollectionUtils.isEmpty(this.token),
            !Strings.isNullOrEmpty(this.topic),
            !Strings.isNullOrEmpty(this.condition)
    );

    ValidatorUtils.checkArgument(count == 1, "Exactly one of token, topic or condition must be specified");

    boolean isEmptyData = StringUtils.isEmpty(data);

    if (this.notification != null) {
        this.notification.check();
    }

    if (null != this.androidConfig) {
        this.androidConfig.check(this.notification);
    }

    if (this.apns != null) {
        this.apns.check();
    }

    if (this.webpush != null) {
        this.webpush.check();
    }
}
 
Example 4
Source File: RdapNameserverSearchAction.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the parameters and calls the appropriate search function.
 *
 * <p>The RDAP spec allows nameserver search by either name or IP address.
 */
@Override
public NameserverSearchResponse getSearchResponse(boolean isHeadRequest) {
  // RDAP syntax example: /rdap/nameservers?name=ns*.example.com.
  if (Booleans.countTrue(nameParam.isPresent(), ipParam.isPresent()) != 1) {
    throw new BadRequestException("You must specify either name=XXXX or ip=YYYY");
  }
  NameserverSearchResponse results;
  if (nameParam.isPresent()) {
    // RDAP Technical Implementation Guilde 2.2.3 - we MAY support nameserver search queries based
    // on a "nameserver search pattern" as defined in RFC7482
    //
    // syntax: /rdap/nameservers?name=exam*.com
    metricInformationBuilder.setSearchType(SearchType.BY_NAMESERVER_NAME);
    results =
        searchByName(
            recordWildcardType(
                RdapSearchPattern.createFromLdhOrUnicodeDomainName(nameParam.get())));
  } else {
    // RDAP Technical Implementation Guide 2.2.3 - we MUST support nameserver search queries based
    // on IP address as defined in RFC7482 3.2.2. Doesn't require pattern matching
    //
    // syntax: /rdap/nameservers?ip=1.2.3.4
    metricInformationBuilder.setSearchType(SearchType.BY_NAMESERVER_ADDRESS);
    InetAddress inetAddress;
    try {
      inetAddress = InetAddresses.forString(ipParam.get());
    } catch (IllegalArgumentException e) {
      throw new BadRequestException("Invalid value of ip parameter");
    }
    results = searchByIp(inetAddress);
  }
  if (results.nameserverSearchResults().isEmpty()) {
    throw new NotFoundException("No nameservers found");
  }
  return results;
}
 
Example 5
Source File: MailboxFactory.java    From james-project with Apache License 2.0 5 votes vote down vote up
private MailboxId computeMailboxId() {
    int idCount = Booleans.countTrue(id.isPresent(), mailboxMetaData.isPresent());
    Preconditions.checkState(idCount == 1, "You need exactly one 'id' 'mailboxMetaData'");
    return id.or(
        () -> mailboxMetaData.map(MailboxMetaData::getId))
        .get();
}
 
Example 6
Source File: RdapDomainSearchAction.java    From nomulus with Apache License 2.0 4 votes vote down vote up
/**
 * Parses the parameters and calls the appropriate search function.
 *
 * <p>The RDAP spec allows for domain search by domain name, nameserver name or nameserver IP.
 */
@Override
public DomainSearchResponse getSearchResponse(boolean isHeadRequest) {
  // RDAP syntax example: /rdap/domains?name=exam*.com.
  if (Booleans.countTrue(nameParam.isPresent(), nsLdhNameParam.isPresent(), nsIpParam.isPresent())
      != 1) {
    throw new BadRequestException(
        "You must specify either name=XXXX, nsLdhName=YYYY or nsIp=ZZZZ");
  }
  DomainSearchResponse results;
  if (nameParam.isPresent()) {
    metricInformationBuilder.setSearchType(SearchType.BY_DOMAIN_NAME);
    // syntax: /rdap/domains?name=exam*.com
    results =
        searchByDomainName(
            recordWildcardType(
                RdapSearchPattern.createFromLdhOrUnicodeDomainName(nameParam.get())));
  } else if (nsLdhNameParam.isPresent()) {
    metricInformationBuilder.setSearchType(SearchType.BY_NAMESERVER_NAME);
    // syntax: /rdap/domains?nsLdhName=ns1.exam*.com
    // RFC 7482 appears to say that Unicode domains must be specified using punycode when
    // passed to nsLdhName, so IDN.toASCII is not called here.
    results =
        searchByNameserverLdhName(
            recordWildcardType(RdapSearchPattern.createFromLdhDomainName(nsLdhNameParam.get())));
  } else {
    metricInformationBuilder.setSearchType(SearchType.BY_NAMESERVER_ADDRESS);
    metricInformationBuilder.setWildcardType(WildcardType.NO_WILDCARD);
    metricInformationBuilder.setPrefixLength(nsIpParam.get().length());
    // syntax: /rdap/domains?nsIp=1.2.3.4
    InetAddress inetAddress;
    try {
      inetAddress = InetAddresses.forString(nsIpParam.get());
    } catch (IllegalArgumentException e) {
      throw new BadRequestException("Invalid value of nsIp parameter");
    }
    results = searchByNameserverIp(inetAddress);
  }
  if (results.domainSearchResults().isEmpty()) {
    throw new NotFoundException("No domains found");
  }
  return results;
}
 
Example 7
Source File: RdapEntitySearchAction.java    From nomulus with Apache License 2.0 4 votes vote down vote up
/** Parses the parameters and calls the appropriate search function. */
@Override
public EntitySearchResponse getSearchResponse(boolean isHeadRequest) {

  // RDAP syntax example: /rdap/entities?fn=Bobby%20Joe*.
  if (Booleans.countTrue(fnParam.isPresent(), handleParam.isPresent()) != 1) {
    throw new BadRequestException("You must specify either fn=XXXX or handle=YYYY");
  }

  // Check the subtype.
  Subtype subtype;
  if (!subtypeParam.isPresent() || subtypeParam.get().equalsIgnoreCase("all")) {
    subtype = Subtype.ALL;
  } else if (subtypeParam.get().equalsIgnoreCase("contacts")) {
    subtype = Subtype.CONTACTS;
  } else if (subtypeParam.get().equalsIgnoreCase("registrars")) {
    subtype = Subtype.REGISTRARS;
  } else {
    throw new BadRequestException("Subtype parameter must specify contacts, registrars or all");
  }

  CursorType cursorType;
  Optional<String> cursorQueryString;
  if (!cursorString.isPresent()) {
    cursorType = CursorType.NONE;
    cursorQueryString = Optional.empty();
  } else {
    if (cursorString.get().startsWith(CONTACT_CURSOR_PREFIX)) {
      cursorType = CursorType.CONTACT;
      cursorQueryString =
          Optional.of(cursorString.get().substring(CONTACT_CURSOR_PREFIX.length()));
    } else if (cursorString.get().startsWith(REGISTRAR_CURSOR_PREFIX)) {
      cursorType = CursorType.REGISTRAR;
      cursorQueryString =
          Optional.of(cursorString.get().substring(REGISTRAR_CURSOR_PREFIX.length()));
    } else {
      throw new BadRequestException(String.format("invalid cursor: %s", cursorTokenParam));
    }
  }

  // Search by name.
  EntitySearchResponse results;
  if (fnParam.isPresent()) {
    metricInformationBuilder.setSearchType(SearchType.BY_FULL_NAME);
    // syntax: /rdap/entities?fn=Bobby%20Joe*
    // The name is the contact name or registrar name (not registrar contact name).
    results =
        searchByName(
            recordWildcardType(RdapSearchPattern.createFromUnicodeString(fnParam.get())),
            cursorType,
            cursorQueryString,
            subtype);

  // Search by handle.
  } else {
    metricInformationBuilder.setSearchType(SearchType.BY_HANDLE);
    // syntax: /rdap/entities?handle=12345-*
    // The handle is either the contact roid or the registrar clientId.
    results =
        searchByHandle(
            recordWildcardType(RdapSearchPattern.createFromUnicodeString(handleParam.get())),
            cursorType,
            cursorQueryString,
            subtype);
  }

  // Build the result object and return it.
  if (results.entitySearchResults().isEmpty()) {
    throw new NotFoundException("No entities found");
  }
  return results;
}
 
Example 8
Source File: BooleansExample.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void count_total_number_of_booleans () {
	
	boolean [] values = {true, true, false, true, false};
	
	int count = Booleans.countTrue(values);
	
	assertEquals(3, count);
}
 
Example 9
Source File: CountBooleansInList.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void count_booleans_arraylist_guava () {
	
	List<Boolean> values = Lists.newArrayList(true, true, false, true, false);
	
	int count = Booleans.countTrue(Booleans.toArray(values));
	
	assertEquals(3, count);
}