Package com.adaptrex.core.utilities

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

package com.adaptrex.core.utilities;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adaptrex.core.config.AdaptrexConfig;
import com.adaptrex.core.service.CdiService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

public class StringUtilities {

  private static final Logger log = LoggerFactory.getLogger(StringUtilities.class);
 
  private static ObjectMapper mapper;
  private static ObjectWriter writer;
  public static String json(Object object) {
    if (mapper == null) mapper = new ObjectMapper();
    try {
      AdaptrexConfig config = CdiService.getAdaptrexConfig();
      if (config.get("debug") != null && (Boolean) config.get("debug")) {
        if (writer == null) {
          writer = mapper.writerWithDefaultPrettyPrinter();
        }
        return writer.writeValueAsString(object);
      } else {
        return mapper.writeValueAsString(object);       
      }
    } catch (Exception e) {
      log.warn("Error", e);
      return null;
    }
  }
 
  public static Object fromJson(String json, Class<?> type) {
    if (mapper == null) mapper = new ObjectMapper();
    try {
      return mapper.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.