Example usage for org.eclipse.jdt.core.compiler CharOperation camelCaseMatch

List of usage examples for org.eclipse.jdt.core.compiler CharOperation camelCaseMatch

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler CharOperation camelCaseMatch.

Prototype

public static final boolean camelCaseMatch(char[] pattern, char[] name, boolean samePartCount) 

Source Link

Document

Answers true if the pattern matches the given name using CamelCase rules, or false otherwise.

Usage

From source file:com.codenvy.ide.ext.java.server.core.search.SearchPattern.java

License:Open Source License

/**
 * Returns whether the given name matches the given pattern.
 * <p>/*from  w w  w  .j  a v  a2s .  c om*/
 * This method should be re-implemented in subclasses that need to define how
 * a name matches a pattern.
 * </p>
 *
 * @param pattern the given pattern, or <code>null</code> to represent "*"
 * @param name the given name
 * @return whether the given name matches the given pattern
 */
public boolean matchesName(char[] pattern, char[] name) {
    if (pattern == null)
        return true; // null is as if it was "*"
    if (name != null) {
        boolean isCaseSensitive = (this.matchRule & R_CASE_SENSITIVE) != 0;
        int matchMode = this.matchRule & MODE_MASK;
        boolean emptyPattern = pattern.length == 0;
        if (emptyPattern && (this.matchRule & R_PREFIX_MATCH) != 0)
            return true;
        boolean sameLength = pattern.length == name.length;
        boolean canBePrefix = name.length >= pattern.length;
        boolean matchFirstChar = !isCaseSensitive || emptyPattern || (name.length > 0 && pattern[0] == name[0]);
        switch (matchMode) {
        case R_EXACT_MATCH:
            if (sameLength && matchFirstChar) {
                return CharOperation.equals(pattern, name, isCaseSensitive);
            }
            break;

        case R_PREFIX_MATCH:
            if (canBePrefix && matchFirstChar) {
                return CharOperation.prefixEquals(pattern, name, isCaseSensitive);
            }
            break;

        case R_PATTERN_MATCH:
            if (!isCaseSensitive)
                pattern = CharOperation.toLowerCase(pattern);
            return CharOperation.match(pattern, name, isCaseSensitive);

        case SearchPattern.R_CAMELCASE_MATCH:
            if (matchFirstChar && CharOperation.camelCaseMatch(pattern, name, false)) {
                return true;
            }
            // only test case insensitive as CamelCase already verified prefix case sensitive
            if (!isCaseSensitive && matchFirstChar && CharOperation.prefixEquals(pattern, name, false)) {
                return true;
            }
            break;

        case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
            return matchFirstChar && CharOperation.camelCaseMatch(pattern, name, true);

        case R_REGEXP_MATCH:
            // TODO implement regular expression match
            return true;
        }
    }
    return false;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.BasicSearchEngine.java

License:Open Source License

boolean match(char patternTypeSuffix, char[] patternPkg, int matchRulePkg, char[] patternTypeName,
        int matchRuleType, int typeKind, char[] pkg, char[] typeName) {
    switch (patternTypeSuffix) {
    case IIndexConstants.CLASS_SUFFIX:
        if (typeKind != TypeDeclaration.CLASS_DECL)
            return false;
        break;/*  w  ww.  ja  va  2  s.  c  om*/
    case IIndexConstants.CLASS_AND_INTERFACE_SUFFIX:
        if (typeKind != TypeDeclaration.CLASS_DECL && typeKind != TypeDeclaration.INTERFACE_DECL)
            return false;
        break;
    case IIndexConstants.CLASS_AND_ENUM_SUFFIX:
        if (typeKind != TypeDeclaration.CLASS_DECL && typeKind != TypeDeclaration.ENUM_DECL)
            return false;
        break;
    case IIndexConstants.INTERFACE_SUFFIX:
        if (typeKind != TypeDeclaration.INTERFACE_DECL)
            return false;
        break;
    case IIndexConstants.INTERFACE_AND_ANNOTATION_SUFFIX:
        if (typeKind != TypeDeclaration.INTERFACE_DECL && typeKind != TypeDeclaration.ANNOTATION_TYPE_DECL)
            return false;
        break;
    case IIndexConstants.ENUM_SUFFIX:
        if (typeKind != TypeDeclaration.ENUM_DECL)
            return false;
        break;
    case IIndexConstants.ANNOTATION_TYPE_SUFFIX:
        if (typeKind != TypeDeclaration.ANNOTATION_TYPE_DECL)
            return false;
        break;
    case IIndexConstants.TYPE_SUFFIX: // nothing
    }

    boolean isPkgCaseSensitive = (matchRulePkg & SearchPattern.R_CASE_SENSITIVE) != 0;
    if (patternPkg != null && !CharOperation.equals(patternPkg, pkg, isPkgCaseSensitive))
        return false;

    boolean isCaseSensitive = (matchRuleType & SearchPattern.R_CASE_SENSITIVE) != 0;
    if (patternTypeName != null) {
        boolean isCamelCase = (matchRuleType
                & (SearchPattern.R_CAMELCASE_MATCH | SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH)) != 0;
        int matchMode = matchRuleType & JavaSearchPattern.MATCH_MODE_MASK;
        if (!isCaseSensitive && !isCamelCase) {
            patternTypeName = CharOperation.toLowerCase(patternTypeName);
        }
        boolean matchFirstChar = !isCaseSensitive || patternTypeName[0] == typeName[0];
        switch (matchMode) {
        case SearchPattern.R_EXACT_MATCH:
            return matchFirstChar && CharOperation.equals(patternTypeName, typeName, isCaseSensitive);
        case SearchPattern.R_PREFIX_MATCH:
            return matchFirstChar && CharOperation.prefixEquals(patternTypeName, typeName, isCaseSensitive);
        case SearchPattern.R_PATTERN_MATCH:
            return CharOperation.match(patternTypeName, typeName, isCaseSensitive);
        case SearchPattern.R_REGEXP_MATCH:
            // TODO (frederic) implement regular expression match
            break;
        case SearchPattern.R_CAMELCASE_MATCH:
            if (matchFirstChar && CharOperation.camelCaseMatch(patternTypeName, typeName, false)) {
                return true;
            }
            return !isCaseSensitive && matchFirstChar
                    && CharOperation.prefixEquals(patternTypeName, typeName, false);
        case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
            return matchFirstChar && CharOperation.camelCaseMatch(patternTypeName, typeName, true);
        }
    }
    return true;

}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.PackageReferenceLocator.java

License:Open Source License

protected int matchLevelForTokens(char[][] tokens) {
    if (this.pattern.pkgName == null)
        return ACCURATE_MATCH;

    switch (this.matchMode) {
    case SearchPattern.R_EXACT_MATCH:
    case SearchPattern.R_PREFIX_MATCH:
        if (CharOperation.prefixEquals(this.pattern.pkgName, CharOperation.concatWith(tokens, '.'),
                this.isCaseSensitive)) {
            return POSSIBLE_MATCH;
        }/*from   w w  w  . ja v  a2 s .co m*/
        break;

    case SearchPattern.R_PATTERN_MATCH:
        char[] patternName = this.pattern.pkgName[this.pattern.pkgName.length - 1] == '*' ? this.pattern.pkgName
                : CharOperation.concat(this.pattern.pkgName, ".*".toCharArray()); //$NON-NLS-1$
        if (CharOperation.match(patternName, CharOperation.concatWith(tokens, '.'), this.isCaseSensitive)) {
            return POSSIBLE_MATCH;
        }
        break;

    case SearchPattern.R_REGEXP_MATCH:
        // TODO (frederic) implement regular expression match
        break;

    case SearchPattern.R_CAMELCASE_MATCH:
        char[] packageName = CharOperation.concatWith(tokens, '.');
        if (CharOperation.camelCaseMatch(this.pattern.pkgName, packageName, false)) {
            return POSSIBLE_MATCH;
        }
        // only test case insensitive as CamelCase already verified prefix case sensitive
        if (!this.isCaseSensitive && CharOperation.prefixEquals(this.pattern.pkgName, packageName, false)) {
            return POSSIBLE_MATCH;
        }
        break;

    case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
        if (CharOperation.camelCaseMatch(this.pattern.pkgName, CharOperation.concatWith(tokens, '.'), true)) {
            return POSSIBLE_MATCH;
        }
        break;
    }
    return IMPOSSIBLE_MATCH;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.PatternLocator.java

License:Open Source License

/**
 * Return how the given name matches the given pattern.
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=79866"
 *
 * @param pattern/* ww w .  ja va 2  s . com*/
 * @param name
 * @return Possible values are:
 * <ul>
 *    <li> {@link #ACCURATE_MATCH}</li>
 *    <li> {@link #IMPOSSIBLE_MATCH}</li>
 *    <li> {@link #POSSIBLE_MATCH} which may be flavored with following values:
 *       <ul>
 *       <li>{@link #EXACT_FLAVOR}: Given name is equals to pattern</li>
 *       <li>{@link #PREFIX_FLAVOR}: Given name prefix equals to pattern</li>
 *       <li>{@link #CAMELCASE_FLAVOR}: Given name matches pattern as Camel Case</li>
 *       <li>{@link #PATTERN_FLAVOR}: Given name matches pattern as Pattern (i.e. using '*' and '?' characters)</li>
 *       </ul>
 *    </li>
 * </ul>
 */
protected int matchNameValue(char[] pattern, char[] name) {
    if (pattern == null)
        return ACCURATE_MATCH; // null is as if it was "*"
    if (name == null)
        return IMPOSSIBLE_MATCH; // cannot match null name
    if (name.length == 0) { // empty name
        if (pattern.length == 0) { // can only matches empty pattern
            return ACCURATE_MATCH;
        }
        return IMPOSSIBLE_MATCH;
    } else if (pattern.length == 0) {
        return IMPOSSIBLE_MATCH; // need to have both name and pattern length==0 to be accurate
    }
    boolean matchFirstChar = !this.isCaseSensitive || pattern[0] == name[0];
    boolean sameLength = pattern.length == name.length;
    boolean canBePrefix = name.length >= pattern.length;
    switch (this.matchMode) {
    case SearchPattern.R_EXACT_MATCH:
        if (sameLength && matchFirstChar && CharOperation.equals(pattern, name, this.isCaseSensitive)) {
            return POSSIBLE_MATCH | EXACT_FLAVOR;
        }
        break;

    case SearchPattern.R_PREFIX_MATCH:
        if (canBePrefix && matchFirstChar && CharOperation.prefixEquals(pattern, name, this.isCaseSensitive)) {
            return POSSIBLE_MATCH;
        }
        break;

    case SearchPattern.R_PATTERN_MATCH:
        // TODO_PERFS (frederic) Not sure this lowercase is necessary
        if (!this.isCaseSensitive) {
            pattern = CharOperation.toLowerCase(pattern);
        }
        if (CharOperation.match(pattern, name, this.isCaseSensitive)) {
            return POSSIBLE_MATCH;
        }
        break;

    case SearchPattern.R_REGEXP_MATCH:
        // TODO (frederic) implement regular expression match
        break;

    case SearchPattern.R_CAMELCASE_MATCH:
        if (CharOperation.camelCaseMatch(pattern, name, false)) {
            return POSSIBLE_MATCH;
        }
        // only test case insensitive as CamelCase same part count already verified prefix case sensitive
        if (!this.isCaseSensitive && CharOperation.prefixEquals(pattern, name, false)) {
            return POSSIBLE_MATCH;
        }
        break;

    case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
        if (CharOperation.camelCaseMatch(pattern, name, true)) {
            return POSSIBLE_MATCH;
        }
        break;
    }
    return IMPOSSIBLE_MATCH;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.PatternLocator.java

License:Open Source License

/**
 * Returns whether the given type binding matches the given simple name pattern
 * and qualification pattern./*from  w  w w  . j  a v  a 2  s . com*/
 * Note that from since 3.1, this method resolve to accurate member or local types
 * even if they are not fully qualified (i.e. X.Member instead of p.X.Member).
 * Returns ACCURATE_MATCH if it does.
 * Returns INACCURATE_MATCH if resolve failed.
 * Returns IMPOSSIBLE_MATCH if it doesn't.
 */
protected int resolveLevelForType(char[] simpleNamePattern, char[] qualificationPattern, TypeBinding binding) {
    //   return resolveLevelForType(qualifiedPattern(simpleNamePattern, qualificationPattern), type);
    char[] qualifiedPattern = getQualifiedPattern(simpleNamePattern, qualificationPattern);
    int level = resolveLevelForType(qualifiedPattern, binding);
    if (level == ACCURATE_MATCH || binding == null || !binding.isValidBinding())
        return level;
    TypeBinding type = binding instanceof ArrayBinding ? ((ArrayBinding) binding).leafComponentType : binding;
    char[] sourceName = null;
    if (type.isMemberType() || type.isLocalType()) {
        if (qualificationPattern != null) {
            sourceName = getQualifiedSourceName(binding);
        } else {
            sourceName = binding.sourceName();
        }
    } else if (qualificationPattern == null) {
        sourceName = getQualifiedSourceName(binding);
    }
    if (sourceName == null)
        return IMPOSSIBLE_MATCH;
    switch (this.matchMode) {
    case SearchPattern.R_PREFIX_MATCH:
        if (CharOperation.prefixEquals(qualifiedPattern, sourceName, this.isCaseSensitive)) {
            return ACCURATE_MATCH;
        }
        break;
    case SearchPattern.R_CAMELCASE_MATCH:
        if ((qualifiedPattern.length > 0 && sourceName.length > 0 && qualifiedPattern[0] == sourceName[0])) {
            if (CharOperation.camelCaseMatch(qualifiedPattern, sourceName, false)) {
                return ACCURATE_MATCH;
            }
            if (!this.isCaseSensitive && CharOperation.prefixEquals(qualifiedPattern, sourceName, false)) {
                return ACCURATE_MATCH;
            }
        }
        break;
    case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
        if ((qualifiedPattern.length > 0 && sourceName.length > 0 && qualifiedPattern[0] == sourceName[0])) {
            if (CharOperation.camelCaseMatch(qualifiedPattern, sourceName, true)) {
                return ACCURATE_MATCH;
            }
        }
        break;
    default:
        if (CharOperation.match(qualifiedPattern, sourceName, this.isCaseSensitive)) {
            return ACCURATE_MATCH;
        }
    }
    return IMPOSSIBLE_MATCH;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.TypeReferenceLocator.java

License:Open Source License

protected int matchLevel(ImportReference importRef) {
    if (this.pattern.qualification == null) {
        if (this.pattern.simpleName == null)
            return ACCURATE_MATCH;
        char[][] tokens = importRef.tokens;
        boolean onDemand = (importRef.bits & ASTNode.OnDemand) != 0;
        final boolean isStatic = importRef.isStatic();
        if (!isStatic && onDemand) {
            return IMPOSSIBLE_MATCH;
        }//  www .  ja  va 2  s .c  o m
        int length = tokens.length;
        if (matchesName(this.pattern.simpleName, tokens[length - 1])) {
            return ACCURATE_MATCH;
        }
        if (isStatic && !onDemand && length > 1) {
            if (matchesName(this.pattern.simpleName, tokens[length - 2])) {
                return ACCURATE_MATCH;
            }
        }
    } else {
        char[][] tokens = importRef.tokens;
        char[] qualifiedPattern = this.pattern.simpleName == null ? this.pattern.qualification
                : CharOperation.concat(this.pattern.qualification, this.pattern.simpleName, '.');
        char[] qualifiedTypeName = CharOperation.concatWith(tokens, '.');
        if (qualifiedPattern == null)
            return ACCURATE_MATCH; // null is as if it was "*"
        if (qualifiedTypeName == null)
            return IMPOSSIBLE_MATCH; // cannot match null name
        if (qualifiedTypeName.length == 0) { // empty name
            if (qualifiedPattern.length == 0) { // can only matches empty pattern
                return ACCURATE_MATCH;
            }
            return IMPOSSIBLE_MATCH;
        }
        boolean matchFirstChar = !this.isCaseSensitive || (qualifiedPattern[0] == qualifiedTypeName[0]);
        switch (this.matchMode) {
        case SearchPattern.R_EXACT_MATCH:
        case SearchPattern.R_PREFIX_MATCH:
            if (CharOperation.prefixEquals(qualifiedPattern, qualifiedTypeName, this.isCaseSensitive)) {
                return POSSIBLE_MATCH;
            }
            break;

        case SearchPattern.R_PATTERN_MATCH:
            if (CharOperation.match(qualifiedPattern, qualifiedTypeName, this.isCaseSensitive)) {
                return POSSIBLE_MATCH;
            }
            break;

        case SearchPattern.R_REGEXP_MATCH:
            // TODO (frederic) implement regular expression match
            break;
        case SearchPattern.R_CAMELCASE_MATCH:
            if (matchFirstChar && CharOperation.camelCaseMatch(qualifiedPattern, qualifiedTypeName, false)) {
                return POSSIBLE_MATCH;
            }
            // only test case insensitive as CamelCase already verified prefix case sensitive
            if (!this.isCaseSensitive
                    && CharOperation.prefixEquals(qualifiedPattern, qualifiedTypeName, false)) {
                return POSSIBLE_MATCH;
            }
            break;
        case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
            if (matchFirstChar && CharOperation.camelCaseMatch(qualifiedPattern, qualifiedTypeName, true)) {
                return POSSIBLE_MATCH;
            }
            break;
        }
    }
    return IMPOSSIBLE_MATCH;
}