Java String Shorten shortenOperationName(String operation)

Here you can find the source of shortenOperationName(String operation)

Description

Shortens full qualified method names.

License

Apache License

Parameter

Parameter Description
operation name to shorten

Return

short method name

Declaration

public static String shortenOperationName(String operation) 

Method Source Code

//package com.java2s;
/**/*from  w w w  . ja  va  2 s.  c  o  m*/
 * Copyright 2014 SAP AG
 *
 * 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.
 */

public class Main {
    /**
     * Shortens full qualified method names. Thus,
     * my.full.package.Class.operation() becomes m.f.p.Class.operation()
     * 
     * @param operation
     *            name to shorten
     * @return short method name
     */
    public static String shortenOperationName(String operation) {
        String mainPart = operation.substring(0, operation.indexOf("("));
        String[] packages = mainPart.split("\\.");
        String result = "";
        for (int i = 0; i < packages.length - 2; i++) {
            result += packages[i].substring(0, 1);
            result += ".";
        }
        result += packages[packages.length - 2] + "."
                + packages[packages.length - 1];
        result += "(";

        String[] parameters = operation.substring(
                operation.indexOf("(") + 1, operation.indexOf(")")).split(
                ",");
        boolean first = true;
        for (String p : parameters) {
            if (!first) {
                result += ",";
            }
            if (p.contains(".")) {
                result += p.substring(p.lastIndexOf(".") + 1);
            } else {
                result += p;
            }
            first = false;

        }

        result += ")";
        return result;
    }
}

Related

  1. shortenLabel(String key)
  2. shortenMiddle(String msg, int maxLen)
  3. shortenName(final String fullName)
  4. shortenName(final String name, final int toBeAdded)
  5. shortenName(String name)
  6. shortenPackageName(String name, boolean remove)
  7. shortenPart(String currentPart)
  8. shortenPath(final String path)
  9. shortenPrefix(String prefix)