Package com.adaptrex.core.utilities

Source Code of com.adaptrex.core.utilities.StringUtilities

/*
* Copyright 2012 Adaptrex, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adaptrex.core.utilities;

import com.adaptrex.core.AdaptrexConfig;
import com.adaptrex.core.AdaptrexRegistry;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

import java.util.List;

import org.apache.log4j.Logger;

public class StringUtilities {

  private static Logger log = Logger.getLogger(StringUtilities.class);

    public static String json(Object object) {
        try {
      Boolean debug = Boolean.valueOf(AdaptrexRegistry.getConfig().get(AdaptrexConfig.DEBUG));
      ObjectMapper mapper = new ObjectMapper();
            if (debug) {
                ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
                return writer.writeValueAsString(object);
            } else {
                return mapper.writeValueAsString(object);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Object fromJson(String json, Class<?> type) {
        try {
            return (new ObjectMapper()).readValue(json, type);
        } catch (Exception e) {
            log.warn("Error", e);
            return null;
        }
    }

    static public String pluralize(String singular) {
        char last = singular.charAt(singular.length() - 1);
        switch (last) {
            case 'x':
            case 's':
                singular += "es";
                break;
            case 'y':
                singular = singular.substring(0, singular.length() - 1) + "ies";
                break;
            default:
                singular += "s";
        }
        return singular;
    }

    public static String join(List<String> strings, String delimiter) {
        String j = "";
        for (String item : strings) {
            if (!j.isEmpty()) {
                j += delimiter;
            }
            j += item;
        }
        return j;
    }

    public static String capitalize(String string) {
        return string.substring(0, 1).toUpperCase() + string.substring(1);
    }

    public static String uncapitalize(String string) {
        return string.substring(0, 1).toLowerCase() + string.substring(1);
    }
}
TOP

Related Classes of com.adaptrex.core.utilities.StringUtilities

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.