All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.siyeh.ig.psiutils.StringUtils Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition java-analysis-impl library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ig.psiutils;

import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.Set;

public class StringUtils {

  @NonNls private static final Set keywordTable =
    new HashSet();
  @NonNls private static final String AN = "an";

  static {
    keywordTable.add("abstract");
    keywordTable.add("assert");
    keywordTable.add("boolean");
    keywordTable.add("break");
    keywordTable.add("byte");
    keywordTable.add("case");
    keywordTable.add("catch");
    keywordTable.add("char");
    keywordTable.add("class");
    keywordTable.add("const");
    keywordTable.add("continue");
    keywordTable.add("default");
    keywordTable.add("do");
    keywordTable.add("double");
    keywordTable.add("else");
    keywordTable.add("enum");
    keywordTable.add("extends");
    keywordTable.add("false");
    keywordTable.add("final");
    keywordTable.add("finally");
    keywordTable.add("float");
    keywordTable.add("for");
    keywordTable.add("goto");
    keywordTable.add("if");
    keywordTable.add("implements");
    keywordTable.add("import");
    keywordTable.add("instanceof");
    keywordTable.add("int");
    keywordTable.add("interface");
    keywordTable.add("long");
    keywordTable.add("native");
    keywordTable.add("new");
    keywordTable.add("null");
    keywordTable.add("package");
    keywordTable.add("private");
    keywordTable.add("protected");
    keywordTable.add("public");
    keywordTable.add("return");
    keywordTable.add("short");
    keywordTable.add("static");
    keywordTable.add("strictfp");
    keywordTable.add("super");
    keywordTable.add("switch");
    keywordTable.add("synchronized");
    keywordTable.add("this");
    keywordTable.add("throw");
    keywordTable.add("throws");
    keywordTable.add("transient");
    keywordTable.add("true");
    keywordTable.add("try");
    keywordTable.add("void");
    keywordTable.add("volatile");
    keywordTable.add("while");
  }

  private StringUtils() {
    super();
  }

  @SuppressWarnings({"HardCodedStringLiteral"})
  @NotNull
  public static String createSingularFromName(@NotNull String name) {
    final int nameLength = name.length();
    final String singularName;
    if (name.endsWith("ies")) {
      singularName = name.substring(0, nameLength - 3) + 'y';
    }
    else if (name.endsWith("sses") || name.endsWith("shes")) {
      singularName = name.substring(0, nameLength - 2);
    }
    else if (name.charAt(nameLength - 1) == 's') {
      singularName = name.substring(0, nameLength - 1);
    }
    else if (name.endsWith("IES")) {
      singularName = name.substring(0, nameLength - 3) + 'Y';
    }
    else if (name.endsWith("SSES") || name.endsWith("SHES")) {
      singularName = name.substring(0, nameLength - 2);
    }
    else if (name.charAt(nameLength - 1) == 'S') {
      singularName = name.substring(0, nameLength - 1);
    }
    else {
      singularName = prependIndefiniteArticle(name);
    }
    if (keywordTable.contains(singularName)) {
      return prependIndefiniteArticle(singularName);
    }
    else {
      return singularName;
    }
  }

  @NotNull
  private static String prependIndefiniteArticle(
    String singularName) {
    switch (singularName.charAt(0)) {
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
        return AN + StringUtil.capitalize(singularName);
      default:
        return 'a' + StringUtil.capitalize(singularName);
    }
  }

  public static String stripAngleBrackets(String string) {
    final int index = string.indexOf('<');
    if (index == -1) {
      return string;
    }
    return string.substring(0, index);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy