Example usage for org.apache.commons.lang3 StringUtils left

List of usage examples for org.apache.commons.lang3 StringUtils left

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils left.

Prototype

public static String left(final String str, final int len) 

Source Link

Document

Gets the leftmost len characters of a String.

If len characters are not available, or the String is null , the String will be returned without an exception.

Usage

From source file:suonos.lucene.ModelType.java

private void addFieldToDoc(Document doc, IndexModels models, IndexedField indexedField, Object val) {

    if (val == null)
        return;//from w w w .java2s  . c  o  m

    Field fld = indexedField.createField(val);
    addToDoc(doc, fld);

    // Somewhat of a kludge!!!!
    // The default token analyser used is SimpleAnalyzer. This will apply
    // lowercase to the tokens. In order to keep
    // consistency, we also have to apply a lowercase to untokenised values.
    //
    Object filter_val = getFilterValue(val);

    // If indexed and docValues, we have already created the normal indexed
    // field.
    // We now have to create a DocValue field with the same name. This will
    // be used for sorting and faceting.
    // The DocValue field is not used during queries:
    // Field fieldName field value field type flags terms
    // StringField muppetName "Beaker Muppet" indexed tokenised "beaker"
    // "muppet" (multiple terms)
    // SortedSetDocValuesField muppetName "Beaker Muppet" "Beaker Muppet"
    // (stored untokenised)
    //
    // In lucene it's perfectly legit to have a docfield and normal indexed
    // field with the same name as they are
    // used for different purposes.
    //
    if (indexedField.isIndexed() && indexedField.isDocValues()) {
        fld = indexedField.createDocValueField(indexedField.getName(), val);
        addToDoc(doc, fld);

        // Add the field value as untokenised. Used for querying all
        // documents containing a facet value.
        // Eg: All albums for genre "Classical"
        //
        IndexedField ndxField = models.getIndexedField(indexedField.getName().concat("_u"));
        fld = ndxField.createField(filter_val);
        addToDoc(doc, fld);
    }

    if (indexedField.isFilterable()) {
        // Create the following fields:
        // Name Type Use
        // field_s DocValuesField Sorting. Eg: "/api/albums?s=title_s" Sort
        // albums by abbrev title (first 4 characters)
        // field_a DocValuesField Faceting. Eg:
        // "/api/facets?fields=album_artists_a" returns all first letters
        // for artists.
        // field_a Normal Field Single character Queries. Eg
        // "/api/albums?q=title_a:B" All titles starting with "B"
        //
        String filterValue = (String) filter_val;
        filterValue = StringUtils.left(filterValue, 4);

        if (filterValue != null) {
            // field_s (DocValueField) max len is 4 characters.
            //
            String filterFieldName = indexedField.getName().concat("_s");

            // For faceting/sorting only. No need to create a normal field
            // for this!
            // Eg: GET "/api/albums?s=title_s"
            // Sorting on tokens that are only 4 letters in length is more
            // efficient than sorting on the full length.
            //
            fld = indexedField.createDocValueField(filterFieldName, filterValue);
            addToDoc(doc, fld);

            // field_a (DocValue) and (Field)
            // For Alphabet browsing.
            // Eg: A / B / C / D / E / F
            //
            filterFieldName = indexedField.getName().concat("_a");
            filterValue = filterValue.substring(0, 1);

            // Create DocField for faceting
            //
            fld = indexedField.createDocValueField(filterFieldName, filterValue);
            addToDoc(doc, fld);

            // Get the IndexedField - field_a
            //
            IndexedField ndxField = models.getIndexedField(filterFieldName);

            // Create normal field for simple queries by first character.
            // /api/albums?q=title_a=B (find all albums beginning with the
            // letter "B")
            //
            fld = ndxField.createField(filterValue);
            addToDoc(doc, fld);

            // Should we add a new field - artist_e (untokenised token) for
            // searching albums by exact author.
        }
    }
}

From source file:tech.sirwellington.alchemy.generator.StringGenerators.java

/**
 * Generates a random hexadecimal string.
 *
 * @param length The length of the String, must be at least 1.
 *
 * @return//  www  .ja  v  a2  s .  com
 */
public static AlchemyGenerator<String> hexadecimalString(int length) {
    checkThat(length > 0, "Length must be at least 1");
    HexBinaryAdapter hexBinaryAdapter = new HexBinaryAdapter();
    AlchemyGenerator<byte[]> binaryGenerator = binary(length);

    return () -> {
        byte[] binary = one(binaryGenerator);
        String hex = hexBinaryAdapter.marshal(binary);
        return StringUtils.left(hex, length);
    };
}