
cn.thinkingdata.tga.javasdk.util.TAPropertyUtil Maven / Gradle / Ivy
package cn.thinkingdata.tga.javasdk.util;
import cn.thinkingdata.tga.javasdk.exception.InvalidArgumentException;
import org.apache.http.util.TextUtils;
import java.util.Map;
import static cn.thinkingdata.tga.javasdk.TAConstData.*;
public class TAPropertyUtil {
/**
* check properties
* @param properties properties
* @param type event type
* */
public static void assertProperties(final Map properties,final DataType type) throws InvalidArgumentException {
if (properties.size() == 0) {
return;
}
for (Map.Entry property : properties.entrySet()) {
Object value = property.getValue();
if (null == value) {
continue;
}
if (! isValidKey(property.getKey())) {
throw new InvalidArgumentException("Invalid key format: " + property.getKey());
}
if(type == DataType.USER_ADD)
{
if ( ! (value instanceof Number))
{
throw new InvalidArgumentException("Only Number is allowed,Invalid property value " + value);
}
}
}
}
/**
* check property name
* @param key key
* */
public static boolean isValidKey(String key)
{
if(TextUtils.isEmpty(key))
{
return false;
}
return KEY_PATTERN.matcher(key).matches();
}
/**
* combine properties by sort. The latter has the highest priority
* @param desProperties target
* @param sourceProperties source
* */
@SafeVarargs
public static void mergeProperties(Map desProperties,Map ... sourceProperties) throws InvalidArgumentException {
for(Map sourceProperty:sourceProperties)
{
if(sourceProperty != null)
{
desProperties.putAll(sourceProperty);
}
}
}
/**
* Move special properties
* @param data target
* @param properties source
* @param propertyKeys properties key
* */
public static void moveProperty(Map data,Map properties,String... propertyKeys)
{
for (String propertyKey:propertyKeys)
{
if(properties.containsKey(propertyKey))
{
data.put(propertyKey, properties.get(propertyKey));
properties.remove(propertyKey);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy