com.aurel.track.lucene.util.StringUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.aurel.track.lucene.util.StringUtil.java

Source

/**
 * Genji Scrum Tool and Issue Tracker
 * Copyright (C) 2015 Steinbeis GmbH & Co. KG Task Management Solutions
    
 * <a href="http://www.trackplus.com">Genji Scrum Tool</a>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/* $Id:$ */

package com.aurel.track.lucene.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

/**
 * <a href="StringUtil.java.html"><b><i>View Source</i></b></a>
 *
 * @author  Brian Wing Shun Chan
 * @version $Revision: 1286 $
 *
 */
public class StringUtil {

    private static final Logger LOGGER = LogManager.getLogger(StringUtil.class);

    private StringUtil() {
        // hide the constructor
    }

    public static String add(String s, String add) {
        return add(s, add, StringPool.COMMA);
    }

    public static String add(String s, String add, String delimiter) {
        return add(s, add, delimiter, false);
    }

    public static String add(String s, String add, String delimiter, boolean allowDuplicates) {

        if ((add == null) || (delimiter == null)) {
            return null;
        }

        if (s == null) {
            s = StringPool.BLANK;
        }

        StringBuilder sb = new StringBuilder(s);

        if (allowDuplicates || !contains(s, add, delimiter)) {
            if (Validator.isNull(s) || s.endsWith(delimiter)) {
                sb.append(add + delimiter);
            } else {
                sb.append(delimiter + add + delimiter);
            }
        }

        return sb.toString();
    }

    public static boolean contains(String s, String text) {
        return contains(s, text, StringPool.COMMA);
    }

    public static boolean contains(String s, String text, String delimiter) {
        if ((s == null) || (text == null) || (delimiter == null)) {
            return false;
        }

        StringBuilder sb = new StringBuilder(s);

        if (!s.endsWith(delimiter)) {
            sb.append(delimiter);
        }

        int pos = sb.indexOf(delimiter + text + delimiter);

        if (pos == -1) {
            if (sb.toString().startsWith(text + delimiter)) {
                return true;
            }

            return false;
        }

        return true;
    }

    public static int count(String s, String text) {
        if ((s == null) || (text == null) || "".equals(s) || "".equals(text)) {
            return 0;
        }

        int count = 0;

        int pos = s.indexOf(text);

        while (pos != -1) {
            pos = s.indexOf(text, pos + text.length());
            count++;
        }

        return count;
    }

    public static boolean endsWith(String s, char end) {
        return endsWith(s, Character.toString(end));
    }

    public static boolean endsWith(String s, String end) {
        if ((s == null) || (end == null)) {
            return false;
        }

        if (end.length() > s.length()) {
            return false;
        }

        String temp = s.substring(s.length() - end.length(), s.length());

        return temp.equalsIgnoreCase(end);

    }

    public static String extractChars(String s) {
        if (s == null) {
            return "";
        }

        char[] c = s.toCharArray();

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < c.length; i++) {
            if (Validator.isChar(c[i])) {
                sb.append(c[i]);
            }
        }

        return sb.toString();
    }

    public static String extractDigits(String s) {
        if (s == null) {
            return "";
        }

        char[] c = s.toCharArray();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < c.length; i++) {
            if (Validator.isDigit(c[i])) {
                sb.append(c[i]);
            }
        }

        return sb.toString();
    }

    public static String merge(List list) {
        return merge(list, StringPool.COMMA);
    }

    public static String merge(List list, String delimiter) {
        return merge((String[]) list.toArray(new String[0]), delimiter);
    }

    public static String merge(String array[]) {
        return merge(array, StringPool.COMMA);
    }

    public static String merge(String array[], String delimiter) {
        if (array == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < array.length; i++) {
            sb.append(array[i].trim());

            if ((i + 1) != array.length) {
                sb.append(delimiter);
            }
        }

        return sb.toString();
    }

    public static String read(ClassLoader classLoader, String name) throws IOException {

        return read(classLoader.getResourceAsStream(name));
    }

    public static String read(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = br.readLine()) != null) {
            sb.append(line).append('\n');
        }

        br.close();

        return sb.toString().trim();
    }

    public static String remove(String s, String remove) {
        return remove(s, remove, StringPool.COMMA);
    }

    public static String remove(String sp, String remove, String delimiter) {
        if ((sp == null) || (remove == null) || (delimiter == null)) {
            return sp;
        }

        if ("".equals(sp) || "".equals(remove)) {
            return sp;
        }

        StringBuilder sb = new StringBuilder(sp);

        if (Validator.isNotNull(sp) && !sp.endsWith(delimiter)) {
            sb.append(delimiter);
        }

        String s = sb.toString();

        while (contains(s, remove, delimiter)) {
            int pos = s.indexOf(delimiter + remove + delimiter);

            if (pos == -1) {
                if (s.startsWith(remove + delimiter)) {
                    s = s.substring(remove.length() + delimiter.length(), s.length());
                }
            } else {
                s = s.substring(0, pos) + s.substring(pos + remove.length() + delimiter.length(), s.length());
            }
        }

        return s;
    }

    public static String replace(String s, char oldSub, char newSub) {
        return replace(s, oldSub, Character.toString(newSub));
    }

    public static String replace(String s, char oldSub, String newSub) {
        if ((s == null) || (newSub == null) || "".equals(s)) {
            return s;
        }

        char[] c = s.toCharArray();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < c.length; i++) {
            if (c[i] == oldSub) {
                sb.append(newSub);
            } else {
                sb.append(c[i]);
            }
        }

        return sb.toString();
    }

    public static String replace(String s, String oldSub, String newSub) {
        if ((s == null) || (oldSub == null) || (newSub == null) || "".equals(s) || "".equals(oldSub)) {
            return s;
        }

        int y = s.indexOf(oldSub);

        if (y >= 0) {
            StringBuilder sb = new StringBuilder();

            int length = oldSub.length();
            int x = 0;

            while (x <= y) {
                sb.append(s.substring(x, y));
                sb.append(newSub);
                x = y + length;
                y = s.indexOf(oldSub, x);
            }

            sb.append(s.substring(x));

            return sb.toString();
        } else {
            return s;
        }
    }

    public static String replace(String sp, String[] oldSubs, String[] newSubs) {
        if ((sp == null) || (oldSubs == null) || (newSubs == null)) {
            return null;
        }

        if (oldSubs.length != newSubs.length) {
            return sp;
        }

        String s = new String(sp);

        for (int i = 0; i < oldSubs.length; i++) {
            s = replace(s, oldSubs[i], newSubs[i]);
        }

        return s;
    }

    public static String reverse(String s) {
        if (s == null) {
            return null;
        }

        char[] c = s.toCharArray();
        char[] reverse = new char[c.length];

        for (int i = 0; i < c.length; i++) {
            reverse[i] = c[c.length - i - 1];
        }

        return new String(reverse);
    }

    public static String shorten(String s) {
        return shorten(s, 20);
    }

    public static String shorten(String s, int length) {
        return shorten(s, length, "..");
    }

    public static String shorten(String s, String suffix) {
        return shorten(s, 20, suffix);
    }

    public static String shorten(String sp, int length, String suffix) {
        if (sp == null || suffix == null) {
            return null;
        }

        String s = new String(sp);

        if (sp.length() > length) {
            s = sp.substring(0, length) + suffix;
        }

        return s;
    }

    public static String[] split(String s) {
        return split(s, StringPool.COMMA);
    }

    public static String[] split(String sp, String delimiter) {
        if (sp == null || delimiter == null) {
            return new String[0];
        }

        String s = new String(sp);

        s = s.trim();

        if (!s.endsWith(delimiter)) {
            s += delimiter;
        }

        if (s.equals(delimiter)) {
            return new String[0];
        }

        List nodeValues = new ArrayList();

        if ("\n".equals(delimiter) || "\r".equals(delimiter)) {
            try {
                BufferedReader br = new BufferedReader(new StringReader(s));

                String line = null;

                while ((line = br.readLine()) != null) {
                    nodeValues.add(line);
                }

                br.close();
            } catch (IOException ioe) {
                LOGGER.error(ExceptionUtils.getStackTrace(ioe));
            }
        } else {
            int offset = 0;
            int pos = s.indexOf(delimiter, offset);

            while (pos != -1) {
                nodeValues.add(s.substring(offset, pos));

                offset = pos + delimiter.length();
                pos = s.indexOf(delimiter, offset);
            }
        }

        return (String[]) nodeValues.toArray(new String[0]);
    }

    public static boolean[] split(String s, String delimiter, boolean x) {
        String[] array = split(s, delimiter);
        boolean[] newArray = new boolean[array.length];

        for (int i = 0; i < array.length; i++) {
            boolean value = x;

            try {
                value = Boolean.valueOf(array[i]).booleanValue();
            } catch (Exception e) {
                LOGGER.warn(e.getMessage());
                LOGGER.debug(e);
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static double[] split(String s, String delimiter, double x) {
        String[] array = split(s, delimiter);
        double[] newArray = new double[array.length];

        for (int i = 0; i < array.length; i++) {
            double value = x;

            try {
                value = Double.parseDouble(array[i].trim());
            } catch (Exception e) {
                LOGGER.warn(e.getMessage());
                LOGGER.debug(e);
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static float[] split(String s, String delimiter, float x) {
        String[] array = split(s, delimiter);
        float[] newArray = new float[array.length];

        for (int i = 0; i < array.length; i++) {
            float value = x;

            try {
                value = Float.parseFloat(array[i].trim());
            } catch (Exception e) {
                LOGGER.warn(e.getMessage());
                LOGGER.debug(e);
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static int[] split(String s, String delimiter, int x) {
        String[] array = split(s, delimiter);
        int[] newArray = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            int value = x;

            try {
                value = Integer.parseInt(array[i].trim());
            } catch (Exception e) {
                LOGGER.warn(e.getMessage());
                LOGGER.debug(e);
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static long[] split(String s, String delimiter, long x) {
        String[] array = split(s, delimiter);
        long[] newArray = new long[array.length];

        for (int i = 0; i < array.length; i++) {
            long value = x;

            try {
                value = Long.parseLong(array[i]);
            } catch (Exception e) {
                LOGGER.warn(e.getMessage());
                LOGGER.debug(e);
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static short[] split(String s, String delimiter, short x) {
        String[] array = split(s, delimiter);
        short[] newArray = new short[array.length];

        for (int i = 0; i < array.length; i++) {
            short value = x;

            try {
                value = Short.parseShort(array[i]);
            } catch (Exception e) {
                LOGGER.warn(e.getMessage());
                LOGGER.debug(e);
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static boolean startsWith(String s, char begin) {
        return startsWith(s, Character.toString(begin));
    }

    public static boolean startsWith(String s, String start) {
        if ((s == null) || (start == null)) {
            return false;
        }

        if (start.length() > s.length()) {
            return false;
        }

        String temp = s.substring(0, start.length());

        return temp.equalsIgnoreCase(start);

    }

    public static String trimLeading(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return s.substring(i, s.length());
            }
        }

        return StringPool.BLANK;
    }

    public static String trimTrailing(String s) {
        for (int i = s.length() - 1; i >= 0; i--) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return s.substring(0, i + 1);
            }
        }

        return StringPool.BLANK;
    }

    public static String wrap(String text) {
        return wrap(text, 80, "\n");
    }

    public static String wrap(String text, int width, String lineSeparator) {
        if (text == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new StringReader(text));

            String s = StringPool.BLANK;

            while ((s = br.readLine()) != null) {
                if (s.length() == 0) {
                    sb.append(lineSeparator);
                } else {
                    String[] tokens = s.split(StringPool.SPACE);
                    boolean firstWord = true;
                    int curLineLength = 0;

                    for (int i = 0; i < tokens.length; i++) {
                        if (!firstWord) {
                            sb.append(StringPool.SPACE);
                            curLineLength++;
                        }

                        if (firstWord) {
                            sb.append(lineSeparator);
                        }

                        sb.append(tokens[i]);

                        curLineLength += tokens[i].length();

                        if (curLineLength >= width) {
                            firstWord = true;
                            curLineLength = 0;
                        } else {
                            firstWord = false;
                        }
                    }
                }
            }
        } catch (IOException ioe) {
            LOGGER.error(ExceptionUtils.getStackTrace(ioe));
        }

        return sb.toString();
    }

}