Example usage for com.google.common.base CharMatcher javaDigit

List of usage examples for com.google.common.base CharMatcher javaDigit

Introduction

In this page you can find the example usage for com.google.common.base CharMatcher javaDigit.

Prototype

public static CharMatcher javaDigit() 

Source Link

Document

Determines whether a character is a digit according to Character#isDigit(char) Java's definition .

Usage

From source file:google.registry.tools.CreateOrUpdateTldCommand.java

@Override
protected final void init() throws Exception {
    assertAllowedEnvironment();//from   w  w  w.  jav  a  2s  .c o  m
    initTldCommand();
    String duplicates = Joiner.on(", ").join(findDuplicates(mainParameters));
    checkArgument(duplicates.isEmpty(), "Duplicate arguments found: \"%s\"", duplicates);
    Set<String> tlds = ImmutableSet.copyOf(mainParameters);
    checkArgument(roidSuffix == null || tlds.size() == 1,
            "Can't update roid suffixes on multiple TLDs simultaneously");
    for (String tld : tlds) {
        checkArgument(tld.equals(canonicalizeDomainName(tld)));
        checkArgument(!CharMatcher.javaDigit().matches(tld.charAt(0)), "TLDs cannot begin with a number.");
        Registry oldRegistry = getOldRegistry(tld);
        if (roidSuffix != null) {
            checkArgument(
                    !isRoidSuffixUsed(roidSuffix)
                            || (oldRegistry != null && roidSuffix.equals(oldRegistry.getRoidSuffix())),
                    "The roid suffix %s is already in use", roidSuffix);
        }
        // TODO(b/26901539): Add a flag to set the pricing engine once we have more than one option.
        Registry.Builder builder = oldRegistry == null ? new Registry.Builder().setTldStr(tld)
                .setPremiumPricingEngine(StaticPremiumListPricingEngine.NAME) : oldRegistry.asBuilder();

        if (escrow != null) {
            builder.setEscrowEnabled(escrow);
        }

        if (dns != null) {
            builder.setDnsPaused(!dns);
        }

        Optional<Map.Entry<DateTime, TldState>> tldStateTransitionToAdd = getTldStateTransitionToAdd();
        if (!tldStateTransitions.isEmpty()) {
            builder.setTldStateTransitions(tldStateTransitions);
        } else if (tldStateTransitionToAdd.isPresent()) {
            ImmutableSortedMap.Builder<DateTime, TldState> newTldStateTransitions = ImmutableSortedMap
                    .naturalOrder();
            if (oldRegistry != null) {
                checkArgument(
                        oldRegistry.getTldStateTransitions().lastKey()
                                .isBefore(tldStateTransitionToAdd.get().getKey()),
                        "Cannot add %s at %s when there is a later transition already scheduled",
                        tldStateTransitionToAdd.get().getValue(), tldStateTransitionToAdd.get().getKey());
                newTldStateTransitions.putAll(oldRegistry.getTldStateTransitions());
            }
            builder.setTldStateTransitions(
                    newTldStateTransitions.put(getTldStateTransitionToAdd().get()).build());
        }

        if (!renewBillingCostTransitions.isEmpty()) {
            // TODO(b/20764952): need invoicing support for multiple renew billing costs.
            if (renewBillingCostTransitions.size() > 1) {
                System.err.println("----------------------\n"
                        + "WARNING: Do not set multiple renew cost transitions until b/20764952 is fixed.\n"
                        + "----------------------\n");
            }
            builder.setRenewBillingCostTransitions(renewBillingCostTransitions);
        }

        if (!eapFeeSchedule.isEmpty()) {
            builder.setEapFeeSchedule(eapFeeSchedule);
        }

        if (addGracePeriod != null) {
            builder.setAddGracePeriodLength(addGracePeriod);
        }

        if (redemptionGracePeriod != null) {
            builder.setRedemptionGracePeriodLength(redemptionGracePeriod);
        }

        if (pendingDeleteLength != null) {
            builder.setPendingDeleteLength(pendingDeleteLength);
        }

        if (automaticTransferLength != null) {
            builder.setAutomaticTransferLength(automaticTransferLength);
        }

        if (driveFolderId != null) {
            builder.setDriveFolderId(driveFolderId.orNull());
        }

        if (createBillingCost != null) {
            builder.setCreateBillingCost(createBillingCost);
        }

        if (restoreBillingCost != null) {
            builder.setRestoreBillingCost(restoreBillingCost);
        }

        if (roidSuffix != null) {
            builder.setRoidSuffix(roidSuffix);
        }

        if (serverStatusChangeCost != null) {
            builder.setServerStatusChangeBillingCost(serverStatusChangeCost);
        }

        if (tldType != null) {
            builder.setTldType(tldType);
        }

        if (premiumPriceAckRequired != null) {
            builder.setPremiumPriceAckRequired(premiumPriceAckRequired);
        }

        if (lordnUsername != null) {
            builder.setLordnUsername(lordnUsername.orNull());
        }

        if (claimsPeriodEnd != null) {
            builder.setClaimsPeriodEnd(claimsPeriodEnd);
        }

        if (premiumListName != null) {
            if (premiumListName.isPresent()) {
                Optional<PremiumList> premiumList = PremiumList.get(premiumListName.get());
                checkArgument(premiumList.isPresent(),
                        String.format("The premium list '%s' doesn't exist", premiumListName.get()));
                builder.setPremiumList(premiumList.get());
            } else {
                builder.setPremiumList(null);
            }
        }

        if (dnsWriter != null) {
            if (dnsWriter.isPresent()) {
                checkArgument(dnsWriterNames.contains(dnsWriter.get()), "The DNS writer '%s' doesn't exist",
                        dnsWriter.get());
                builder.setDnsWriter(dnsWriter.get());
            }
        }

        if (lrpPeriod != null) {
            builder.setLrpPeriod(lrpPeriod.orNull());
        }

        ImmutableSet<String> newReservedListNames = getReservedLists(oldRegistry);
        checkReservedListValidityForTld(tld, newReservedListNames);
        builder.setReservedListsByName(newReservedListNames);

        builder.setAllowedRegistrantContactIds(getAllowedRegistrants(oldRegistry));

        builder.setAllowedFullyQualifiedHostNames(getAllowedNameservers(oldRegistry));

        // Update the Registry object.
        setCommandSpecificProperties(builder);
        stageEntityChange(oldRegistry, builder.build());
    }
}

From source file:com.google.devtools.build.lib.rules.objc.ProtoAttributes.java

/**
 * Processes the case of the proto file name in the same fashion as the objective_c generator's
 * UnderscoresToCamelCase function. This converts snake case to camel case by splitting words
 * by non alphabetic characters. This also treats the URL, HTTP and HTTPS as special words that
 * need to be completely uppercase./*from ww w  .ja va 2s . c om*/
 *
 * Examples:
 *   - j2objc_descriptor -> J2ObjcDescriptor (notice that O is uppercase after the 2)
 *   - my_http_url_array -> MyHTTPURLArray
 *   - proto-descriptor  -> ProtoDescriptor
 *
 * Original code reference:
 * <p>https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc
 */
String getGeneratedProtoFilename(String protoFilename, boolean upcaseReservedWords) {
    boolean lastCharWasDigit = false;
    boolean lastCharWasUpper = false;
    boolean lastCharWasLower = false;

    StringBuilder currentSegment = new StringBuilder();

    ArrayList<String> segments = new ArrayList<>();

    for (int i = 0; i < protoFilename.length(); i++) {
        char currentChar = protoFilename.charAt(i);
        if (CharMatcher.javaDigit().matches(currentChar)) {
            if (!lastCharWasDigit) {
                segments.add(currentSegment.toString());
                currentSegment = new StringBuilder();
            }
            currentSegment.append(currentChar);
            lastCharWasDigit = true;
            lastCharWasUpper = false;
            lastCharWasLower = false;
        } else if (CharMatcher.javaLowerCase().matches(currentChar)) {
            if (!lastCharWasLower && !lastCharWasUpper) {
                segments.add(currentSegment.toString());
                currentSegment = new StringBuilder();
            }
            currentSegment.append(currentChar);
            lastCharWasDigit = false;
            lastCharWasUpper = false;
            lastCharWasLower = true;
        } else if (CharMatcher.javaUpperCase().matches(currentChar)) {
            if (!lastCharWasUpper) {
                segments.add(currentSegment.toString());
                currentSegment = new StringBuilder();
            }
            currentSegment.append(Character.toLowerCase(currentChar));
            lastCharWasDigit = false;
            lastCharWasUpper = true;
            lastCharWasLower = false;
        } else {
            lastCharWasDigit = false;
            lastCharWasUpper = false;
            lastCharWasLower = false;
        }
    }

    segments.add(currentSegment.toString());

    StringBuilder casedSegments = new StringBuilder();
    for (String segment : segments) {
        if (upcaseReservedWords && UPPERCASE_SEGMENTS.contains(segment)) {
            casedSegments.append(segment.toUpperCase());
        } else {
            casedSegments.append(LOWER_UNDERSCORE.to(UPPER_CAMEL, segment));
        }
    }
    return casedSegments.toString();
}

From source file:com.google.devtools.build.lib.rules.objc.ProtoSupport.java

/**
 * Processes the case of the proto file name in the same fashion as the objective_c generator's
 * UnderscoresToCamelCase function./*from w  w w.j a  v a2  s  . com*/
 *
 * https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc
 */
private String generateProtobufFilename(String protoFilename) {
    boolean lastCharWasDigit = false;
    boolean lastCharWasUpper = false;
    boolean lastCharWasLower = false;

    StringBuilder currentSegment = new StringBuilder();

    ArrayList<String> segments = new ArrayList<>();

    for (int i = 0; i < protoFilename.length(); i++) {
        char currentChar = protoFilename.charAt(i);
        if (CharMatcher.javaDigit().matches(currentChar)) {
            if (!lastCharWasDigit) {
                segments.add(currentSegment.toString());
                currentSegment = new StringBuilder();
            }
            currentSegment.append(currentChar);
            lastCharWasDigit = true;
            lastCharWasUpper = false;
            lastCharWasLower = false;
        } else if (CharMatcher.javaLowerCase().matches(currentChar)) {
            if (!lastCharWasLower && !lastCharWasUpper) {
                segments.add(currentSegment.toString());
                currentSegment = new StringBuilder();
            }
            currentSegment.append(currentChar);
            lastCharWasDigit = false;
            lastCharWasUpper = false;
            lastCharWasLower = true;
        } else if (CharMatcher.javaUpperCase().matches(currentChar)) {
            if (!lastCharWasUpper) {
                segments.add(currentSegment.toString());
                currentSegment = new StringBuilder();
            }
            currentSegment.append(Character.toLowerCase(currentChar));
            lastCharWasDigit = false;
            lastCharWasUpper = true;
            lastCharWasLower = false;
        } else {
            lastCharWasDigit = false;
            lastCharWasUpper = false;
            lastCharWasLower = false;
        }
    }

    segments.add(currentSegment.toString());

    StringBuilder casedSegments = new StringBuilder();
    for (String segment : segments) {
        if (UPPERCASE_SEGMENTS.contains(segment)) {
            casedSegments.append(segment.toUpperCase());
        } else {
            casedSegments.append(LOWER_UNDERSCORE.to(UPPER_CAMEL, segment));
        }
    }
    return casedSegments.toString();
}