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

com.mitchellbosecke.pebble.extension.core.CapitalizeFilter Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
/*******************************************************************************
 * This file is part of Pebble.
 * 
 * Copyright (c) 2014 by Mitchell Bösecke
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 ******************************************************************************/
package com.mitchellbosecke.pebble.extension.core;

import com.mitchellbosecke.pebble.extension.Filter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class CapitalizeFilter implements Filter {

    @Override
    public List getArgumentNames() {
        return null;
    }

    @Override
    public Object apply(Object input, Map args) {
        if (input == null) {
            return null;
        }
        String value = (String) input;

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

        StringBuilder result = new StringBuilder();

        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];

            if (Character.isWhitespace(c)) {
                result.append(c);
            } else {
                result.append(Character.toTitleCase(c));
                result.append(Arrays.copyOfRange(chars, i + 1, chars.length));
                break;
            }
        }

        return result.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy