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

org.joinedworkz.common.helper.StringHelper Maven / Gradle / Ivy

package org.joinedworkz.common.helper;

import javax.inject.Singleton;

@Singleton
@SuppressWarnings("all")
public class StringHelper {
  public static final char NL = '\n';

  public static final char CR = '\r';

  public boolean isNewLineChar(final char c) {
    return ((c == StringHelper.NL) || (c == StringHelper.CR));
  }

  public boolean beginsLineChar(final StringBuilder buffer) {
    return ((buffer.length() == 0) || this.isNewLineChar(buffer.charAt((buffer.length() - 1))));
  }

  public String trimLastLineBreak(final String str) {
    if ((str != null)) {
      int totalCount = 0;
      int crCount = 0;
      int nlCount = 0;
      for (int i = (str.length() - 1); (i >= 0); i--) {
        {
          totalCount++;
          final char c = str.charAt(i);
          if ((c == StringHelper.CR)) {
            crCount++;
          }
          if ((c == StringHelper.NL)) {
            nlCount++;
          }
          if ((totalCount > (crCount + nlCount))) {
            if ((totalCount == 1)) {
              return str;
            } else {
              int _length = str.length();
              int _minus = (_length - totalCount);
              int _plus = (_minus + 1);
              return str.substring(0, _plus);
            }
          }
        }
      }
    }
    return null;
  }

  public String contentOfLineBefore(final String str, final int index) {
    for (int i = index; (i >= 0); i--) {
      {
        final char c = str.charAt(i);
        if (((c == StringHelper.CR) || (c == StringHelper.NL))) {
          return str.substring((i + 1), index);
        }
      }
    }
    return str;
  }

  public String contentOfLineAfter(final String str, final int index) {
    final int strLength = str.length();
    for (int i = index; (i < strLength); i++) {
      {
        final char c = str.charAt(i);
        if (((c == StringHelper.CR) || (c == StringHelper.NL))) {
          return str.substring(index, i);
        }
      }
    }
    return str.substring(index);
  }

  public boolean containsLineBreak(final String str) {
    if ((str != null)) {
      return ((str.indexOf(StringHelper.CR) >= 0) || (str.indexOf(StringHelper.NL) >= 0));
    } else {
      return false;
    }
  }

  public void insertLinesOfString(final StringBuilder buffer, final String str, final String indention) {
    int _length = str.trim().length();
    final boolean hasContent = (_length > 0);
    if ((!hasContent)) {
      return;
    }
    final String[] lines = str.split("\\r?\\n");
    int _length_1 = str.length();
    int _minus = (_length_1 - 1);
    final char lastChar = str.charAt(_minus);
    int _length_2 = indention.length();
    final boolean hasIndention = (_length_2 > 0);
    final int lineCount = lines.length;
    final int lastLine = (lineCount - 1);
    for (int i = 0; (i < lineCount); i++) {
      {
        final String line = lines[i];
        if ((hasIndention && this.beginsLineChar(buffer))) {
          buffer.append(indention);
        }
        buffer.append(line);
        if (((i < lastLine) || this.isNewLineChar(lastChar))) {
          buffer.append("\r\n");
        }
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy