Java Code Examples for com.jstarcraft.core.common.conversion.ConversionUtility#convert()

The following examples show how to use com.jstarcraft.core.common.conversion.ConversionUtility#convert() . 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: InputDefinition.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
/**
 * 将内容转换为消息体
 * 
 * @param inputValue
 * @return
 */
MessageBody getMessageBody(Map<Byte, ContentCodec> codecs, Object inputValue) {
    try {
        if (inputValue != null) {
            Class<?> clazz = inputValue.getClass();
            if (Collection.class.isAssignableFrom(clazz)) {
                Collection<?> inputValues = (Collection<?>) inputValue;
                return getMessageBody(codecs, inputValues.toArray());
            }
        }
        Object content = ConversionUtility.convert(inputValue, getContentType());
        MessageFormat format = MessageFormat.fromByte(contentFormat);
        ContentCodec codec = codecs.get(format.getMark());
        byte[] data = codec.encode(getContentType(), content);
        return MessageBody.instanceOf(MessageFormat.isZip(contentFormat), format, data);
    } catch (Exception exception) {
        throw new IllegalArgumentException(exception);
    }
}
 
Example 2
Source File: InputDefinition.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
/**
 * 根据指定消息与会话转化为输入值
 * 
 * @param message
 * @param session
 * @return
 */
Object[] getInputValues(Map<Byte, ContentCodec> codecs, CommunicationMessage message, CommunicationSession<?> session) {
    Type inputType = getInputType();
    MessageBody body = message.getBody();
    ContentCodec codec = codecs.get(body.getType().getMark());
    Object content = codec.decode(getContentType(), body.getContent());
    if (void.class.equals(inputType) && inputVariables.length == 0) {
        if (content != null) {
            throw new IllegalArgumentException();
        }
        return new Object[] {};
    }
    content = ConversionUtility.convert(content, inputType);
    Object[] values = new Object[inputVariables.length];
    for (int cursor = 0, size = inputVariables.length; cursor < size; cursor++) {
        values[cursor] = inputVariables[cursor].getValue(content, message, session);
    }
    return values;
}
 
Example 3
Source File: PropertyFormatAdapter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <E> Iterator<E> iterator(Class<E> clazz, InputStream stream) {
    // 实例列表
    HashMap<Object, E> instanceObjects = new HashMap<>();
    try {
        Properties properties = new Properties();
        properties.load(stream);
        Constructor<E> constructor = clazz.getDeclaredConstructor();
        ReflectionUtility.makeAccessible(constructor);

        Field storageId = ReflectionUtility.uniqueField(clazz, ResourceId.class);
        storageId.setAccessible(true);

        TreeMap<?, ?> keyValues = new TreeMap<>(properties);
        for (Entry<?, ?> keyValue : keyValues.entrySet()) {
            LinkedList<String> fieldNames = new LinkedList<>(Arrays.asList(String.class.cast(keyValue.getKey()).split(dot)));
            String fieldName = fieldNames.pollFirst();
            String fieldValue = String.class.cast(keyValue.getValue());
            Object instanceId = ConversionUtility.convert(fieldName, storageId.getGenericType());
            E instanceObject = instanceObjects.get(instanceId);
            if (instanceObject == null) {
                instanceObject = constructor.newInstance();
                storageId.set(instanceObject, instanceId);
                instanceObjects.put(instanceId, instanceObject);
            }
            if (fieldNames.isEmpty()) {
                continue;
            } else {
                fieldName = fieldNames.pollFirst();
                process(instanceObject, clazz, fieldName, fieldNames, fieldValue);
            }
        }
        return instanceObjects.values().iterator();
    } catch (Exception exception) {
        throw new StorageException("遍历Properties异常", exception);
    }
}
 
Example 4
Source File: OutputDefinition.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
/**
 * 根据指定消息与会话转化为输出值
 * 
 * @param message
 * @param session
 * @return
 */
Object getOutputValue(Map<Byte, ContentCodec> codecs, CommunicationMessage message, CommunicationSession<?> session) {
    Type outputType = getOutputType();
    MessageBody body = message.getBody();
    ContentCodec codec = codecs.get(body.getType().getMark());
    Object content = codec.decode(getContentType(), body.getContent());
    if (void.class.equals(outputType)) {
        if (content != null) {
            throw new IllegalArgumentException();
        }
        return null;
    }
    content = ConversionUtility.convert(content, outputType);
    return outputVariable.getValue(content, message, session);
}
 
Example 5
Source File: XlsxFormatAdapter.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
private void set(Object instance, String content) throws Exception {
    Object value = ConversionUtility.convert(content, field.getGenericType());
    field.set(instance, value);
}
 
Example 6
Source File: Neo4jAccessor.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
@Override
public <K extends Comparable, I, T extends IdentityObject<K>> Map<K, I> queryIdentities(Class<T> clazz, String name, StorageCondition<I> condition) {
	try {
		Neo4jMetadata metadata = metadatas.get(clazz);
		HashMap<String, Object> parameters = new HashMap<>();
		StringBuilder buffer = new StringBuilder(INDEX_2_ID_MAP_BEGIN);
		ConditionType type = condition.getType();
		I[] values = condition.getValues();
		switch (type) {
		case All:
			break;
		case Between:
			buffer.append(BETWEEN_CONDITION);
			break;
		case Equal:
			buffer.append(EQUAL_CONDITION);
			break;
		case Higher:
			buffer.append(HIGHER_CONDITION);
			break;
		case In:
			StringBuilder string = new StringBuilder();
			for (int index = 1, size = values.length - 1; index <= size; index++) {
				string.append(", {");
				string.append(index);
				string.append("}");
			}
			buffer.append(StringUtility.format(IN_CONDITION, name, string.toString()));
			break;
		case Lower:
			buffer.append(LOWER_CONDITION);
			break;
		case Unequal:
			buffer.append(UNEQUAL_CONDITION);
			break;
		}
		buffer.append(StringUtility.format(INDEX_2_ID_MAP_END, metadata.getPrimaryName(), name));
		String cql = buffer.toString();
		cql = StringUtility.format(cql, metadata.getOrmName(), name, name);
		for (int index = 0; index < values.length; index++) {
			parameters.put(String.valueOf(index), values[index]);
		}
		Iterable<Map<String, Object>> keyValues = template.query(cql, parameters);
		Map<K, I> map = new HashMap<>();
		for (Map<String, Object> keyValue : keyValues) {
			Object key = keyValue.get("key");
			Object value = keyValue.get("value");
			key = ConversionUtility.convert(key, metadata.getFields().get(metadata.getPrimaryName()));
			value = ConversionUtility.convert(value, metadata.getFields().get(name));
			map.put((K) key, (I) value);
		}
		return map;
	} finally {
		template.clear();
	}
}