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

com.github.siwenyan.common.Sys Maven / Gradle / Ivy

There is a newer version: 1.25
Show newest version
package com.github.siwenyan.common;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

public class Sys {
    private static Logger log = Logger.getLogger(Sys.class.getName());

    public static Properties conf = Conf.getInstance();

    public static void setConf(Properties conf0) {
        if (null != conf0) {
            conf = conf0;
        }
    }

    private static IFileFinder fileFinder = new IFileFinder() {
        @Override
        public File find(String path) {
            try {
                File file = new File(path);
                if (file.exists()) {
                    return file;
                } else {
                    file = new File("../" + path);
                    if (file.exists()) {
                        return file;
                    } else {
                        file = new File(dir() + path);
                        if (file.exists()) {
                            return file;
                        } else {
                            return null;
                        }
                    }
                }
            } catch (Exception e) {
                return null;
            }
        }
    };

    public static void setFileFinder(IFileFinder fileFinder0) {
        if (null != fileFinder0) {
            fileFinder = fileFinder0;
        }
    }

    public final static Reporter out = new Reporter(System.out);
    public final static Scanner in = new Scanner(System.in);

    public final static String DIR = System.getProperty("user.dir");
    public final static String SEP = System.getProperty("file.separator");
    public static final String LBR = System.getProperty("line.separator");

    private final static String OUT = DIR + SEP + "target" + SEP + "output" + SEP;

    public static void sleep(long l) {
        try {
            Thread.sleep(l);
        } catch (InterruptedException e) {
            // do nothing
        }
    }

    public static void wait(String message, int sec) {
        behold(message);
        sleep(sec);
    }

    public static void behold(String prompt) {
        Sys.out.pl(prompt);
        Sys.out.pl(Sys.in.nextLine());
    }

    public static String dir() {
        return DIR + SEP;
    }

    public static String outputDir() {
        return conf.getProperty("output", OUT);
    }

    public static File findFile(String path) {
        return fileFinder.find(path);
    }

    /**
     * For logging: write a String or random Object instance as a json file
     *
     * @param name  name
     * @param value value
     */
    public static void tryLoggingJsonString(String name, Object value) {
        try {
            if (StringTools.isEmpty(name) || null == value) {
                return;
            }
            String jsonString = null;
            if (value instanceof String) {
                String sValue = (String) value;
                if (StringTools.isEmpty(sValue)) {
                    return;
                }
                sValue = sValue.trim();
                if (sValue.startsWith("{") || sValue.startsWith("[")) {
                    jsonString = EasyJson.prettyJson(sValue);
                } else {
                    return;
                }
            } else {
                jsonString = EasyJson.prettyJson(value);
            }
            if (StringTools.isEmpty(jsonString)) {
                return;
            } else {
                String logFileName = name + "_" + DateTimeTools.timestamp();
                FileUtils.writeStringToFile(new File(Sys.outputDir() + logFileName + ".json"), jsonString);
            }
        } catch (IOException e) {
            e.printStackTrace();
            // do nothing
        }
    }

    public synchronized static String getUniqueNameWithinFolder(String folderFullPath, String name, String dotExt,
                                                                boolean override) {
        String uniqueFullpath = folderFullPath + name + dotExt;
        if (override) {
            File file = new File(uniqueFullpath);
            if (file.exists()) {
                file.delete();
            }
            return uniqueFullpath;
        }

        if (!new File(uniqueFullpath).exists()) {
            return name;
        }

        int seq = 0;
        while (new File(uniqueFullpath).exists()) {
            uniqueFullpath = folderFullPath + name + "_" + ++seq + dotExt;
        }

        return name + "_" + seq;
    }

    public static void tryOutputFile(String fn, String s) {
        try {
            FileUtils.writeStringToFile(new File(outputDir() + fn), s);
        } catch (Exception e) {
            // do nothing
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy