package net.waltzstudio.base.framework.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class SyntaxSugar {
	@SuppressWarnings("rawtypes")
	public static class M extends HashMap {
		private static final long serialVersionUID = 1L;
		@SuppressWarnings("unchecked")
		public M $(Object key, Object value) {
			put(key, value);
			return this;
		}
	}

	public static String S(Object... items) {
		if (items == null) {
			return "";
		}
		StringBuilder buf = new StringBuilder();
		for (Object item : items) {
			if (item != null) {
				buf.append(item);
			}
		}
		return buf.toString();
	}

	public static String SB(boolean trimming, String delim, Object... items) {
		if (items == null) {
			return "";
		}
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < items.length; i++) {
			if (i != 0) {
				buf.append(delim);
			}
			if (items[i] != null) {
				buf.append(trimming ? items[i].toString().trim() : items[i]);
			}
		}
		return buf.toString();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List L(Object... items) {
		List list = new ArrayList();
		if (items == null) {
			return list;
		}
		for (Object item : items) {
			list.add(item);
		}
		return list;
	}

	public static M M() {
		return new M();
	}
	public static M M(Object key, Object value) {
		return new M().$(key, value);
	}

	public static Date D() {
		return D(new Date());
	}
	public static Date D(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}
	public static Date D(int year, int month, int date) {
		Calendar c = Calendar.getInstance();
		c.set(year, month, date, 0, 0, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	public static Date T() {
		return T(new Date());
	}
	public static Date T(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.YEAR, 1970);
		c.set(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}
	public static Date T(int hour, int minute, int second) {
		Calendar c = Calendar.getInstance();
		c.set(1970, 0, 1, hour, minute, second);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	public static Date DT() {
		return DT(new Date());
	}
	public static Date DT(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}
	public static Date DT(int year, int month, int date, int hour, int minute, int second) {
		Calendar c = Calendar.getInstance();
		c.set(year, month, date, hour, minute, second);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	public static Calendar C() {
		return C(new Date());
	}
	public static Calendar C(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.MILLISECOND, 0);
		return c;
	}
	public static Calendar C(int year, int month, int date) {
		return C(year, month, date, 0, 0, 0);
	}
	public static Calendar C(int year, int month, int date, int hour, int minute, int second) {
		Calendar c = Calendar.getInstance();
		c.set(year, month, date, hour, minute, second);
		c.set(Calendar.MILLISECOND, 0);
		return c;
	}

}
