Example usage for java.lang CharSequence length

List of usage examples for java.lang CharSequence length

Introduction

In this page you can find the example usage for java.lang CharSequence length.

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:org.cleverbus.common.Strings.java

public static boolean isEmpty(@Nullable CharSequence c) {
    return (c == null) || (c.length() == 0);
}

From source file:org.cleverbus.common.Strings.java

public static boolean isBlank(@Nullable CharSequence c) {
    return (c == null) || (c.length() == 0) || (trim(c).length() == 0);
}

From source file:Main.java

/**
 * Green implementation of regionMatches.
 *
 * @param cs         the {@code CharSequence} to be processed
 * @param ignoreCase whether or not to be case insensitive
 * @param thisStart  the index to start on the {@code cs} CharSequence
 * @param substring  the {@code CharSequence} to be looked for
 * @param start      the index to start on the {@code substring} CharSequence
 * @param length     character length of the region
 * @return whether the region matched//from w w w  .  ja  va2 s .  c o  m
 */
static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
        final CharSequence substring, final int start, final int length) {
    if (cs instanceof String && substring instanceof String) {
        return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
    }
    int index1 = thisStart;
    int index2 = start;
    int tmpLen = length;

    // Extract these first so we detect NPEs the same as the java.lang.String version
    final int srcLen = cs.length() - thisStart;
    final int otherLen = substring.length() - start;

    // Check for invalid parameters
    if (thisStart < 0 || start < 0 || length < 0) {
        return false;
    }

    // Check that the regions are long enough
    if (srcLen < length || otherLen < length) {
        return false;
    }

    while (tmpLen-- > 0) {
        final char c1 = cs.charAt(index1++);
        final char c2 = substring.charAt(index2++);

        if (c1 == c2) {
            continue;
        }

        if (!ignoreCase) {
            return false;
        }

        // The same check as in String.regionMatches():
        if (Character.toUpperCase(c1) != Character.toUpperCase(c2)
                && Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
            return false;
        }
    }

    return true;
}

From source file:net.darkmist.alib.res.PkgRes.java

/**
 * Gets a resource path using cls's package name as the prefix.
 * @param name The name of the resource.
 * @param cls The class to get the package prefix from.
 * @return Path of a resource prefixed by the class package name.
 * @throws NullPointerException if name or cls are null.
 *///  w ww .j a  v  a 2 s. co m
public static String getResourcePathFor(CharSequence name, Class<?> cls) {
    int nameLen;
    StringBuilder sb;

    if (name == null)
        throw new NullPointerException("name is null");
    if (cls == null)
        throw new NullPointerException("cls is null");
    nameLen = name.length();
    sb = new StringBuilder(cls.getName().length() + nameLen + 2);
    appendResourcePathPrefixFor(sb, cls);
    cls = null;
    if (name.charAt(0) != '/')
        sb.append(name, 1, nameLen);
    else
        sb.append(name);
    name = null;
    return sb.toString();
}

From source file:be.milieuinfo.core.proxy.controller.ProxyServlet.java

/**
 * <p>Encodes characters in the query or fragment part of the URI.
 *
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}. To be more
 * forgiving, we must escape the problematic characters.  See the URI class for the spec.
 *
 * @param in example: name=value&foo=bar#fragment
 */// w ww. j ava  2  s .co  m
static CharSequence encodeUriQuery(CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            //escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);//TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:org.realityforge.proxy_servlet.AbstractProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 * <p/>//from  w  w w  .j  a  v  a  2  s.co  m
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link java.net.URI}.
 * To be more forgiving, we must escape the problematic characters.  See the URI class for the
 * spec.
 *
 * @param in example: name=value&foo=bar#fragment
 */
private static CharSequence encodeUriQuery(final CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder sb = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < MAX_ASCII_VALUE) {
            if (ASCII_QUERY_CHARS.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {
            //not-ascii
            escape = false;
        }
        if (!escape) {
            if (null != sb) {
                sb.append(c);
            }
        } else {
            //escape
            if (null == sb) {
                final int formatLength = 5 * 3;
                sb = new StringBuilder(in.length() + formatLength);
                sb.append(in, 0, i);
                formatter = new Formatter(sb);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);
        }
    }
    return sb != null ? sb : in;
}

From source file:uk.ac.ebi.phenotype.web.proxy.ExternalUrlConfiguratbleProxyServlet.java

/**
 * <p>//  w  ww  .  j a  v  a2  s  . com
 * Encodes characters in the query or fragment part of the URI.
 * 
 * <p>
 * Unfortunately, an incoming URI sometimes has characters disallowed by the
 * spec. HttpClient insists that the outgoing proxied request has a valid
 * URI because it uses Java's {@link URI}. To be more forgiving, we must
 * escape the problematic characters. See the URI class for the spec.
 * 
 * @param in
 *            example: name=value&foo=bar#fragment
 */
static CharSequence encodeUriQuery(CharSequence in) {
    // Note that I can't simply use URI.java to encode because it will
    // escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {// not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            // escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            // leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);// TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:org.jets3t.service.utils.SignatureUtils.java

/**
 * Slightly modified version of "uri-encode" from:
 * {@link "http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html"}
 *
 * @param input//w  ww. j a  v  a  2s.  c o m
 * URI or URI-fragment string to encode.
 * @param encodeSlash
 * true if slash (/) character should be encoded.
 * @return URI string encoded per recommendations from AWS.
 */
public static String awsV4EncodeURI(CharSequence input, boolean encodeSlash) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_'
                || ch == '-' || ch == '~' || ch == '.') {
            result.append(ch);
        } else if (ch == '/') {
            result.append(encodeSlash ? "%2F" : ch);
        } else {
            String hex = RestUtils.encodeUrlString(String.valueOf(ch));
            result.append(hex);
        }
    }
    return result.toString();
}

From source file:org.cleverbus.common.Strings.java

/**
 * @param input the {@link CharSequence} to be trimmed
 * @return the trimmed char sequence//from  w  w  w  .j  a  v  a 2s  . c o  m
 * @since 3.8.2
 */
@Nullable
public static CharSequence trim(@Nullable CharSequence input) {
    if (input == null) {
        return null;
    }

    if (input.length() == 0) {
        return input;
    }

    if (input instanceof String) {
        return ((String) input).trim();
    }

    int count = input.length();
    int len = count;
    int st = 0;
    int off = 0;

    while ((st < len) && (input.charAt(off + st) <= ' ')) {
        st++;
    }
    while ((st < len) && (input.charAt((off + len) - 1) <= ' ')) {
        len--;
    }

    if ((st == 0) && (input instanceof StringBuilder)) {
        ((StringBuilder) input).setLength(len);
        return input;
    }

    return ((st > 0) || (len < count)) ? input.subSequence(st, len) : input;
}

From source file:jp.co.ctc_g.jse.core.validation.util.Validators.java

/**
 * ???????????//from w  w  w . jav a2s  . c  om
 * ???\t, \n, \x, 0B, \f, \r, ?, ??????
 * @param suspect 
 * @return ?????true
 */
public static boolean containBlank(CharSequence suspect) {
    return suspect == null || suspect.length() == 0 || BLANK_PATTERN.matcher(suspect).find();
}