jp.co.nemuzuka.utils.ConvertUtils.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.nemuzuka.utils.ConvertUtils.java

Source

/*
 * Copyright 2012 Kazumune Katagiri. (http://d.hatena.ne.jp/nemuzuka)
 *
 * 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 jp.co.nemuzuka.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

/**
 * ??.
 * @author k-katagiri
 */
public class ConvertUtils {

    /**
     * String?Long??.
     * @param targets ??
     * @return ?Long?(???null???0??)
     */
    public static Long[] convert(String[] targets) {
        if (targets == null) {
            return new Long[0];
        }

        List<Long> list = new ArrayList<Long>();
        for (String target : targets) {
            list.add(Long.valueOf(target));
        }
        return list.toArray(new Long[0]);
    }

    /**
     * Long?String??.
     * @param targets ??
     * @return ?String?(???null???0??)
     */
    public static String[] convert(Long[] targets) {
        if (targets == null) {
            return new String[0];
        }

        List<String> list = new ArrayList<String>();
        for (Long target : targets) {
            list.add(target.toString());
        }
        return list.toArray(new String[0]);
    }

    /**
     * Date?.
     * @param source 
     * @param sdf ?
     * @return Date
     */
    public static Date toDate(String source, SimpleDateFormat sdf) {

        if (StringUtils.isEmpty(source)) {
            return null;
        }

        try {
            return sdf.parse(source);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Long?.
     * @param source 
     * @return Long
     */
    public static Long toLong(String source) {
        if (StringUtils.isEmpty(source)) {
            return null;
        }
        return Long.valueOf(source);
    }

    /**
     * Integer?.
     * @param source 
     * @return Integer
     */
    public static Integer toInteger(String source) {
        if (StringUtils.isEmpty(source)) {
            return null;
        }
        return Integer.valueOf(source);
    }

    /**
     * StringBigdecimal?.
     * @param source 
     * @return BigDecimal
     */
    public static BigDecimal toBigDecimal(String source) {
        if (StringUtils.isEmpty(source)) {
            return null;
        }
        return new BigDecimal(source);
    }

    /**
     * IntegerString?.
     * ??null???????
     * @param val ?
     * @return ?
     */
    public static String toString(Integer val) {
        if (val == null) {
            return "";
        }
        return String.valueOf(val);
    }

    /**
     * Date?.
     * ??null???????
     * @param date ?
     * @param sdf 
     * @return 
     */
    public static String toString(Date date, SimpleDateFormat sdf) {
        if (date == null) {
            return "";
        }
        return sdf.format(date);
    }

    /**
     * LongString?.
     * ??null???????
     * @param val ?
     * @return ?
     */
    public static String toString(Long val) {
        if (val == null) {
            return "";
        }
        return String.valueOf(val);
    }

    /**
     * NumberString?.
     * ??null???????
     * @param val ?
     * @param df 
     * @return 
     */
    public static String toString(Number val, DecimalFormat df) {
        if (val == null) {
            return "";
        }
        return df.format(val);
    }

    /**
     * StringString??.
     * ??String??????
     * ???????????
     * @param target ?
     * @return ??
     */
    public static String[] toStringArray(String target) {
        String name = target;
        if (name == null) {
            name = "";
        }
        name = name.replaceAll("\r\n", "\n");
        name = name.replaceAll("\r", "\n");
        String[] names = name.split("\n");
        return createTrimStrArray(names);
    }

    /**
     * StringString??.
     * ??String??????
     * ???????????
     * @param s ?
     * @param splitStr 
     * @return ??
     */
    public static String[] toStringArray(String s, String splitStr) {

        String target = s;
        if (target == null) {
            target = "";
        }
        String[] array = target.split(splitStr);
        return createTrimStrArray(array);
    }

    /**
     * String???.
     * ??SP?
     * 
     * ?
     * ???String?????
     * @param array ?String?
     * @return ?String?
     */
    private static String[] createTrimStrArray(String[] array) {
        Set<String> strSet = new LinkedHashSet<String>();

        for (String value : array) {
            String target = StringUtils.trimToEmpty(value);
            if (StringUtils.isEmpty(target)) {
                continue;
            }
            strSet.add(target);
        }
        return strSet.toArray(new String[0]);
    }
}