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

functionalj.environments.Log Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// Copyright (c) 2017-2021 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
// 
// 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 functionalj.environments;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Collection;
import java.util.function.Supplier;

import functionalj.function.Func;
import functionalj.function.Func1;
import functionalj.list.FuncList;
import lombok.val;


public final class Log {
    
    private Log() {
    }
    
    public static  Func1 tab() {
        return Log::log;
    }
    public static  Func1 tab(Object prefix) {
        return v -> log(prefix, v);
    }
    public static  Func1 tabf(String format) {
        return v -> {
            log(String.format(format, v));
            return v;
        };
    }
    
    public static  DATA log(DATA value) {
        return Env.log().log(value);
    }
    
    public static  DATA log(Object prefix, DATA value) {
        return Env.log().log(prefix, value);
    }
    public static  DATA log(Object prefix, DATA value, Object suffix) {
        return Env.log().log(prefix, value, suffix);
    }
    
    @SuppressWarnings("unchecked")
    public static  FuncList logEach(DATA ... values) {
        return Env.log().logEach(values);
    }
    
    public static  FuncList logEach(String prefix, Collection values) {
        return Env.log().logEach(prefix, values);
    }
    public static  FuncList logEach(String prefix, Collection values, String suffix) {
        return Env.log().logEach(prefix, values, suffix);
    }
    
    public static  DATA logBy(Supplier supplier) {
        return Env.log().logBy(supplier);
    }
    public static  THROWABLE logErr(THROWABLE throwable) {
        return Env.log().logErr(throwable);
    }
    public static  THROWABLE logErr(Object prefix, THROWABLE throwable) {
        return Env.log().logErr(prefix, throwable);
    }
    public static  THROWABLE logErr(Object prefix, THROWABLE throwable, Object suffix) {
        return Env.log().logErr(prefix, throwable, suffix);
    }
    
    
    public static class Instance {
        
        public static final Instance instance = new Instance();
        
        public  DATA log(DATA value) {
            Env.console().println(value);
            return value;
        }
        
        public  DATA log(Object prefix, DATA value) {
            return log(prefix, value, null);
        }
        public  DATA log(Object prefix, DATA value, Object suffix) {
            val prefixStr = (prefix != null) ? String.valueOf(prefix) : "";
            val suffixStr = (suffix != null) ? String.valueOf(suffix) : "";
            val line      = prefixStr + value + suffixStr;
            log(line);
            return value;
        }
        
        @SuppressWarnings("unchecked")
        public  FuncList logEach(DATA ... values) {
            val funcList = FuncList.of((DATA[])values);
            funcList.forEach(value -> this.log(value));
            return funcList;
        }
        
        public  FuncList logEach(String prefix, Collection values) {
            return logEach(prefix, values, null);
        }
        public  FuncList logEach(String prefix, Collection values, String suffix) {
            val list = FuncList.from(values);
            list.forEach(value -> this.log(prefix, value, suffix));
            return list;
        }
        
        public  DATA logBy(Supplier supplier) {
            DATA value = Func.getOrElse(supplier, null);
            return log(value);
        }
        
        public  THROWABLE logErr(THROWABLE throwable) {
            return logErr(null, throwable, null);
        }
        public  THROWABLE logErr(Object prefix, THROWABLE throwable) {
            return logErr(prefix, throwable, null);
        }
        public  THROWABLE logErr(Object prefix, THROWABLE throwable, Object suffix) {
            val prefixStr = (prefix != null) ? String.valueOf(prefix) + "\n" : "";
            val suffixStr = (suffix != null) ? String.valueOf(suffix) : "";
            val buffer = new ByteArrayOutputStream();
            throwable.printStackTrace(new PrintStream(buffer));
            val toPrint = prefixStr + buffer.toString() + suffixStr;
            Env.console().errPrintln(toPrint);
            return throwable;
        }
        
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy