Exemple #1
0
        /**
         * Extracts the country calling code from the beginning of nationalNumber to
         * prefixBeforeNationalNumber when they are available, and places the remaining input into
         * nationalNumber.
         *
         * @return  true when a valid country calling code can be found.
         */
        private boolean attemptToExtractCountryCallingCode()
        {
            if (nationalNumber.length() == 0)
            {
                return(false);
            }
            StringBuilder numberWithoutCountryCallingCode = new StringBuilder();
            int           countryCode = phoneUtil.extractCountryCode(nationalNumber, numberWithoutCountryCallingCode);

            if (countryCode == 0)
            {
                return(false);
            }
            nationalNumber.setLength(0);
            nationalNumber.append(numberWithoutCountryCallingCode);
            String newRegionCode = phoneUtil.getRegionCodeForCountryCode(countryCode);

            if (PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY.equals(newRegionCode))
            {
                currentMetadata = phoneUtil.getMetadataForNonGeographicalRegion(countryCode);
            }
            else if (!newRegionCode.equals(defaultCountry))
            {
                currentMetadata = getMetadataForRegion(newRegionCode);
            }
            String countryCodeString = Integer.toString(countryCode);

            prefixBeforeNationalNumber.append(countryCodeString).append(SEPARATOR_BEFORE_NATIONAL_NUMBER);
            return(true);
        }
Exemple #2
0
        // Returns true if a new template is created as opposed to reusing the existing template.
        private boolean maybeCreateNewTemplate()
        {
            // When there are multiple available formats, the formatter uses the first format where a
            // formatting template could be created.
            Iterator <NumberFormat> it = possibleFormats.iterator();

            while (it.hasNext())
            {
                NumberFormat numberFormat = it.next();
                String       pattern      = numberFormat.getPattern();
                if (currentFormattingPattern.equals(pattern))
                {
                    return(false);
                }
                if (createFormattingTemplate(numberFormat))
                {
                    currentFormattingPattern          = pattern;
                    shouldAddSpaceAfterNationalPrefix =
                        NATIONAL_PREFIX_SEPARATORS_PATTERN.matcher(
                            numberFormat.getNationalPrefixFormattingRule()).find();
                    // With a new formatting template, the matched position using the old template needs to be
                    // reset.
                    lastMatchPosition = 0;
                    return(true);
                }
                else // Remove the current number format from possibleFormats.
                {
                    it.remove();
                }
            }
            ableToFormat = false;
            return(false);
        }
Exemple #3
0
        /**
         * Attempts to extract a match from {@code candidate} if the whole candidate does not qualify as a
         * match.
         *
         * @param candidate  the candidate text that might contain a phone number
         * @param offset  the current offset of {@code candidate} within {@link #text}
         * @return  the match found, null if none can be found
         */
        private PhoneNumberMatch extractInnerMatch(String candidate, int offset)
        {
            // Try removing either the first or last "group" in the number and see if this gives a result.
            // We consider white space to be a possible indication of the start or end of the phone number.
            Matcher groupMatcher = GROUP_SEPARATOR.matcher(candidate);

            if (groupMatcher.find())
            {
                // Try the first group by itself.
                CharSequence firstGroupOnly = candidate.substring(0, groupMatcher.start());
                firstGroupOnly = trimAfterFirstMatch(PhoneNumberUtil.UNWANTED_END_CHAR_PATTERN,
                                                     firstGroupOnly);
                PhoneNumberMatch match = parseAndVerify(firstGroupOnly.toString(), offset);
                if (match != null)
                {
                    return(match);
                }
                maxTries--;

                int withoutFirstGroupStart = groupMatcher.end();
                // Try the rest of the candidate without the first group.
                CharSequence withoutFirstGroup = candidate.substring(withoutFirstGroupStart);
                withoutFirstGroup = trimAfterFirstMatch(PhoneNumberUtil.UNWANTED_END_CHAR_PATTERN,
                                                        withoutFirstGroup);
                match = parseAndVerify(withoutFirstGroup.toString(), offset + withoutFirstGroupStart);
                if (match != null)
                {
                    return(match);
                }
                maxTries--;

                if (maxTries > 0)
                {
                    int lastGroupStart = withoutFirstGroupStart;
                    while (groupMatcher.find())
                    {
                        // Find the last group.
                        lastGroupStart = groupMatcher.start();
                    }
                    CharSequence withoutLastGroup = candidate.substring(0, lastGroupStart);
                    withoutLastGroup = trimAfterFirstMatch(PhoneNumberUtil.UNWANTED_END_CHAR_PATTERN,
                                                           withoutLastGroup);
                    if (withoutLastGroup.equals(firstGroupOnly))
                    {
                        // If there are only two groups, then the group "without the last group" is the same as
                        // the first group. In these cases, we don't want to re-check the number group, so we exit
                        // already.
                        return(null);
                    }
                    match = parseAndVerify(withoutLastGroup.toString(), offset);
                    if (match != null)
                    {
                        return(match);
                    }
                    maxTries--;
                }
            }
            return(null);
        }
Exemple #4
0
 // Some national prefixes are a substring of others. If extracting the shorter NDD doesn't result
 // in a number we can format, we try to see if we can extract a longer version here.
 private boolean ableToExtractLongerNdd()
 {
     if (nationalPrefixExtracted.length() > 0)
     {
         // Put the extracted NDD back to the national number before attempting to extract a new NDD.
         nationalNumber.insert(0, nationalPrefixExtracted);
         // Remove the previously extracted NDD from prefixBeforeNationalNumber. We cannot simply set
         // it to empty string because people sometimes incorrectly enter national prefix after the
         // country code, e.g. +44 (0)20-1234-5678.
         int indexOfPreviousNdd = prefixBeforeNationalNumber.lastIndexOf(nationalPrefixExtracted);
         prefixBeforeNationalNumber.setLength(indexOfPreviousNdd);
     }
     return(!nationalPrefixExtracted.equals(removeNationalPrefixFromNationalNumber()));
 }
        public boolean equals(Object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (!(obj is PhoneNumberMatch))
            {
                return(false);
            }
            PhoneNumberMatch other = (PhoneNumberMatch)obj;

            return(_rawString.equals(other._rawString) && (_start == other._start) &&
                   _number.equals(other._number));
        }
Exemple #6
0
 public boolean exactlySameAs(PhoneNumber other)
 {
     if (other == null)
     {
         return(false);
     }
     if (this == other)
     {
         return(true);
     }
     return(countryCode_ == other.countryCode_ && nationalNumber_ == other.nationalNumber_ &&
            extension_.equals(other.extension_) && italianLeadingZero_ == other.italianLeadingZero_ &&
            rawInput_.equals(other.rawInput_) && countryCodeSource_ == other.countryCodeSource_ &&
            preferredDomesticCarrierCode_.equals(other.preferredDomesticCarrierCode_) &&
            hasPreferredDomesticCarrierCode() == other.hasPreferredDomesticCarrierCode());
 }
Exemple #7
0
 public boolean exactlySameAs(PhoneNumberDesc other)
 {
     return(nationalNumberPattern_.equals(other.nationalNumberPattern_) &&
            possibleNumberPattern_.equals(other.possibleNumberPattern_) &&
            exampleNumber_.equals(other.exampleNumber_));
 }
		public Iterable<MemberInfo> getMembers(String identifier) {
			if (identifier.equals("this")) {
				return getMembers();
			} else if (identifier.equals("super")) {
				var type = typeInfos[typeInfos.size() - 1];
				return getMembers(type.BaseType, false);
			}
			var members = new HashMap<String, MemberInfo>();
			putTypeMembers(members);
			var type = typeInfos[typeInfos.size() - 1];
			foreach (var t in type.getBaseTypes().reverse()) {
				putFields(t, members);
				putProperties(t, members);
				putNestedTypes(t, members);
			}
			putFields(type, members);
			putProperties(type, members);
			putNestedTypes(type, members);
			putLocals(members);
			var member = members[identifier];
			if (member == null) {
				return Query.empty();
			}
			return getMembers(member.Type, (member.MemberKind == MemberKind.Local) ? false : member.IsStatic);
		}
 public void visitInnerClass(String name, String outerName, String innerName, int access) {
     if (!name.equals(classFileType.FullName)) {
         // Inner classes with outerName == null are method classes: ignore them.
         if (outerName != null && outerName.equals(classFileType.FullName)) {
             var nestedType = (ClassFileType)classFileType.typeSystem.getType(name);
             nestedType.nestedModifiers = access;
             nestedType.declaringType = classFileType;
             classFileType.nestedTypes.add(nestedType);
         }
     }
 }
        public static Iterable<MemberInfo> getMembers(Library typeSystem, TypeInfo type, String name) {
            var result = new ArrayList<MemberInfo>();
            
            foreach (var f in type.Fields) {
                if (f.Name.equals(name)) {
                    result.add(MemberInfo.getInfo(f));
                    break;
                }
            }

            MethodInfo getAccessor = null;
            MethodInfo setAccessor = null;
            foreach (var m in type.Methods) {
                if (getAccessor == null || setAccessor == null) {
                    foreach (var annotation in BytecodeHelper.getAnnotations(typeSystem, m)) {
                        if (BytecodeHelper.isPropertyGet(annotation) && name.equals(BytecodeHelper.getPropertyGetName(m, annotation))) {
                            getAccessor = m;
                            break;
                        }
                        if (BytecodeHelper.isPropertySet(annotation) && name.equals(BytecodeHelper.getPropertySetName(m, annotation))) {
                            setAccessor = m;
                            break;
                        }
                    }
                }
                if (m.Name.equals(name)) {
                    result.add(MemberInfo.getInfo(m));
                }
            }
            if (getAccessor != null || setAccessor != null) {
                result.add(MemberInfo.getInfo(getAccessor, setAccessor, name));
            }
            
            foreach (var t in type.NestedTypes) {
                if (name.equals(t.Name)) {
                    result.add(MemberInfo.getInfo(t));
                    break;
                }
            }
            
            return result;
        }
 private boolean mayFallBackToEnglish(String lang)
 {
     // Don't fall back to English if the requested language is among the following:
     // - Chinese
     // - Japanese
     // - Korean
     return !lang.equals("zh") && !lang.equals("ja") && !lang.equals("ko");
 }
 /**
    * Returns the customary display name in the given language for the given region.
    */
 private String getRegionDisplayName(String regionCode, Locale language)
 {
     return (regionCode == null || regionCode.equals("ZZ") ||
     regionCode.equals(PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY))
     ? "" : new Locale("", regionCode).getDisplayCountry(language);
 }
 /**
    * As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale)} but also considers the
    * region of the user. If the phone number is from the same region as the user, only a lower-level
    * description will be returned, if one exists. Otherwise, the phone number's region will be
    * returned, with optionally some more detailed information.
    *
    * <p>For example, for a user from the region "US" (United States), we would show "Mountain View,
    * CA" for a particular number, omitting the United States from the description. For a user from
    * the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United
    * States" or even just "United States".
    *
    * <p>This method assumes the validity of the number passed in has already been checked.
    *
    * @param number  the phone number for which we want to get a text description
    * @param languageCode  the language code for which the description should be written
    * @param userRegion  the region code for a given user. This region will be omitted from the
    *     description if the phone number comes from this region. It is a two-letter uppercase ISO
    *     country code as defined by ISO 3166-1.
    * @return  a text description for the given language code for the given phone number, or empty
    *     string if the number passed in is invalid
    */
 public String getDescriptionForValidNumber(PhoneNumber number, Locale languageCode,
                                      String userRegion)
 {
     // If the user region matches the number's region, then we just show the lower-level
     // description, if one exists - if no description exists, we will show the region(country) name
     // for the number.
     String regionCode = phoneUtil.getRegionCodeForNumber(number);
     if (userRegion.equals(regionCode)) {
       return getDescriptionForValidNumber(number, languageCode);
     }
     // Otherwise, we just show the region(country) name for now.
     return getRegionDisplayName(regionCode, languageCode);
     // TODO: Concatenate the lower-level and country-name information in an appropriate
     // way for each language.
 }