Java tutorial
//package com.java2s; /* * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.Map; public class Main { /** * Make the log more meanings * * @param <K> * @param <V> * @param map * @return */ public static <K, V> String toLog(Map<K, V> map) { // using StringBuffer instead of String because expect there are many append operation StringBuffer sb = new StringBuffer(); if (map == null) { return null; } if (map.isEmpty()) { return map.toString(); } sb.append("{"); for (Iterator<K> iterator = map.keySet().iterator(); iterator.hasNext();) { K key = iterator.next(); Object value = map.get(key); sb.append(key).append("="); sb.append(toString4Log(value)); if (iterator.hasNext()) { sb.append(", "); } } sb.append("}"); return sb.toString(); } public static <E> String toLog(Collection<E> collec) { return toLog("", collec); } /** * Make the log more meanings * * @param <E> * @param collec * @return */ public static <E> String toLog(String separator, Collection<E> collec) { // using StringBuffer instead of String because expect there are many append operation StringBuffer sb = new StringBuffer(); if (collec == null) { return null; } if (collec.isEmpty()) { return collec.toString(); } sb.append("["); for (Iterator<E> iterator = collec.iterator(); iterator.hasNext();) { E value = iterator.next(); sb.append(separator).append(toString4Log(value)).append(separator); if (iterator.hasNext()) { sb.append(", "); } } sb.append("]"); return sb.toString(); } public static <E> String toLog(E... array) { return toLog("", array); } public static <E> String toLog(String separator, E... array) { if (array != null) return toLog(separator, Arrays.asList(array)); else return null; } /** * true if collection is null or empty. * * @param collection */ public static boolean isEmpty(Collection<?> collection) { return collection == null || collection.isEmpty(); } private static String toString4Log(Object value) { if (value == null) { return null; } else if (value instanceof String) { return (String) value; } else if (value instanceof Short) { return "" + ((Short) value).shortValue(); } else if (value instanceof Integer) { return "" + ((Integer) value).intValue(); } else if (value instanceof Long) { return "" + ((Long) value).longValue(); } else if (value instanceof Float) { return "" + ((Float) value).floatValue(); } else if (value instanceof Double) { return "" + ((Double) value).doubleValue(); } else if (value instanceof Object[]) { Object[] objs = (Object[]) value; StringBuffer sb = new StringBuffer(); sb.append("["); for (int i = 0; i < objs.length; i++) { sb.append(toString4Log(objs[i])); if (i != objs.length - 1) { sb.append(", "); } } sb.append("]"); return sb.toString(); } else if (value instanceof Iterator) { StringBuffer sb = new StringBuffer(); sb.append("["); for (Iterator<?> i = (Iterator<?>) value; i.hasNext();) { sb.append(toString4Log(i.next())); if (i.hasNext()) { sb.append(", "); } } sb.append("]"); return sb.toString(); } else return value.toString(); } }