Java Code Examples for com.jstarcraft.core.common.reflection.TypeUtility#refineType()

The following examples show how to use com.jstarcraft.core.common.reflection.TypeUtility#refineType() . 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: CollectionConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public void writeValueTo(CsvWriter context, Type type, Collection<Object> value) throws Exception {
    // TODO 处理null
    CSVPrinter out = context.getOutputStream();
    if (value == null) {
        out.print(StringUtility.EMPTY);
        return;
    }
    // 兼容UniMi
    type = TypeUtility.refineType(type, Collection.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Collection<?> collection = Collection.class.cast(value);
    out.print(collection.size());
    Class<?> elementClazz = TypeUtility.getRawType(types[0], null);
    CsvConverter converter = context.getCsvConverter(Specification.getSpecification(elementClazz));
    for (Object element : collection) {
        converter.writeValueTo(context, types[0], element);
    }
    return;
}
 
Example 3
Source File: MapConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public void writeValueTo(CsvWriter context, Type type, Map<Object, Object> value) throws Exception {
    CSVPrinter out = context.getOutputStream();
    if (value == null) {
        out.print(StringUtility.EMPTY);
        return;
    }
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Map<Object, Object> map = Map.class.cast(value);
    out.print(map.size());
    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 (Entry<Object, Object> keyValue : map.entrySet()) {
        Object key = keyValue.getKey();
        keyConverter.writeValueTo(context, types[0], key);
        Object element = keyValue.getValue();
        valueConverter.writeValueTo(context, types[1], element);
    }
    return;
}
 
Example 4
Source File: MapIndexConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneIndex annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type keyType = types[0];
    Class<?> keyClazz = TypeUtility.getRawType(keyType, null);
    Type valueType = types[1];
    Class<?> valueClazz = TypeUtility.getRawType(valueType, null);

    try {
        // TODO 此处需要代码重构
        Map<Object, Object> map = Map.class.cast(data);
        Specification keySpecification = Specification.getSpecification(keyClazz);
        IndexConverter keyConverter = context.getIndexConverter(keySpecification);
        Specification valueSpecification = Specification.getSpecification(valueClazz);
        IndexConverter valueConverter = context.getIndexConverter(valueSpecification);

        // 只索引Key,不索引Value
        return context.getIndexConverter(Specification.COLLECTION).convert(context, path, field, annotation, TypeUtility.parameterize(Collection.class, keyType), map.keySet());
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 5
Source File: MapStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type keyType = types[0];
    Class<?> keyClazz = TypeUtility.getRawType(keyType, null);
    Type valueType = types[1];
    Class<?> valueClazz = TypeUtility.getRawType(valueType, null);

    try {
        // TODO 此处需要代码重构
        Map<Object, Object> map = (Map) context.getInstance(clazz);
        Specification keySpecification = Specification.getSpecification(keyClazz);
        StoreConverter keyConverter = context.getStoreConverter(keySpecification);
        Specification valueSpecification = Specification.getSpecification(valueClazz);
        StoreConverter valueConverter = context.getStoreConverter(valueSpecification);

        IndexableField indexable = indexables.get(path + ".size");
        int size = indexable.numericValue().intValue();
        for (int index = 0; index < size; index++) {
            Object key = keyConverter.decode(context, path + "[" + index + "_key]", field, annotation, keyType, indexables);
            Object value = valueConverter.decode(context, path + "[" + index + "_value]", field, annotation, valueType, indexables);
            map.put(key, value);
        }
        return map;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 6
Source File: MapStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
    NavigableMap<String, IndexableField> indexables = new TreeMap<>();
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type keyType = types[0];
    Class<?> keyClazz = TypeUtility.getRawType(keyType, null);
    Type valueType = types[1];
    Class<?> valueClazz = TypeUtility.getRawType(valueType, null);

    try {
        // TODO 此处需要代码重构
        Map<Object, Object> map = Map.class.cast(instance);
        Specification keySpecification = Specification.getSpecification(keyClazz);
        StoreConverter keyConverter = context.getStoreConverter(keySpecification);
        Specification valueSpecification = Specification.getSpecification(valueClazz);
        StoreConverter valueConverter = context.getStoreConverter(valueSpecification);

        int size = map.size();
        IndexableField indexable = new StoredField(path + ".size", size);
        indexables.put(path + ".size", indexable);
        int index = 0;
        for (Entry<Object, Object> keyValue : map.entrySet()) {
            Object key = keyValue.getKey();
            indexables.putAll(keyConverter.encode(context, path + "[" + index + "_key]", field, annotation, keyType, key));
            Object value = keyValue.getValue();
            indexables.putAll(valueConverter.encode(context, path + "[" + index + "_value]", field, annotation, valueType, value));
            index++;
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 7
Source File: CollectionStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    // 兼容UniMi
    type = TypeUtility.refineType(type, Collection.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type elementType = types[0];
    Class<?> elementClazz = TypeUtility.getRawType(elementType, null);

    try {
        // TODO 此处需要代码重构
        Collection<Object> collection = (Collection) context.getInstance(clazz);
        Specification specification = Specification.getSpecification(elementClazz);
        StoreConverter converter = context.getStoreConverter(specification);

        IndexableField indexable = indexables.get(path + ".size");
        int size = indexable.numericValue().intValue();
        for (int index = 0; index < size; index++) {
            Object element = converter.decode(context, path + "[" + index + "]", field, annotation, elementType, indexables);
            collection.add(element);
        }
        return collection;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 8
Source File: CollectionStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
    NavigableMap<String, IndexableField> indexables = new TreeMap<>();
    // 兼容UniMi
    type = TypeUtility.refineType(type, Collection.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type elementType = types[0];
    Class<?> elementClazz = TypeUtility.getRawType(elementType, null);

    try {
        // TODO 此处需要代码重构
        Collection<?> collection = Collection.class.cast(instance);
        Specification specification = Specification.getSpecification(elementClazz);
        StoreConverter converter = context.getStoreConverter(specification);

        int size = collection.size();
        IndexableField indexable = new StoredField(path + ".size", size);
        indexables.put(path + ".size", indexable);
        int index = 0;
        for (Object element : collection) {
            indexables.putAll(converter.encode(context, path + "[" + index + "]", field, annotation, elementType, element));
            index++;
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 9
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;
}