实用工具类代码块

package com.company.peroject.moduel.util;

import com.google.common.collect.Lists;
import com.ibatis.common.beans.Probe;
import com.ibatis.common.beans.ProbeFactory;
import jodd.datetime.JDateTime;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by chaowen on 2017/8/13.
*/
public class CommonUtil {

public static final Collection NULL_COLLECTION = new NullCollection();

private static final Probe PROBE = ProbeFactory.getProbe();

/**
 * @author chaowen
 *
 *         
 * 批量获取map中的值
 * 
* * @param * @param * @param keys * @param source * @return */ public static List getAllFormMap(List keys, Map source) { List values = Collections.emptyList(); if (keys != null && !keys.isEmpty() && source != null && !source.isEmpty()) { values = new ArrayList(); for (K k : keys) { values.add(source.get(k)); } } return values; } /** * @author chaowen * *
 * 从List中获取valueProp组成一个新的list
 * 
* * @param * @param list * @param valueProp * @return */ public static List getValueList(List list, String valueProp) { List valueList = Collections.emptyList(); if (CollectionUtils.isNotEmpty(list)) { list.removeAll(nullCollection()); valueList = new ArrayList(list.size()); for (E e : list) { @SuppressWarnings("unchecked") K value = (K) PROBE.getObject(e, valueProp); valueList.add(value); } } return valueList; } /** * @author chaowen * *
 * 用list生成一个map,keyProp 为指定的key,valueProp 为指定是value
 * 
* * @param * @param * @param * @param list * @param keyProp * @param valueProp * @return */ @SuppressWarnings("unchecked") public static Map listforMap(List list, String keyProp, String valueProp) { Map rs = new HashMap<>(); if (CollectionUtils.isNotEmpty(list)) { ConcurrentHashMap map = new ConcurrentHashMap(); list.removeAll(nullCollection()); for (E object : list) { K key = (K) PROBE.getObject(object, keyProp); Object value = null; if (StringUtils.isEmpty(valueProp)) { value = object; } else { value = PROBE.getObject(object, valueProp); } map.put(key, (V) value); } rs = map; } return rs; } /** * @author chaowen * *
 * list 生成一个Map>
 * 
* * @param * @param * @param * @param list * @param keyProp * @param valueProp * @return */ @SuppressWarnings("unchecked") public static Map> listforListMap(List list, String keyProp, String valueProp) { Map> map = Collections.emptyMap(); if (CollectionUtils.isNotEmpty(list)) { list.removeAll(nullCollection()); map = new HashMap>(list.size()); V value = null; for (E object : list) { K key = (K) PROBE.getObject(object, keyProp); if (StringUtils.isEmpty(valueProp)) { value = (V) object; } else { value = (V) PROBE.getObject(object, valueProp); } List values = map.get(key); if (values == null) { values = new ArrayList(); } values.add(value); map.put(key, values); } } return map; } @SuppressWarnings("unchecked") public static final Collection nullCollection() { return (List) NULL_COLLECTION; } public static String getId(){ return UUID.randomUUID().toString().replace("-",""); } public static List stringToList(String in){ String[] ids = in.split(","); return Lists.newArrayList(ids); } public static void main(String[] args) { JDateTime jDateTime = new JDateTime(new Date()); System.out.println(jDateTime.toString()); jDateTime.setDay(1); jDateTime.setHour(0); jDateTime.setMinute(0); jDateTime.setSecond(0); System.out.println(jDateTime.toString()); jDateTime.addMonth(1); System.out.println(jDateTime.toString()); }

}

package com.company.peroject.moduel.util;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.RandomAccess;

public class NullCollection extends AbstractList
implements RandomAccess, Serializable {

private static final long serialVersionUID = 5206887786441397812L;

@Override
public Object get(int index) {
    return null;
}

@Override
public int size() {
    return 1;
}

public boolean contains(Object obj) {
    return null == obj;
}

private Object readResolve() {
    return null;
}

}

很实用的工具类

你可能感兴趣的:(代码备忘)