Java String Shorten shortenClassName(Class clazz, int maxLength)

Here you can find the source of shortenClassName(Class clazz, int maxLength)

Description

shorten Class Name

License

LGPL

Declaration

public static String shortenClassName(Class<?> clazz, int maxLength) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static String shortenClassName(Class<?> clazz, int maxLength) {
        String name = clazz.getSimpleName();
        Package pkg = clazz.getPackage();
        if (pkg == null) {
            return name;
        } else {//from   ww  w.j  a va2  s .  co m
            String[] tokens = pkg.getName().split("\\.");
            boolean shortened = false;
            for (int i = tokens.length - 1; i > -1; i--) {
                if (shortened || (name.length() + tokens[i].length() + 1 + (i * 2)) > maxLength) {
                    tokens[i] = tokens[i].substring(0, 1);
                    shortened = true;
                }
                name = tokens[i] + "." + name;
            }
        }
        return name;
    }
}

Related

  1. shorten(String text, int size, int mode)
  2. shorten(String[] a, int from, int to)
  3. shortenAddress(String address)
  4. shortenArray(byte[] src, int length)
  5. shortenClassName(Class klass)
  6. shortenClassName(String cname)
  7. shortenClassName(String str)
  8. shortenComponentLoggerName(String loggerName)
  9. shortenContent(String s, int maxLength)