Java Code Examples for com.jstarcraft.core.utility.StringUtility#isEmpty()

The following examples show how to use com.jstarcraft.core.utility.StringUtility#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<Object> readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String check = in.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    int length = Integer.valueOf(check);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    // 兼容UniMi
    type = TypeUtility.refineType(type, Collection.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    ClassDefinition definition = context.getClassDefinition(clazz);
    Collection<Object> collection = (Collection) definition.getInstance();
    Class<?> elementClazz = TypeUtility.getRawType(types[0], null);
    CsvConverter converter = context.getCsvConverter(Specification.getSpecification(elementClazz));
    for (int index = 0; index < length; index++) {
        Object element = converter.readValueFrom(context, types[0]);
        collection.add(element);
    }
    return collection;
}
 
Example 2
Source File: ObjectConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String check = in.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    int length = Integer.valueOf(check);
    // 处理对象类型
    Class<?> clazz = TypeUtility.getRawType(type, null);
    ClassDefinition definition = context.getClassDefinition(clazz);
    Object instance = definition.getInstance();
    for (PropertyDefinition property : definition.getProperties()) {
        CsvConverter converter = context.getCsvConverter(property.getSpecification());
        Object value = converter.readValueFrom(context, property.getType());
        property.setValue(instance, value);
    }
    return instance;
}
 
Example 3
Source File: MovieDataConfigurer.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
@Bean("movieItems")
List<MovieItem> getItems(DataSpace movieDataSpace) throws Exception {
    File movieItemFile = new File("data/ml-100k/u.item");
    List<MovieItem> items = new LinkedList<>();

    QualityAttribute<Integer> itemAttribute = movieDataSpace.getQualityAttribute("item");
    try (InputStream stream = new FileInputStream(movieItemFile); InputStreamReader reader = new InputStreamReader(stream, StringUtility.CHARSET); BufferedReader buffer = new BufferedReader(reader)) {
        try (CSVParser parser = new CSVParser(buffer, CSVFormat.newFormat('|'))) {
            Iterator<CSVRecord> iterator = parser.iterator();
            while (iterator.hasNext()) {
                CSVRecord datas = iterator.next();
                // 物品标识
                int id = Integer.parseInt(datas.get(0));
                // 物品索引
                int index = itemAttribute.convertData(id);
                // 物品标题
                String title = datas.get(1);
                // 物品日期
                LocalDate date = StringUtility.isEmpty(datas.get(2)) ? LocalDate.of(1970, 1, 1) : LocalDate.parse(datas.get(2), formatter);
                MovieItem item = new MovieItem(index, title, date);
                items.add(item);
            }
        }
    }

    items = new ArrayList<>(items);
    return items;
}
 
Example 4
Source File: TypeConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Type readValueFrom(CsvReader context, Type type) throws Exception {
    Iterator<String> in = context.getInputStream();
    String check = in.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    int code = Integer.valueOf(check);
    ClassDefinition definition = context.getClassDefinition(code);
    if (definition.getType() == Class.class) {
        code = Integer.valueOf(in.next());
        definition = context.getClassDefinition(code);
        return definition.getType();
    } else if (definition.getType() == GenericArrayType.class) {
        if (type == Class.class) {
            type = readValueFrom(context, type);
            Class<?> clazz = Class.class.cast(type);
            return Array.newInstance(clazz, 0).getClass();
        } else {
            type = readValueFrom(context, type);
            return TypeUtility.genericArrayType(type);
        }
    } else if (definition.getType() == ParameterizedType.class) {
        code = Integer.valueOf(in.next());
        definition = context.getClassDefinition(code);
        Integer length = Integer.valueOf(in.next());
        Type[] types = new Type[length];
        for (int index = 0; index < length; index++) {
            types[index] = readValueFrom(context, type);
        }
        return TypeUtility.parameterize(definition.getType(), types);
    } else {
        throw new CodecConvertionException();
    }
}
 
Example 5
Source File: NumberConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Number readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String element = in.next();
    if (StringUtility.isEmpty(element)) {
        return null;
    }
    Class<Number> clazz = (Class<Number>) TypeUtility.getRawType(type, null);
    return NumberUtility.convert(element, clazz);
}
 
Example 6
Source File: MapConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Map<Object, Object> readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String check = in.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    int length = Integer.valueOf(check);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    ClassDefinition definition = context.getClassDefinition(clazz);
    Map<Object, Object> map = (Map) definition.getInstance();
    Class<?> keyClazz = TypeUtility.getRawType(types[0], null);
    CsvConverter keyConverter = context.getCsvConverter(Specification.getSpecification(keyClazz));
    Class<?> valueClazz = TypeUtility.getRawType(types[1], null);
    CsvConverter valueConverter = context.getCsvConverter(Specification.getSpecification(valueClazz));
    for (int index = 0; index < length; index++) {
        Object key = keyConverter.readValueFrom(context, types[0]);
        Object element = valueConverter.readValueFrom(context, types[1]);
        map.put(key, element);
    }
    return map;
}
 
Example 7
Source File: EnumerationConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
    Iterator<String> in = context.getInputStream();
    String element = in.next();
    if (StringUtility.isEmpty(element)) {
        return null;
    }
    int index = Integer.valueOf(element);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    return clazz.getEnumConstants()[index];
}
 
Example 8
Source File: ArrayConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String check = in.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    int length = Integer.valueOf(check);
    Class<?> componentClass = null;
    Type componentType = null;
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
        componentType = genericArrayType.getGenericComponentType();
        componentClass = TypeUtility.getRawType(componentType, null);
    } else {
        Class<?> clazz = TypeUtility.getRawType(type, null);
        componentType = clazz.getComponentType();
        componentClass = clazz.getComponentType();
    }
    Object array = Array.newInstance(componentClass, length);
    Specification specification = Specification.getSpecification(componentClass);
    CsvConverter converter = context.getCsvConverter(specification);
    for (int index = 0; index < length; index++) {
        Object element = converter.readValueFrom(context, componentType);
        Array.set(array, index, element);
    }
    return array;
}
 
Example 9
Source File: StringConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
    Iterator<String> in = context.getInputStream();
    String element = in.next();
    if (StringUtility.isEmpty(element)) {
        return null;
    }
    element = element.substring(0, element.length() - 1);
    if (type == char.class || type == Character.class) {
        return element.charAt(0);
    } else {
        return element;
    }
}
 
Example 10
Source File: BooleanConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String element = in.next();
    if (StringUtility.isEmpty(element)) {
        return null;
    }
    if (type == AtomicBoolean.class) {
        return new AtomicBoolean(element.equals(TRUE));
    }
    return element.equals(TRUE);
}
 
Example 11
Source File: CsvUtility.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
private static Object readValue(Type type, Class<?> clazz, CsvInformation information, Iterator<String> input) throws IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException {
    String check = input.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    Constructor<?> constructor = information.getConstructor();
    Object object = constructor.newInstance();
    // TODO 此处代码需要优化
    HashMap<String, Type> types = new HashMap<>();
    TypeVariable<?>[] typeVariables = clazz.getTypeParameters();
    if (typeVariables.length > 0) {
        ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
        for (int index = 0; index < typeVariables.length; index++) {
            types.put(typeVariables[index].getName(), parameterizedType.getActualTypeArguments()[index]);
        }
    }
    for (Field field : information.getFields()) {
        Object value;
        type = field.getGenericType();
        if (type instanceof TypeVariable) {
            TypeVariable<?> typeVariable = TypeVariable.class.cast(type);
            value = readValue(types.get(typeVariable.getName()), input);
        } else {
            value = readValue(type, input);
        }
        field.set(object, value);
    }
    return object;
}