com.jstarcraft.core.utility.StringUtility Java Examples

The following examples show how to use com.jstarcraft.core.utility.StringUtility. 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: MqttTopicEventChannel.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public void registerMonitor(Set<Class> types, EventMonitor monitor) {
    try {
        for (Class type : types) {
            EventManager manager = managers.get(type);
            if (manager == null) {
                manager = new EventManager();
                managers.put(type, manager);
                address2Classes.put(type.getName(), type);
                // TODO 需要防止路径冲突
                CountDownLatch latch = new CountDownLatch(1);
                session.subscribe(name + StringUtility.DOT + type.getName(), MqttQoS.AT_MOST_ONCE.value(), (subscribe) -> {
                    latch.countDown();
                });
                latch.await();
            }
            manager.attachMonitor(monitor);
        }
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
}
 
Example #2
Source File: NlpSegmenterTestCase.java    From jstarcraft-nlp with Apache License 2.0 6 votes vote down vote up
@Test
public void testSegmenter() throws Exception {
    Tokenizer segmenter = getSegmenter();
    String text = "中华人民共和国(People's Republic of China),简称'中国'";
    segmenter.setReader(new StringReader(text));
    segmenter.reset();
    while (segmenter.incrementToken()) {
        // 词元
        CharTermAttribute term = segmenter.getAttribute(CharTermAttribute.class);
        // 偏移量
        OffsetAttribute offset = segmenter.getAttribute(OffsetAttribute.class);
        // 距离
        PositionIncrementAttribute position = segmenter.getAttribute(PositionIncrementAttribute.class);
        // 词性
        TypeAttribute type = segmenter.getAttribute(TypeAttribute.class);
        LOGGER.debug(StringUtility.format("segmenter:term is {}, begin is {}, end is {}", term, offset.startOffset(), offset.endOffset()));
        Assert.assertEquals(term.toString().toLowerCase(), text.substring(offset.startOffset(), offset.endOffset()).toLowerCase());
    }
}
 
Example #3
Source File: NettyTcpClientConnector.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized CommunicationSession<Channel> open(InetSocketAddress address, long wait) {
    if (sessionManager.getSession(address) != null) {
        throw new CommunicationException();
    }
    try {
        ChannelFuture future = connector.connect(address);
        future.sync();
        Channel channel = future.channel();
        channels.put(address, channel);
        return sessionManager.attachSession(address, channel);
    } catch (Throwable throwable) {
        String message = StringUtility.format("客户端异常");
        LOGGER.error(message, throwable);
        throw new CommunicationException();
    }
}
 
Example #4
Source File: VertxEventChannel.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public void triggerEvent(Object event) {
    Class type = event.getClass();
    byte[] bytes = codec.encode(type, event);
    switch (mode) {
    case QUEUE: {
        // TODO 需要防止路径冲突
        bus.send(name + StringUtility.DOT + type.getName(), bytes);
        break;
    }
    case TOPIC: {
        // TODO 需要防止路径冲突
        bus.publish(name + StringUtility.DOT + type.getName(), bytes);
        break;
    }
    }
}
 
Example #5
Source File: SimpleCondition.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public String getBeginContent() {
    if (AwkOperator.IN.equals(operator)) {
        StringBuilder buffer = new StringBuilder();
        int length = Array.getLength(value);
        for (int index = 0; index < length; index++) {
            Object element = Array.get(value, index);
            buffer.append(property);
            buffer.append("[");
            buffer.append(index);
            buffer.append("]=");
            buffer.append(element);
            buffer.append(";");
        }
        return buffer.toString();
    } else {
        return StringUtility.EMPTY;
    }

}
 
Example #6
Source File: PromptPersistenceManager.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public PersistenceElement deleteInstance(Comparable cacheId) {
	PersistenceElement element = new PersistenceElement(PersistenceOperation.DELETE, cacheId, null);
	Exception exception = null;
	Lock writeLock = lock.writeLock();
	try {
		writeLock.lock();
		accessor.deleteInstance(cacheClass, element.getCacheId());
		deletedCount.incrementAndGet();
	} catch (Exception throwable) {
		String message = StringUtility.format("立即策略[{}]处理元素[{}]时异常", new Object[] { name, element });
		LOGGER.error(message, throwable);
		exception = throwable;
		exceptionCount.incrementAndGet();
	} finally {
		writeLock.unlock();
	}
	if (monitor != null) {
		monitor.notifyOperate(element.getOperation(), element.getCacheId(), element.getCacheObject(), exception);
	}
	return element;
}
 
Example #7
Source File: LunarExpression.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
private int[] getRange(String field, int from, int to) {
    int[] range = new int[2];
    if (field.contains(StringUtility.ASTERISK)) {
        // 处理星符
        range[0] = from;
        range[1] = to - 1;
    } else {
        // 处理连接符
        if (!field.contains(StringUtility.DASH)) {
            range[0] = range[1] = field.startsWith("L") ? -Integer.valueOf(field.substring(1)) : Integer.valueOf(field);
        } else {
            String[] split = field.split(StringUtility.DASH);
            if (split.length > 2) {
                throw new IllegalArgumentException("Range has more than two fields: '" + field + "' in expression \"" + this.expression + "\"");
            }
            range[0] = split[0].startsWith("L") ? -Integer.valueOf(split[0].substring(1)) : Integer.valueOf(split[0]);
            range[1] = split[1].startsWith("L") ? -Integer.valueOf(split[1].substring(1)) : Integer.valueOf(split[1]);
        }
    }
    return range;
}
 
Example #8
Source File: ItemBigramModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
protected void readoutParameters() {
    float value;
    float sumAlpha = alpha.getSum(false);
    for (int userIndex = 0; userIndex < userSize; userIndex++) {
        for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
            value = (userTopicTimes.getValue(userIndex, topicIndex) + alpha.getValue(topicIndex)) / (userTokenNumbers.getValue(userIndex) + sumAlpha);
            userTopicSums.shiftValue(userIndex, topicIndex, value);
        }
    }
    for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
        float betaTopicValue = beta.getRowVector(topicIndex).getSum(false);
        for (int previousItemIndex = 0; previousItemIndex < itemSize + 1; previousItemIndex++) {
            for (int nextItemIndex = 0; nextItemIndex < itemSize; nextItemIndex++) {
                value = (topicItemBigramTimes[topicIndex][previousItemIndex][nextItemIndex] + beta.getValue(topicIndex, previousItemIndex)) / (topicItemProbabilities.getValue(topicIndex, previousItemIndex) + betaTopicValue);
                topicItemBigramSums[topicIndex][previousItemIndex][nextItemIndex] += value;
            }
        }
    }
    if (logger.isInfoEnabled()) {
        String message = StringUtility.format("sumAlpha is {}", sumAlpha);
        logger.info(message);
    }
    numberOfStatistics++;
}
 
Example #9
Source File: TermExpression.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
public TermExpression(String expression) {
    super(expression);

    this.seconds = new BitSet(60);
    this.minutes = new BitSet(60);
    this.hours = new BitSet(24);

    this.terms = new BitSet(24);
    this.years = new BitSet(200);

    String[] fields = expression.split(StringUtility.SPACE);
    if (fields.length != 4 && fields.length != 5) {
        throw new IllegalArgumentException();
    } else {
        this.setBits(this.seconds, fields[0], 0, 60, 0);
        this.setBits(this.minutes, fields[1], 0, 60, 0);
        this.setBits(this.hours, fields[2], 0, 24, 0);

        this.setBits(this.terms, this.replaceOrdinals(fields[3]), 0, 24, 0);
        if (fields.length == 5) {
            this.setBits(this.years, fields[4], 1900, 2100, 1900);
        } else {
            this.setBits(this.years, StringUtility.ASTERISK, 1900, 2100, 1900);
        }
    }
}
 
Example #10
Source File: MockDataFactory.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * item(离散:1:稠密)-profile(连续:n:稀疏)
 * 
 * <pre>
 * 可以当作item(离散:1:稠密)-item(离散:1:稠密)-degree(连续:1:稠密)
 * </pre>
 */
@Test
public void mockItemProfile() throws Exception {
    File file = new File("data/mock/item-profile");
    FileUtils.deleteQuietly(file);
    file.getParentFile().mkdirs();
    file.createNewFile();
    StringBuilder buffer = new StringBuilder();
    try (FileWriter writer = new FileWriter(file); BufferedWriter out = new BufferedWriter(writer);) {
        for (int leftIndex = 0; leftIndex < itemSize; leftIndex++) {
            buffer.setLength(0);
            for (int rightIndex = 0; rightIndex < profileSize; rightIndex++) {
                if (RandomUtility.randomFloat(1F) < ratio) {
                    float degree = RandomUtility.randomFloat(profileScope);
                    buffer.append(degree);
                }
                buffer.append(" ");
            }
            String profile = buffer.substring(0, buffer.length() - 1);
            out.write(StringUtility.format("{} {}", leftIndex, profile));
            out.newLine();
        }
    }
}
 
Example #11
Source File: MatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodec() throws Exception {
    EnvironmentContext context = EnvironmentFactory.getContext();
    Future<?> task = context.doTask(() -> {
        // 维度设置为100,可以测试编解码的效率.
        int dimension = 100;
        MathMatrix oldMatrix = getRandomMatrix(dimension);

        for (ModemCodec codec : ModemCodec.values()) {
            long encodeInstant = System.currentTimeMillis();
            byte[] data = codec.encodeModel(oldMatrix);
            String encodeMessage = StringUtility.format("编码{}数据的时间:{}毫秒", codec, System.currentTimeMillis() - encodeInstant);
            logger.info(encodeMessage);
            long decodeInstant = System.currentTimeMillis();
            MathMatrix newMatrix = (MathMatrix) codec.decodeModel(data);
            String decodeMessage = StringUtility.format("解码{}数据的时间:{}毫秒", codec, System.currentTimeMillis() - decodeInstant);
            logger.info(decodeMessage);
            Assert.assertThat(newMatrix, CoreMatchers.equalTo(oldMatrix));
        }
    });
    task.get();
}
 
Example #12
Source File: Neo4jAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
public Neo4jAccessor(SessionFactory factory) {
	this.template = factory.openSession();
	MetaData metaData = factory.metaData();
	for (ClassInfo information : metaData.persistentEntities()) {
		Class<?> ormClass = information.getUnderlyingClass();
		NodeEntity node = ormClass.getAnnotation(NodeEntity.class);
		RelationshipEntity relation = ormClass.getAnnotation(RelationshipEntity.class);
		if (node == null && relation == null) {
			continue;
		}
		Neo4jMetadata metadata = new Neo4jMetadata(ormClass);
		metadatas.put(ormClass, metadata);
		String ormName = metadata.getOrmName();
		String idName = metadata.getPrimaryName();

		String deletecCql = StringUtility.format(DELETE_CQL, ormName, idName);
		deleteCqls.put(ormClass, deletecCql);

		String maximumIdCql = StringUtility.format(MAXIMUM_ID, ormName, idName, idName, idName);
		maximumIdCqls.put(ormClass, maximumIdCql);

		String minimumIdCql = StringUtility.format(MINIMUM_ID, ormName, idName, idName, idName);
		minimumIdCqls.put(ormClass, minimumIdCql);
	}
}
 
Example #13
Source File: MyBatisAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
public MyBatisAccessor(Collection<Class<?>> classes, SqlSessionTemplate template) {
	this.template = template;

	Configuration configuration = template.getConfiguration();
	for (Class clazz : classes) {
		if (!configuration.hasMapper(clazz)) {
			configuration.addMapper(clazz);
		}

		MyBatisMetadata metadata = new MyBatisMetadata(clazz);
		metadatas.put(metadata.getOrmClass(), metadata);

		String maximumIdSql = StringUtility.format(MAXIMUM_ID, metadata.getColumnName(metadata.getPrimaryName()));
		maximumIdSqls.put(metadata.getOrmClass(), maximumIdSql);

		String minimumIdSql = StringUtility.format(MINIMUM_ID, metadata.getColumnName(metadata.getPrimaryName()));
		minimumIdSqls.put(metadata.getOrmClass(), minimumIdSql);
	}
}
 
Example #14
Source File: WeightLayer.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
public WeightLayer(int numberOfInputs, int numberOfOutputs, MathCache factory, Map<String, ParameterConfigurator> configurators, ActivationFunction function) {
    super(numberOfInputs, numberOfOutputs, configurators, function);

    if (!this.configurators.containsKey(WEIGHT_KEY)) {
        String message = StringUtility.format("参数{}配置缺失.", WEIGHT_KEY);
        throw new IllegalArgumentException(message);
    }

    MathMatrix weightParameter = factory.makeMatrix(numberOfInputs, numberOfOutputs);
    configurators.get(WEIGHT_KEY).getFactory().setValues(weightParameter);
    this.parameters.put(WEIGHT_KEY, weightParameter);
    MathMatrix weightGradient = factory.makeMatrix(numberOfInputs, numberOfOutputs);
    this.gradients.put(WEIGHT_KEY, weightGradient);

    if (this.configurators.containsKey(BIAS_KEY)) {
        MathMatrix biasParameter = factory.makeMatrix(1, numberOfOutputs);
        configurators.get(BIAS_KEY).getFactory().setValues(biasParameter);
        this.parameters.put(BIAS_KEY, biasParameter);
        MathMatrix biasGradient = factory.makeMatrix(1, numberOfOutputs);
        this.gradients.put(BIAS_KEY, biasGradient);
    }
}
 
Example #15
Source File: LuaFunction.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T doWith(Class<T> clazz, Object... arguments) {
    try {
        LuaHolder holder = threadHolder.get();
        for (int index = 0, size = classes.length; index < size; index++) {
            holder.attributes.put(StringUtility.format("argument{}", index), arguments[index]);
        }
        holder.attributes.putAll(holder.scope.getAttributes());
        CompiledScript script = holder.script;
        T object = (T) script.eval();
        holder.scope.deleteAttributes();
        return object;
    } catch (ScriptException exception) {
        throw new ScriptExpressionException(exception);
    }
}
 
Example #16
Source File: SecurityUtility.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
/**
 * DES加密
 * 
 * @param data
 * @param key
 * @return
 */
public static byte[] encryptDes(byte[] data, byte[] key) {
    try {
        // DES算法要求有一个可信任的随机数源
        SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey secretKey = keyFactory.generateSecret(desKey);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
        return cipher.doFinal(data);
    } catch (Exception exception) {
        String message = StringUtility.format("DES加密数据异常:[{}]", Arrays.toString(data));
        throw new RuntimeException(message, exception);
    }
}
 
Example #17
Source File: ContentCodecTestCase.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
private void testPerformance(ContentCodec contentCodec, Type type, Object instance) {
    byte[] data = contentCodec.encode(type, instance);
    String message = StringUtility.format("格式化{}大小:{},{}", type.getTypeName(), data.length, Arrays.toString(data));
    logger.debug(message);

    Instant now = null;
    int times = 100000;
    now = Instant.now();
    for (int index = 0; index < times; index++) {
        contentCodec.encode(type, instance);
    }
    logger.debug(StringUtility.format("编码{}次一共消耗{}毫秒.", times, System.currentTimeMillis() - now.toEpochMilli()));

    now = Instant.now();
    for (int index = 0; index < times; index++) {
        contentCodec.decode(type, data);
    }
    logger.debug(StringUtility.format("解码{}次一共消耗{}毫秒.", times, System.currentTimeMillis() - now.toEpochMilli()));
}
 
Example #18
Source File: MockDataFactory.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * user(离散:1:稠密)-profile(连续:n:稀疏)
 * 
 * <pre>
 * 可以当作user(离散:1:稠密)-user(离散:1:稠密)-degree(连续:1:稠密)
 * </pre>
 * 
 * >
 */
@Test
public void mockUserProfile() throws Exception {
    File file = new File("data/mock/user-profile");
    FileUtils.deleteQuietly(file);
    file.getParentFile().mkdirs();
    file.createNewFile();
    StringBuilder buffer = new StringBuilder();
    try (FileWriter writer = new FileWriter(file); BufferedWriter out = new BufferedWriter(writer);) {
        for (int leftIndex = 0; leftIndex < userSize; leftIndex++) {
            buffer.setLength(0);
            for (int rightIndex = 0; rightIndex < profileSize; rightIndex++) {
                if (RandomUtility.randomFloat(1F) < ratio) {
                    float degree = RandomUtility.randomFloat(profileScope);
                    buffer.append(degree);
                }
                buffer.append(" ");
            }
            String profile = buffer.substring(0, buffer.length() - 1);
            out.write(StringUtility.format("{} {}", leftIndex, profile));
            out.newLine();
        }
    }
}
 
Example #19
Source File: BooleanConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public Object readValueFrom(ProtocolReader context, Type type, ClassDefinition definition) throws IOException {
    InputStream in = context.getInputStream();
    byte information = (byte) in.read();
    byte mark = getMark(information);
    if (mark == NULL_MARK) {
        return null;
    }
    if (mark == FALSE_MARK) {
        if (type == AtomicBoolean.class) {
            return new AtomicBoolean(false);
        }
        return false;
    } else if (mark == TRUE_MARK) {
        if (type == AtomicBoolean.class) {
            return new AtomicBoolean(true);
        }
        return true;
    }
    String message = StringUtility.format("类型码[{}]没有对应标记码[{}]", type, mark);
    throw new CodecConvertionException(message);
}
 
Example #20
Source File: ResourceMonitor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
/** 更新通知 */
@Override
public void update(Observable manager, Object argument) {
    try {
        updateStorage(ResourceManager.class.cast(manager));
    } catch (Exception exception) {
        String message = StringUtility.format("仓储[{}]更新异常", clazz);
        logger.error(message);
        throw new StorageException(message, exception);
    }
}
 
Example #21
Source File: RockMqTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messages, ConsumeConcurrentlyContext context) {
    for (MessageExt message : messages) {
        String body = new String(message.getBody(), StringUtility.CHARSET);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "消费响应:msgId : " + message.getMsgId() + ",  msgBody : " + body);// 输出消息内容
        semaphore.release(1);
    }

    // 返回消费状态
    // CONSUME_SUCCESS 消费成功
    // RECONSUME_LATER 消费失败,需要重新消费
    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
 
Example #22
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 #23
Source File: NettyTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
/**
 * 测试性能
 */
@Test
public void testPerformance() throws Exception {
    int count = 25; // 线程数
    int times = 1000; // 任务数
    nettyClientConnector.open(clientAddress, 5000);
    // TODO 是否等待服务端?
    ExecutorService executor = Executors.newFixedThreadPool(count);
    CountDownLatch latch = new CountDownLatch(count);
    for (int thread = 1; thread <= count; thread++) {
        long userId = thread;
        executor.submit(() -> {
            try {
                MockServerInterface service = clientCommandManager.getProxy(MockServerInterface.class, SessionManager.address2Key(clientAddress), 10000);
                String userName = "birdy";
                UserObject user = UserObject.instanceOf(userId, userName);
                for (int time = 0; time < times; time++) {
                    Assert.assertThat(service.createUser(user), CoreMatchers.equalTo(user));
                    Assert.assertThat(service.getUser(userId), CoreMatchers.equalTo(user));
                    Assert.assertNull(service.deleteUser(userId));
                }
                latch.countDown();
            } catch (Exception exception) {
                logger.error(StringUtility.EMPTY, exception);
            }
        });
    }
    latch.await();
    nettyClientConnector.close(clientAddress);
}
 
Example #24
Source File: RedisTopicEventChannel.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
protected RTopic getTopic(Class type) {
    RTopic topic = topics.get(type);
    if (topic == null) {
        topic = redisson.getTopic(name + StringUtility.DOT + type.getName(), byteCodec);
        topics.put(type, topic);
    }
    return topic;
}
 
Example #25
Source File: SecurityUtility.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
/**
 * SHA224摘要
 * 
 * @param data
 * @return
 */
public static byte[] signatureSha224(byte[] data) {
    try {
        MessageDigest algorithm = MessageDigest.getInstance(SHA224);
        return algorithm.digest(data);
    } catch (Exception exception) {
        String message = StringUtility.format("[{}]:SHA224信息摘要异常", data);
        throw new RuntimeException(message, exception);
    }
}
 
Example #26
Source File: JsonType.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
private static String getFieldName(Class<?> clazz, String columnName) {
    Map<String, String> column2Field = CLAZZ_2_COLUMN.get(clazz);
    synchronized (CLAZZ_2_COLUMN) {
        if (column2Field == null) {
            column2Field = new ConcurrentHashMap<>();
            CLAZZ_2_COLUMN.put(clazz, column2Field);
        }
    }
    String fieldName = column2Field.get(columnName);
    if (fieldName != null) {
        return fieldName;
    }
    synchronized (column2Field) {
        while (clazz != null) {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                org.hibernate.annotations.Type typeAnnotation = field.getDeclaredAnnotation(org.hibernate.annotations.Type.class);
                if (typeAnnotation != null && typeAnnotation.type().equals(JsonType.class.getName())) {
                    if (field.getName().equalsIgnoreCase(columnName)) {
                        fieldName = field.getName();
                        column2Field.put(columnName, fieldName);
                        return fieldName;
                    } else {
                        Column columnAnnotation = field.getDeclaredAnnotation(Column.class);
                        if (columnAnnotation != null && columnAnnotation.name().equalsIgnoreCase(columnName)) {
                            fieldName = field.getName();
                            column2Field.put(columnName, fieldName);
                            return fieldName;
                        }
                    }
                }
            }
            clazz = clazz.getSuperclass();
        }
    }
    String message = StringUtility.format("数据列{}对应字段的不存在", columnName);
    throw new StorageAccessException(message);
}
 
Example #27
Source File: MongoMetadata.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
/**
 * 构造方法
 * 
 * @param metadata
 */
MongoMetadata(Class<?> clazz) {
    Document document = clazz.getAnnotation(Document.class);
    if (document == null) {
        throw new IllegalArgumentException();
    }
    ormName = document.collection();
    if (StringUtility.isBlank(ormName)) {
        ormName = clazz.getSimpleName();
        ormName = ormName.substring(0, 1).toLowerCase() + ormName.substring(1, ormName.length());
    }
    ormClass = clazz;
    ReflectionUtility.doWithFields(ormClass, (field) -> {
        if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) {
            return;
        }
        if (field.isAnnotationPresent(Version.class)) {
            versionName = field.getName();
            return;
        }
        Class<?> type = field.getType();
        fields.put(field.getName(), type);
        if (field.isAnnotationPresent(Id.class)) {
            primaryName = field.getName();
            primaryClass = type;
        }
        if (field.isAnnotationPresent(Indexed.class)) {
            indexNames.add(field.getName());
        }
    });
}
 
Example #28
Source File: SecurityUtilityTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testAes() {
    byte[] aseKey = SecurityUtility.getAes();
    byte[] data = content.getBytes(StringUtility.CHARSET);
    data = SecurityUtility.encryptAes(data, aseKey);
    data = SecurityUtility.decryptAes(data, aseKey);
    Assert.assertThat(new String(data, StringUtility.CHARSET), CoreMatchers.equalTo(content));
}
 
Example #29
Source File: EnumerationConverter.java    From jstarcraft-core with Apache License 2.0 5 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;
    }
    Enum<?> enumeration = (Enum<?>) value;
    int index = enumeration.ordinal();
    out.print(index);
}
 
Example #30
Source File: StringConverter.java    From jstarcraft-core with Apache License 2.0 5 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;
    }
    value = value + StringUtility.SEMICOLON;
    out.print(value);
}