java 适用于Java 8的NullHandlers

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 适用于Java 8的NullHandlers相关的知识,希望对你有一定的参考价值。

package com.bacon.util;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;
import java.util.function.Supplier;

public class NullHandlers {
	
	public static final Supplier<String> EMPTY_STRING = () -> "";
	public static final Supplier<Date> UNIX_START = () -> DateTimeUtils.parseToDate("1970-01-01", "yyyy-MM-dd");
	public static final Supplier<BigDecimal> NEGATIVE_ONE = () -> BigDecimal.valueOf(-1);
	
	private NullHandlers() {
		//private constructor
	}
	
	@SafeVarargs
	public static <T> T coalesce(Supplier<T>... ts) {
        return Arrays.asList(ts)
            .stream()
            .map(Supplier<T>::get)
            .filter(Objects::nonNull)
            .findFirst()
            .orElse(null);
    }
	
	@SafeVarargs
	public static String coalesceToEmptyString(Supplier<String>... suppliers) {
		
		Supplier<String>[] supps = Arrays.copyOf(suppliers,  suppliers.length + 1);
		supps[supps.length - 1] = EMPTY_STRING;
		return coalesce(supps);
		
    }
	
	@SuppressWarnings("unchecked")
	@SafeVarargs 
	public static String coalesceToEmptyString(String... strings) {
		
		Supplier<String>[] supps = new Supplier[strings.length];
				
		for (int i = 0; i < strings.length; i++) {
			String string = strings[i];
			supps[i] = () -> string;
		}
		
		return coalesceToEmptyString(supps);
		
	}
	
	@SuppressWarnings("unchecked")
	@SafeVarargs 
	public static <T> T coalesce(T... args) {
		
		Supplier<T>[] supps = new Supplier[args.length];
				
		for (int i = 0; i < args.length; i++) {
			T arg = args[i];
			supps[i] = () -> arg;
		}
		
		return coalesce(supps);
		
	}
	
	@SafeVarargs
	public static Date coalesceToDefaultDate(Supplier<Date>... suppliers) {
		
		Supplier<Date>[] supps = Arrays.copyOf(suppliers,  suppliers.length + 1);
		supps[supps.length - 1] = UNIX_START;
		return coalesce(supps);
		
    }
	
	@SuppressWarnings("unchecked")
	@SafeVarargs 
	public static Date coalesceToDefaultDate(Date... dates) {
		
		Supplier<Date>[] supps = new Supplier[dates.length];
				
		for (int i = 0; i < dates.length; i++) {
			Date date = dates[i];
			supps[i] = () -> date;
		}
		
		return coalesceToDefaultDate(supps);
		
	}
	
	@SuppressWarnings("unchecked")
	@SafeVarargs
	public static <T> T coalesceToZero(Class<T> clazz, Supplier<T>... suppliers) {
				
		Supplier<T>[] supps = Arrays.copyOf(suppliers,  suppliers.length + 1);
		Supplier<T> zeroSupplier = null;
		
		if (Long.class.equals(clazz)) {
			zeroSupplier = () -> (T) Long.valueOf(0L);
		} else if (Integer.class.equals(clazz)) {
			zeroSupplier = () -> (T) Integer.valueOf(0);
		} else if (BigDecimal.class.equals(clazz)) {
			zeroSupplier = () -> (T) BigDecimal.ZERO;
		}
		
		supps[supps.length - 1] = zeroSupplier;
		return coalesce(supps);
		
	}
	
}

以上是关于java 适用于Java 8的NullHandlers的主要内容,如果未能解决你的问题,请参考以下文章

适用于 Java 8 的 Tomcat 8 Maven 插件

防止 Java 7/8 采样声音在播放时点击(适用于 6)

android面临崩溃报告仅适用于8.0和8.1中的firebase通知

Java 9 中的 Maven 更新,Eclipse 中的 Java 8 中的 Maven 编译

java8新特性→Optional:适用于层级处理非空判断(依赖上一步操作)的场合

java面试题-tcp和udp