All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.datahub.client.util.FormatUtils Maven / Gradle / Ivy

The newest version!
package com.aliyun.datahub.client.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.aliyun.datahub.client.exception.InvalidParameterException;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class FormatUtils {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    // date format
    public static Date parseDateNoTime(String sDate) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        if (sDate != null && sDate.length() >= "yyyyMMdd".length()) {
            if (!StringUtils.isNumeric(sDate)) {
                throw new ParseException("not all digit", 0);
            } else {
                return dateFormat.parse(sDate);
            }
        } else {
            throw new ParseException("length too little", 0);
        }
    }

    public static Date parseDateNoTime(String sDate, String format) throws ParseException {
        if (StringUtils.isBlank(format)) {
            throw new ParseException("null format.", 0);
        } else {
            DateFormat dateFormat = new SimpleDateFormat(format);
            if (sDate != null && sDate.length() >= format.length()) {
                return dateFormat.parse(sDate);
            } else {
                throw new ParseException("length too little.", 0);
            }
        }
    }


    // check parameter format
    public static void checkProjectName(String projectName, boolean strongValidation) {
        if (!isNameValid(projectName, 3, 32, strongValidation)) {
            throw new InvalidParameterException("ProjectName format is invalid");
        }
    }

    public static void checkProjectName(String projectName) {
        checkProjectName(projectName, false);
    }

    public static void checkTopicName(String topicName, boolean strongValidation) {
        if (!isNameValid(topicName, 1, 128, strongValidation)) {
            throw new InvalidParameterException("TopicName format is invalid");
        }
    }

    public static void checkTopicName(String topicName) {
        checkTopicName(topicName, false);
    }

    public static void checkKafkaGroupName(String groupName) {
        if (!isNameValid(groupName, 3, 128, true)) {
            throw new InvalidParameterException("GroupName format is invalid");
        }
    }

    public static void checkShardId(String shardId) {
        if (!StringUtils.isNumeric(shardId)) {
            throw new InvalidParameterException("ShardId format is invalid");
        }
    }

    public static void checkComment(String comment) {
        if (comment == null || /* (comment.length() < 1 ||*/ comment.length() > 1024) {
            throw new InvalidParameterException("Comment format is invalid");
        }
    }

    public static void checkDay(String day) {
        if (day == null || day.isEmpty() || day.length() != 8) {
            throw new InvalidParameterException("Day format is invalid");
        }
    }

    private static boolean isNameValid(String name, int minLen, int maxLen, boolean strongValidation) {
        if (name == null || (name.length() < minLen || name.length() > maxLen)) {
            return false;
        }

        for (int i = 0; i < name.length(); ++i) {
            char c = name.charAt(i);
            if (i == 0) {
                if (!Character.isAlphabetic(c) && strongValidation) {
                    return false;
                }
            } else {
                if (!Character.isAlphabetic(c) && !Character.isDigit(c) && c != '_') {
                    return false;
                }
            }
        }

        return true;
    }

    // check field value
    private static final long MIN_TIMESTAMP = -62135798400000000L; // 0000-00-00 00:00:00
    private static final long MAX_TIMESTAMP = 253402271999000000L; // 9999-12-31 23:59:59
    public static boolean checkString(Object value) {
        return (value instanceof String);
    }

    public static boolean checkTinyInt(Object value) {
        return (value instanceof Byte) ||
                (value instanceof Integer && (Integer) value <= Byte.MAX_VALUE && (Integer) value >= Byte.MIN_VALUE) ||
                (value instanceof Long && (Long) value <= Byte.MAX_VALUE && (Long) value >= Byte.MIN_VALUE);
    }

    public static boolean checkSmallInt(Object value) {
        return (value instanceof Short) ||
                (value instanceof Integer && (Integer) value <= Short.MAX_VALUE && (Integer) value >= Short.MIN_VALUE) ||
                (value instanceof Long && (Long) value <= Short.MAX_VALUE && (Long) value >= Short.MIN_VALUE);
    }

    public static boolean checkInteger(Object value) {
        return (value instanceof Integer) ||
                (value instanceof Long && (Long) value <= Integer.MAX_VALUE && (Long) value >= Integer.MIN_VALUE);
    }

    public static boolean checkBigint(Object value) {
        return ((value instanceof Long) || (value instanceof Integer));
    }

    public static boolean checkFloat(Object value) {
        return (value instanceof Float) || (value instanceof Double);
    }

    public static boolean checkDouble(Object value) {
        return (value instanceof Double);
    }

    public static boolean checkBoolean(Object value) {
        return (value instanceof Boolean);
    }

    public static boolean checkTimestamp(Object value) {
        boolean valid = checkBigint(value);
        if (valid) {
            // add timestamp range check
            long ts = (Long) value;
            if (ts < MIN_TIMESTAMP || ts > MAX_TIMESTAMP) {
                throw new InvalidParameterException("Timestamp range error. min:" + MIN_TIMESTAMP + ", max:" + MAX_TIMESTAMP);
            }
        }
        return valid;
    }

    public static boolean checkDecimal(Object value) {
        return (value instanceof BigDecimal);
    }

    public static boolean checkJson(Object value) {
        try {
            OBJECT_MAPPER.readTree((String) value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy