com.enremmeta.onenow.Utils.java Source code

Java tutorial

Introduction

Here is the source code for com.enremmeta.onenow.Utils.java

Source

package com.enremmeta.onenow;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Various utilities. All methods here are static.
 * 
 * @author Gregory Golberg (grisha@alum.mit.edu)
 * 
 *         Copyright  <a href="http://www.enremmeta.com">Enremmeta LLC</a>
 *         2014. All Rights Reserved. This code is licensed under <a
 *         href="http://www.gnu.org/licenses/agpl-3.0.html">Alfero GPL 3.0</a>
 * 
 */
public class Utils {

    /**
     * All methods here are static.
     */
    private Utils() {
        // TODO Auto-generated constructor stub
    }

    /**
     * There are various logs in the Lot49 project. This is a utility method to
     * quickly log to the right one. Here we also explain what logs there are.
     *
     * <ul>
     * <li>{@link LogUtils#LOG_BID bid.log}
     * </ul>
     * 
     * All logs use {@link TimeAndSizeRollingAppender}, so that they are rotated
     * by size and hourly (And then end up in the data warehouse, which in our
     * case means Amazon S3, because we are huge Amazon fans, to the point of
     * being AWS Certified Solution Architects, Consulting Partners and
     * groupies.
     * 
     * @throws Lot49Exception
     */
    public static void log() {

    }

    public static String csvFormat(Object... obj) {
        String retval = "";
        for (Object o : obj) {
            if (retval.length() > 0) {
                retval += ",";
            }
            if (o == null) {
                o = "";
            }
            String s = o.toString();
            s = s.replaceAll("\\\\", "\\\\\\\\");
            s = s.replaceAll("\"", "\\\"");
            retval += "\"" + s + "\"";
        }
        return retval;
    }

    public static String readFile(String s) throws IOException {
        return readFile(new File(s));
    }

    public static String capitalize(String s) {
        return Character.toUpperCase(s.charAt(0)) + s.substring(1);
    }

    public static Config loadConfig(String fileName, Class<? extends Config> configClass)
            throws MalformedURLException, IOException {
        JsonFactory jsonFactory = new JsonFactory();
        jsonFactory.configure(Feature.ALLOW_COMMENTS, true);
        ObjectMapper mapper = new ObjectMapper(jsonFactory);
        Config config;

        if (fileName.startsWith("http://") || fileName.startsWith("https://")) {
            URL url = new URL(fileName);
            config = mapper.readValue(url, configClass);
        } else {
            File file = new File(fileName);
            config = mapper.readValue(file, configClass);
        }
        return config;
    }

    public static String readFile(File f) throws IOException {
        byte[] encoded;
        encoded = Files.readAllBytes(f.toPath());
        return new String(encoded);
    }

}