/*
 *  The MIT License
 *
 *   Copyright (c) 2016, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

package org.easybatch.core.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import static java.lang.String.format;

/**
 * Easy Batch's utilities class.
 *
 * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
 */
public abstract class Utils {

    private static final Logger LOGGER = Logger.getLogger(Utils.class.getName());

    public static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public static final String FILE_SEPARATOR = System.getProperty("file.separator");

    public static final String JAVA_IO_TMPDIR = System.getProperty("java.io.tmpdir");

    public static final String JMX_MBEAN_NAME = "org.easybatch.core.monitor:";

    public static final String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";

    private Utils() {

    }



    /**
     * Return the localhost name
     * @return get host name
     */
    public static String getHostName() {
        String hostName = "N/A";
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            LOGGER.log(Level.WARNING, "Unable to get host name", e);
        }
        return hostName;
    }

    /**
     * Mute easy batch loggers when silent mode is enabled.
     */
    public static void muteLoggers() {
        Enumeration<String> loggerNames = LogManager.getLogManager().getLoggerNames();
        while (loggerNames.hasMoreElements()) {
            String loggerName = loggerNames.nextElement();
            if (loggerName.startsWith("org.easybatch")) {
                muteLogger(loggerName);
            }
        }
    }

    private static void muteLogger(String logger) {
        Logger.getLogger(logger).setUseParentHandlers(false);
        Handler[] handlers = Logger.getLogger(logger).getHandlers();
        for (Handler handler : handlers) {
            Logger.getLogger(logger).removeHandler(handler);
        }
    }

    public static long toMinutes(long milliseconds) {
        return TimeUnit.MILLISECONDS.toMinutes(milliseconds);
    }

    public static void checkNotNull(Object argument, String argumentName) {
        if (argument == null) {
            throw new IllegalArgumentException(format("The %s must not be null", argumentName));
        }
    }

    public static void checkArgument(boolean assertion, String message) {
        if (!assertion) {
            throw new IllegalArgumentException(message);
        }
    }

    public static Map<String, Method> getGetters(final Class type) throws IntrospectionException {
        Map<String, Method> getters = new HashMap<>();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            getters.put(propertyDescriptor.getName(), propertyDescriptor.getReadMethod());
        }
        getters.remove("class"); //exclude property "class"
        return getters;
    }

    public static String formatTime(long time) {
        return new SimpleDateFormat(DATE_FORMAT).format(new Date(time));
    }

}

@@ -1396,122 +1396,27 @@
 ava.
-io.IOException;%0Aimport java.lang.reflect.Method;%0Aimport java.net.InetAddress;%0Aimport java.net.UnknownHostException
+lang.reflect.Method
 ;%0Aim
@@ -1475,38 +1475,8 @@
 te;%0A
-import java.util.Enumeration;%0A
 impo
@@ -1522,182 +1522,8 @@
 Map;
-%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.logging.Handler;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.LogManager;%0Aimport java.util.logging.Logger;
 %0A%0Aim
@@ -1571,22 +1571,9 @@
 %0A * 
-Easy Batch's u
+U
 tili
@@ -1689,91 +1689,8 @@
  %7B%0A%0A
-    private static final Logger LOGGER = Logger.getLogger(Utils.class.getName());%0A%0A
     
@@ -2129,1260 +2129,8 @@
  %7D%0A%0A
-%0A%0A    /**%0A     * Return the localhost name%0A     * @return get host name%0A     */%0A    public static String getHostName() %7B%0A        String hostName = %22N/A%22;%0A        try %7B%0A            hostName = InetAddress.getLocalHost().getHostName();%0A        %7D catch (UnknownHostException e) %7B%0A            LOGGER.log(Level.WARNING, %22Unable to get host name%22, e);%0A        %7D%0A        return hostName;%0A    %7D%0A%0A    /**%0A     * Mute easy batch loggers when silent mode is enabled.%0A     */%0A    public static void muteLoggers() %7B%0A        Enumeration%3CString%3E loggerNames = LogManager.getLogManager().getLoggerNames();%0A        while (loggerNames.hasMoreElements()) %7B%0A            String loggerName = loggerNames.nextElement();%0A            if (loggerName.startsWith(%22org.easybatch%22)) %7B%0A                muteLogger(loggerName);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private static void muteLogger(String logger) %7B%0A        Logger.getLogger(logger).setUseParentHandlers(false);%0A        Handler%5B%5D handlers = Logger.getLogger(logger).getHandlers();%0A        for (Handler handler : handlers) %7B%0A            Logger.getLogger(logger).removeHandler(handler);%0A        %7D%0A    %7D%0A%0A    public static long toMinutes(long milliseconds) %7B%0A        return TimeUnit.MILLISECONDS.toMinutes(milliseconds);%0A    %7D%0A%0A
     

remove unused methods