Java Code Examples for com.jstarcraft.core.common.reflection.TypeUtility

The following examples show how to use com.jstarcraft.core.common.reflection.TypeUtility. These examples are extracted from open source projects. 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 Project: jstarcraft-nlp   Source File: DetectionPattern.java    License: Apache License 2.0 6 votes vote down vote up
public static final Map<String, DetectionPattern> loadPatterns(InputStream stream) {
    try (DataInputStream buffer = new DataInputStream(stream)) {
        Map<String, DetectionPattern> detections = new HashMap<>();
        byte[] data = new byte[buffer.available()];
        buffer.readFully(data);
        String json = new String(data, StringUtility.CHARSET);
        // 兼容\x转义ASCII字符
        json = StringUtility.unescapeJava(json);
        Type type = TypeUtility.parameterize(LinkedHashMap.class, String.class, String.class);
        LinkedHashMap<String, String> regulations = JsonUtility.string2Object(json, type);

        for (Entry<String, String> scriptTerm : regulations.entrySet()) {
            String script = scriptTerm.getKey();
            String regulation = scriptTerm.getValue();
            Pattern pattern = Pattern.compile(regulation, Pattern.MULTILINE);
            DetectionPattern detection = new DetectionPattern(script, pattern);
            detections.put(script, detection);
        }
        return Collections.unmodifiableMap(detections);
    } catch (Exception exception) {
        throw new IllegalArgumentException(exception);
    }
}
 
Example 2
Source Project: jstarcraft-core   Source File: ArraySortConverter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneSort annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    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();
    }
    throw new StorageException();
}
 
Example 3
Source Project: jstarcraft-core   Source File: ObjectSortConverter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneSort annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);

    try {
        // TODO 此处需要代码重构
        for (Entry<Field, SortConverter> keyValue : context.getSortKeyValues(clazz).entrySet()) {
            // TODO 此处代码可以优反射次数.
            field = keyValue.getKey();
            SortConverter converter = keyValue.getValue();
            annotation = field.getAnnotation(LuceneSort.class);
            String name = field.getName();
            for (IndexableField indexable : converter.convert(context, path + "." + name, field, annotation, field.getGenericType(), field.get(data))) {
                indexables.add(indexable);
            }
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 4
Source Project: jstarcraft-core   Source File: ObjectIndexConverter.java    License: Apache License 2.0 6 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<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);

    try {
        // TODO 此处需要代码重构
        for (Entry<Field, IndexConverter> keyValue : context.getIndexKeyValues(clazz).entrySet()) {
            // TODO 此处代码可以优反射次数.
            field = keyValue.getKey();
            IndexConverter converter = keyValue.getValue();
            annotation = field.getAnnotation(LuceneIndex.class);
            String name = field.getName();
            for (IndexableField indexable : converter.convert(context, path + "." + name, field, annotation, field.getGenericType(), field.get(data))) {
                indexables.add(indexable);
            }
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 5
Source Project: jstarcraft-core   Source File: ObjectStoreConverter.java    License: Apache License 2.0 6 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<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);

    try {
        // TODO 此处需要代码重构
        for (Entry<Field, StoreConverter> keyValue : context.getStoreKeyValues(clazz).entrySet()) {
            // TODO 此处代码可以优反射次数.
            field = keyValue.getKey();
            StoreConverter converter = keyValue.getValue();
            annotation = field.getAnnotation(LuceneStore.class);
            String name = field.getName();
            type = field.getGenericType();
            Object data = field.get(instance);
            for (IndexableField indexable : converter.encode(context, path + "." + name, field, annotation, type, data).values()) {
                indexables.put(path + "." + name, indexable);
            }
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 6
Source Project: jstarcraft-core   Source File: ArrayStoreConverter.java    License: Apache License 2.0 6 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<>();
    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();
    }
    Specification specification = Specification.getSpecification(componentClass);
    StoreConverter converter = context.getStoreConverter(specification);
    int size = Array.getLength(instance);
    IndexableField indexable = new StoredField(path + ".size", size);
    indexables.put(path + ".size", indexable);
    for (int index = 0; index < size; index++) {
        Object element = Array.get(instance, index);
        indexables.putAll(converter.encode(context, path + "[" + index + "]", field, annotation, componentType, element));
    }
    return indexables;
}
 
Example 7
Source Project: jstarcraft-core   Source File: CollectionConverter.java    License: 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 8
Source Project: jstarcraft-core   Source File: CollectionConverter.java    License: 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 9
Source Project: jstarcraft-core   Source File: MapConverter.java    License: 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 10
Source Project: jstarcraft-core   Source File: InstantConverter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void writeValueTo(CsvWriter context, Type type, Object value) throws Exception {
    CSVPrinter out = context.getOutputStream();
    if (value == null) {
        out.print(StringUtility.EMPTY);
        return;
    }
    // 处理日期类型
    if (TypeUtility.isAssignable(type, Date.class)) {
        value = Date.class.cast(value).getTime();
        out.print(value);
        return;
    } else {
        value = Instant.class.cast(value).toEpochMilli();
        out.print(value);
        return;
    }
}
 
Example 11
Source Project: jstarcraft-core   Source File: ObjectConverter.java    License: 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 12
Source Project: jstarcraft-core   Source File: ObjectConverter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void writeValueTo(CsvWriter context, Type type, Object instance) throws Exception {
    // TODO 处理null
    CSVPrinter out = context.getOutputStream();
    if (instance == null) {
        out.print(StringUtility.EMPTY);
        return;
    }
    // 处理对象类型
    Class<?> clazz = TypeUtility.getRawType(type, null);
    ClassDefinition definition = context.getClassDefinition(clazz);
    int length = definition.getProperties().length;
    out.print(length);
    for (PropertyDefinition property : definition.getProperties()) {
        CsvConverter converter = context.getCsvConverter(property.getSpecification());
        Object value = property.getValue(instance);
        converter.writeValueTo(context, property.getType(), value);
    }
}
 
Example 13
Source Project: jstarcraft-core   Source File: JsonUtility.java    License: Apache License 2.0 6 votes vote down vote up
public static Type java2Type(JavaType java) {
    Type type = null;
    if (java.isArrayType()) {
        // 数组类型
        type = java.getRawClass();
    } else if (java.hasGenericTypes()) {
        // 泛型类型
        List<JavaType> javas = java.getBindings().getTypeParameters();
        Type[] generics = new Type[javas.size()];
        int index = 0;
        for (JavaType term : javas) {
            generics[index++] = java2Type(term);
        }
        Class<?> clazz = java.getRawClass();
        type = TypeUtility.parameterize(clazz, generics);
    } else {
        type = java.getRawClass();
    }
    return type;
}
 
Example 14
Source Project: jstarcraft-core   Source File: ClassUtilityTestCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    // 模拟类不在ClassLoader的情况
    String name = "com.jstarcraft.core.common.compilation.MockTask";
    String path = "MockTask.clazz";
    Map<String, byte[]> bytes = new HashMap<>();
    try (InputStream stream = ClassUtilityTestCase.class.getResourceAsStream(path); DataInputStream buffer = new DataInputStream(stream)) {
        byte[] data = new byte[buffer.available()];
        buffer.readFully(data);
        bytes.put(name, data);
        Class<?>[] classes = ClassUtility.bytes2Classes(bytes);
        Type type = TypeUtility.parameterize(classes[0], String.class);
        Callable<String> task = JsonUtility.string2Object("{\"value\":\"birdy\"}", type);
        Assert.assertEquals("birdy", task.call());
        Assert.assertEquals(bytes, ClassUtility.classes2Bytes(classes));
    }
}
 
Example 15
Source Project: jstarcraft-core   Source File: VariableDefinition.java    License: Apache License 2.0 6 votes vote down vote up
static VariableDefinition instanceOf(Type type, CommandVariable variable, Integer index) {
    Class<?> clazz = TypeUtility.getRawType(type, null);
    int modifier = clazz.getModifiers();
    if (!clazz.isPrimitive() && Modifier.isAbstract(modifier)) {
        throw new CommunicationDefinitionException("指令的参数与返回不能为抽象");
    }
    if (Modifier.isInterface(modifier)) {
        throw new CommunicationDefinitionException("指令的参数与返回不能为接口");
    }

    VariableDefinition instance = new VariableDefinition();
    instance.variable = variable;
    instance.position = index;
    instance.type = type;
    return instance;
}
 
Example 16
Source Project: jstarcraft-nlp   Source File: DetectionTrie.java    License: Apache License 2.0 5 votes vote down vote up
public static final Map<String, Set<DetectionTrie>> loadTries(InputStream stream) {
    try (DataInputStream buffer = new DataInputStream(stream)) {
        Map<String, Set<DetectionTrie>> detections = new HashMap<>();
        byte[] data = new byte[buffer.available()];
        buffer.readFully(data);
        String json = new String(data, StringUtility.CHARSET);
        // 兼容\x转义ASCII字符
        json = StringUtility.unescapeJava(json);
        Type type = TypeUtility.parameterize(LinkedHashMap.class, String.class, String.class);
        type = TypeUtility.parameterize(LinkedHashMap.class, String.class, LinkedHashMap.class);
        LinkedHashMap<String, LinkedHashMap<String, String>> dictionaries = JsonUtility.string2Object(json, type);

        for (Entry<String, LinkedHashMap<String, String>> scriptTerm : dictionaries.entrySet()) {
            Set<DetectionTrie> tries = new HashSet<>();
            String script = scriptTerm.getKey();
            LinkedHashMap<String, String> languages = scriptTerm.getValue();
            for (Entry<String, String> languageTerm : languages.entrySet()) {
                String language = languageTerm.getKey();
                String[] dictionary = languageTerm.getValue().split("\\|");
                int weight = 0;
                TreeMap<String, Integer> tree = new TreeMap<>();
                for (String word : dictionary) {
                    tree.put(word, weight++);
                }
                DoubleArrayTrie<Integer> trie = new DoubleArrayTrie<>(tree);
                DetectionTrie detection = new DetectionTrie(language, trie);
                tries.add(detection);
            }
            detections.put(script, Collections.unmodifiableSet(tries));
        }
        return Collections.unmodifiableMap(detections);
    } catch (Exception exception) {
        throw new IllegalArgumentException(exception);
    }
}
 
Example 17
Source Project: jstarcraft-rns   Source File: ConfigurationTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigurationJson() {
    LinkedList<KeyValue<String, Class<?>>> left = new LinkedList<>();
    left.add(new KeyValue<>("user", String.class));
    left.add(new KeyValue<>("item", int.class));
    left.add(new KeyValue<>("score", double.class));
    left.add(new KeyValue<>("word", String.class));
    String json = JsonUtility.object2String(left);
    Type type = TypeUtility.parameterize(KeyValue.class, String.class, Class.class);
    type = TypeUtility.parameterize(LinkedList.class, type);
    LinkedList<KeyValue<String, Class<?>>> right = JsonUtility.string2Object(json, type);
    assertEquals(left, right);
}
 
Example 18
Source Project: jstarcraft-core   Source File: JsonFormatAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <E> Iterator<E> iterator(Class<E> clazz, InputStream stream) {
    try {
        JavaType type = JsonUtility.type2Java(TypeUtility.parameterize(LinkedList.class, clazz));
        List<E> list = TYPE_CONVERTER.readValue(stream, type);
        return list.iterator();
    } catch (Exception exception) {
        throw new StorageException("遍历JSON异常", exception);
    }
}
 
Example 19
Source Project: jstarcraft-core   Source File: YamlFormatAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <E> Iterator<E> iterator(Class<E> clazz, InputStream stream) {
    try {
        JavaType type = JsonUtility.type2Java(TypeUtility.parameterize(LinkedList.class, clazz));
        List<E> list = TYPE_CONVERTER.readValue(stream, type);
        return list.iterator();
    } catch (Exception exception) {
        throw new StorageException("遍历YAML异常", exception);
    }
}
 
Example 20
Source Project: jstarcraft-core   Source File: NumberSortConverter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneSort annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    if (Byte.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, (byte) data));
        return indexables;
    }
    if (Short.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, (short) data));
        return indexables;
    }
    if (Integer.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, (int) data));
        return indexables;
    }
    if (Long.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, (long) data));
        return indexables;
    }
    if (Float.class.isAssignableFrom(clazz)) {
        indexables.add(new FloatDocValuesField(path, (float) data));
        return indexables;
    }
    if (Double.class.isAssignableFrom(clazz)) {
        indexables.add(new DoubleDocValuesField(path, (double) data));
        return indexables;
    }
    throw new StorageException();
}
 
Example 21
Source Project: jstarcraft-core   Source File: BooleanSortConverter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneSort annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    if (AtomicBoolean.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, AtomicBoolean.class.cast(data).get() ? 1L : 0L));
        return indexables;
    }
    if (Boolean.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, Boolean.class.cast(data) ? 1L : 0L));
        return indexables;
    }
    throw new StorageException();
}
 
Example 22
Source Project: jstarcraft-core   Source File: NumberIndexConverter.java    License: 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<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    if (Byte.class.isAssignableFrom(clazz)) {
        indexables.add(new IntPoint(path, (byte) data));
        return indexables;
    }
    if (Short.class.isAssignableFrom(clazz)) {
        indexables.add(new IntPoint(path, (short) data));
        return indexables;
    }
    if (Integer.class.isAssignableFrom(clazz)) {
        indexables.add(new IntPoint(path, (int) data));
        return indexables;
    }
    if (Long.class.isAssignableFrom(clazz)) {
        indexables.add(new LongPoint(path, (long) data));
        return indexables;
    }
    if (Float.class.isAssignableFrom(clazz)) {
        indexables.add(new FloatPoint(path, (float) data));
        return indexables;
    }
    if (Double.class.isAssignableFrom(clazz)) {
        indexables.add(new DoublePoint(path, (double) data));
        return indexables;
    }
    throw new StorageException();
}
 
Example 23
Source Project: jstarcraft-core   Source File: BooleanIndexConverter.java    License: 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<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    if (AtomicBoolean.class.isAssignableFrom(clazz)) {
        indexables.add(new IntPoint(path, AtomicBoolean.class.cast(data).get() ? 1 : 0));
        return indexables;
    }
    if (Boolean.class.isAssignableFrom(clazz)) {
        indexables.add(new IntPoint(path, Boolean.class.cast(data) ? 1 : 0));
        return indexables;
    }
    throw new StorageException();
}
 
Example 24
Source Project: jstarcraft-core   Source File: MapIndexConverter.java    License: 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 25
Source Project: jstarcraft-core   Source File: MapStoreConverter.java    License: 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 26
Source Project: jstarcraft-core   Source File: MapStoreConverter.java    License: 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 27
Source Project: jstarcraft-core   Source File: EnumerationStoreConverter.java    License: 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);
    IndexableField indexable = indexables.firstEntry().getValue();
    Class clazz = TypeUtility.getRawType(type, null);
    return Enum.valueOf(clazz, indexable.stringValue());
}
 
Example 28
Source Project: jstarcraft-core   Source File: NumberStoreConverter.java    License: 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);
    IndexableField indexable = indexables.firstEntry().getValue();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    Number number = indexable.numericValue();
    if (Byte.class.isAssignableFrom(clazz)) {
        return number.byteValue();
    }
    if (Short.class.isAssignableFrom(clazz)) {
        return number.shortValue();
    }
    if (Integer.class.isAssignableFrom(clazz)) {
        return number.intValue();
    }
    if (Long.class.isAssignableFrom(clazz)) {
        return number.longValue();
    }
    if (Float.class.isAssignableFrom(clazz)) {
        return number.floatValue();
    }
    if (Double.class.isAssignableFrom(clazz)) {
        return number.doubleValue();
    }
    throw new StorageException();
}
 
Example 29
Source Project: jstarcraft-core   Source File: NumberStoreConverter.java    License: 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<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    if (Byte.class.isAssignableFrom(clazz)) {
        indexables.put(path, new StoredField(path, (byte) instance));
        return indexables;
    }
    if (Short.class.isAssignableFrom(clazz)) {
        indexables.put(path, new StoredField(path, (short) instance));
        return indexables;
    }
    if (Integer.class.isAssignableFrom(clazz)) {
        indexables.put(path, new StoredField(path, (int) instance));
        return indexables;
    }
    if (Long.class.isAssignableFrom(clazz)) {
        indexables.put(path, new StoredField(path, (long) instance));
        return indexables;
    }
    if (Float.class.isAssignableFrom(clazz)) {
        indexables.put(path, new StoredField(path, (float) instance));
        return indexables;
    }
    if (Double.class.isAssignableFrom(clazz)) {
        indexables.put(path, new StoredField(path, (double) instance));
        return indexables;
    }
    throw new StorageException();
}
 
Example 30
Source Project: jstarcraft-core   Source File: ObjectStoreConverter.java    License: 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);

    try {
        // TODO 此处需要代码重构
        Object instance = context.getInstance(clazz);
        for (Entry<Field, StoreConverter> keyValue : context.getStoreKeyValues(clazz).entrySet()) {
            // TODO 此处代码可以优反射次数.
            field = keyValue.getKey();
            StoreConverter converter = keyValue.getValue();
            annotation = field.getAnnotation(LuceneStore.class);
            String name = field.getName();
            type = field.getGenericType();
            Object data = converter.decode(context, path + "." + name, field, annotation, type, indexables);
            field.set(instance, data);
        }
        return instance;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}