com.feilong.formatter.AbstractFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* Copyright (C) 2008 feilong
*
* 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.
*/
package com.feilong.formatter;
import static com.feilong.core.Validator.isNullOrEmpty;
import static com.feilong.core.bean.ConvertUtil.toArray;
import static com.feilong.core.lang.StringUtil.EMPTY;
import static com.feilong.core.lang.StringUtil.SPACE;
import static com.feilong.core.util.CollectionsUtil.addIgnoreNullOrEmpty;
import static com.feilong.core.util.CollectionsUtil.first;
import static com.feilong.core.util.CollectionsUtil.getPropertyValueList;
import static com.feilong.core.util.CollectionsUtil.newArrayList;
import static com.feilong.core.util.SortUtil.sortMapByKeyAsc;
import static java.lang.Math.max;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.feilong.core.bean.ConvertUtil;
import com.feilong.core.bean.PropertyUtil;
import com.feilong.core.util.CollectionsUtil;
import com.feilong.formatter.builder.FormatterBuilder;
import com.feilong.formatter.builder.FormatterBuilderFactory;
import com.feilong.formatter.entity.BeanFormatterConfig;
import com.feilong.formatter.entity.FormatterColumnEntity;
import com.feilong.json.JsonUtil;
import com.feilong.lib.lang3.StringUtils;
/**
* Formatter相关父类.
*
* @author feilong
* @since 1.8.5
*/
abstract class AbstractFormatter{
/** The Constant log. */
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractFormatter.class);
//---------------------------------------------------------------
/**
* 对java bean
格式化.
*
* @param
* the generic type
* @param bean
* the bean
* @return 如果 bean
是null,返回 {@link StringUtils#EMPTY}
*/
public String format(T bean){
return isNullOrEmpty(bean) ? EMPTY : format(PropertyUtil.describe(bean));
}
/**
* 将map
格式化成字符串.
*
* @param
* the key type
* @param
* the value type
* @param map
* the map
* @return 如果 map
是null,返回 {@link StringUtils#EMPTY}
*/
public String format(Map map){
if (isNullOrEmpty(map)){
return EMPTY;
}
//---------------------------------------------------------------
//key 字符串最长的那个
int maxKeyLength = -1;
for (K key : map.keySet()){
maxKeyLength = max(maxKeyLength, StringUtils.length(ConvertUtil.toString(key)));
}
//---------------------------------------------------------------
List