cn.hutool.core.util.CharUtil Java Examples

The following examples show how to use cn.hutool.core.util.CharUtil. 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: ChineseValidator.java    From yue-library with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
	String validValue = null;
	if ((CharUtil.isChar(value) && !CharUtil.isBlankChar((char) value))
			|| (value instanceof String && StrUtil.isNotBlank((String) value))) {
		validValue = StrUtil.toString(value);
	}
	
	if (StringUtils.isNotBlank(validValue)) {
		return Validator.isChinese(validValue);
	}
	
	if (notNull) {
		return false;
	}
	
	return true;
}
 
Example #2
Source File: AutoIncrementZerofillUtils.java    From yue-library with Apache License 2.0 6 votes vote down vote up
/**
 * 字符串尾部值自动递增
 * @param str 尾部值是 {@linkplain Integer} 类型
 * @return 自动递增后的值
 * @throws ParamException 如:("999", "str999")
 */
public static String autoIncrement(String str) {
	int maxIndex = str.length() - 1;
	Integer autoIncrementValue = Integer.parseInt(CharUtil.toString(str.charAt(maxIndex))) + 1;
	if (autoIncrementValue == 10) {
		int cycleIndex = 0;
		for (int i = maxIndex - 1; i >= 0; i--) {
			Integer autoIncrementValueI = Integer.parseInt(CharUtil.toString(str.charAt(i))) + 1;
			cycleIndex++;
			if (autoIncrementValueI != 10) {
				String pad = StrUtil.padPre("0", cycleIndex, '0');
				String replaceValue = autoIncrementValueI.toString() + pad;
				return StringUtils.replace(str, replaceValue, i, i + 1 + replaceValue.length());
			}
		}
		
		throw new ParamException("无法自动递增,此参数已是最大值:" + str);
	}
	
	return str.substring(0, maxIndex) + autoIncrementValue;
}
 
Example #3
Source File: AutoIncrementZerofillUtils.java    From yue-library with Apache License 2.0 6 votes vote down vote up
/**
 * 字符串尾部值自动递减
 * @param str 尾部值是 {@linkplain Integer} 类型
 * @return 自动递减后的值
 */
public static String autoDecr(String str) {
	int maxIndex = str.length() - 1;
	Integer autoDecrValue = Integer.parseInt(CharUtil.toString(str.charAt(maxIndex))) - 1;
	if (autoDecrValue == -1) {
		int cycleIndex = 0;
		for (int i = maxIndex - 1; i >= 0; i--) {
			Integer autoDecrValueI = Integer.parseInt(CharUtil.toString(str.charAt(i))) - 1;
			cycleIndex++;
			if (autoDecrValueI != -1) {
				String pad = StrUtil.padPre("9", cycleIndex, '9');
				String replaceValue = autoDecrValueI.toString() + pad;
				return StringUtils.replace(str, replaceValue, i, i + 1 + replaceValue.length());
			}
		}
		
		throw new ParamException("无法自动递减,此参数已是最小值:" + str);
	}
	
	return str.substring(0, maxIndex) + autoDecrValue;
}
 
Example #4
Source File: GenUtil.java    From albedo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 获取数据模型
 *
 * @param scheme
 * @return
 */
public static Map<String, Object> getDataModel(SchemeDto scheme) {
	Map<String, Object> model = Maps.newHashMap();
	String applicationName = SpringContextHolder.getApplicationContext().getBeansWithAnnotation(SpringBootApplication.class).keySet().iterator().next();
	model.put("applicationName", SpringContextHolder.getApplicationContext().getBean(applicationName).getClass().getPackage().getName() + StringUtil.DOT + StringUtil.upperFirst(applicationName));
	model.put("packageName", StringUtil.lowerCase(scheme.getPackageName()));
	model.put("lastPackageName", StringUtil.subAfter((String) model.get("packageName"), StringUtil.DOT, true));
	model.put("moduleName", StringUtil.lowerCase(scheme.getModuleName()));
	model.put("subModuleName", StringUtil.lowerCase(StringUtil.isEmpty(scheme.getSubModuleName()) ? "" : scheme.getSubModuleName()));
	model.put("className", StringUtil.lowerFirst(scheme.getTableDto().getClassName()));
	model.put("classNameUrl", StringUtil.toRevertCamelCase(StringUtil.toStrString(model.get("className")), CharUtil.DASHED));
	model.put("ClassName", StringUtil.upperFirst(scheme.getTableDto().getClassName()));

	model.put("functionName", scheme.getFunctionName());
	model.put("functionNameSimple", scheme.getFunctionNameSimple());
	model.put("functionAuthor", StringUtil.isNotBlank(scheme.getFunctionAuthor()) ? scheme.getFunctionAuthor() : "");
	model.put("functionVersion", DateUtil.now());
	model.put("urlPrefix", model.get("moduleName") + (StringUtil.isNotBlank(scheme.getSubModuleName()) ? StringUtil.SLASH +
		StringUtil.lowerCase(scheme.getSubModuleName()) : "") + StringUtil.SLASH + model.get("classNameUrl")
	);
	model.put("viewPrefix", model.get("urlPrefix"));
	model.put("permissionPrefix", model.get("moduleName") + (StringUtil.isNotBlank(scheme.getSubModuleName()) ? "_" + StringUtil.lowerCase(scheme.getSubModuleName()) : "") + "_" + model.get("className"));
	model.put("table", scheme.getTableDto());
	model.put("scheme", scheme);
	return model;
}
 
Example #5
Source File: Wrapper.java    From yue-library with Apache License 2.0 5 votes vote down vote up
/**
 * 去除字段包装
 * 
 * @param field 字段
 * @return 去除包装后的字段
 */
public String unwrap(String field) {
	if (StringUtils.isEmpty(field)) {
		return field;
	}
	
	return StringUtils.deleteFirstLastEqualString(field, CharUtil.toString(getPreWrapQuote()),
			CharUtil.toString(getSufWrapQuote()));
}
 
Example #6
Source File: FileTailWatcherRun.java    From Jpom with MIT License 5 votes vote down vote up
private void startRead() throws IOException {
    if (ExtConfigBean.getInstance().getLogInitReadLine() == 0) {
        // 不初始读取
        return;
    }
    long len = randomFile.length();
    long start = randomFile.getFilePointer();
    long nextEnd = start + len - 1;
    randomFile.seek(nextEnd);
    int c;
    while (nextEnd > start) {
        // 满
        if (limitQueue.full()) {
            break;
        }
        c = randomFile.read();
        if (c == CharUtil.LF || c == CharUtil.CR) {
            this.readLine();
            nextEnd--;
        }
        nextEnd--;
        randomFile.seek(nextEnd);
        if (nextEnd == 0) {
            // 当文件指针退至文件开始处,输出第一行
            this.readLine();
            break;
        }
    }
    // 移动到尾部
    randomFile.seek(len);
}
 
Example #7
Source File: SchemeResource.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Log(value = "生成方案编辑")
@PostMapping("/gen-menu")
@PreAuthorize("@pms.hasPermission('gen_scheme_menu')")
public Result genMenu(@Valid @RequestBody SchemeGenDto schemeGenDto) {
	SchemeDto schemeDto = schemeService.getOneDto(schemeGenDto.getId());
	TableDto tableDto = schemeDto.getTableDto();
	if (tableDto == null) {
		tableDto = tableService.getOneDto(schemeDto.getTableId());
	}
	String url = StringUtil.toAppendStr(StringUtil.SLASH, StringUtil.lowerCase(schemeDto.getModuleName()), (StringUtil.isNotBlank(schemeDto.getSubModuleName()) ? StringUtil.SLASH + StringUtil.lowerCase(schemeDto.getSubModuleName()) : ""), StringUtil.SLASH,
		StringUtil.toRevertCamelCase(StringUtil.lowerFirst(tableDto.getClassName()), CharUtil.DASHED), StringUtil.SLASH);
	menuService.saveByGenScheme(new GenSchemeDto(schemeDto.getName(), schemeGenDto.getParentMenuId(), url, tableDto.getClassName()));
	return Result.buildOk("生成", schemeDto.getName(), "菜单成功");
}
 
Example #8
Source File: QueryWrapperUtil.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Wrapper<?> fillWrapperOrder(Page<?> page, Wrapper<?> wrapper) {
	if (null == page) {
		return wrapper;
	}
	if (ObjectUtil.isEmpty(page.orders())) {
		return wrapper;
	}
	QueryWrapper queryWrapper = null == wrapper ? new QueryWrapper() : (QueryWrapper) wrapper;
	if (ObjectUtil.isNotEmpty(page.orders())) {
		page.orders().forEach(orderItem -> queryWrapper.orderBy(true, orderItem.isAsc(),
			StringUtil.toRevertCamelCase(orderItem.getColumn(), CharUtil.UNDERLINE)));
		page.setOrders(null);
	}
	return queryWrapper;
}
 
Example #9
Source File: QueryWrapperUtil.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static <T> QueryWrapper<T> getWrapper(Object query) {
	QueryWrapper<T> entityWrapper = Wrappers.query();
	if (query == null) {
		return entityWrapper;
	}
	Field[] fields = ReflectUtil.getFields(query.getClass());
	try {
		for (Field field : fields) {
			boolean accessible = field.isAccessible();
			field.setAccessible(true);
			Query q = field.getAnnotation(Query.class);
			if (q != null) {
				String propName = q.propName();
				String blurry = q.blurry();
				String attributeName = StringUtil.isEmpty(propName) ? StringUtil.toRevertCamelCase(field.getName(), CharUtil.UNDERLINE) : propName;
				Object val = field.get(query);
				if (cn.hutool.core.util.ObjectUtil.isNull(val) || "".equals(val)) {
					continue;
				}
				// 模糊多字段
				if (cn.hutool.core.util.ObjectUtil.isNotEmpty(blurry)) {
					String[] blurrys = blurry.split(",");
					entityWrapper.and(i -> {
						for (String s : blurrys) {
							i.or().like(s, val.toString());
						}
					});
					continue;
				}
				parseWarpper(entityWrapper, q, attributeName, val);
			}
			field.setAccessible(accessible);
		}
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
	return entityWrapper;
}
 
Example #10
Source File: CommUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 4 votes vote down vote up
static String combineTextBlocks(List<TextBlock> textBlocks, boolean isEng) {
    textBlocks.sort(Comparator.comparingInt(o -> o.getTopLeft().y));
    List<List<TextBlock>> lineBlocks = new ArrayList<>();
    int lastY = -1;
    List<TextBlock> lineBlock = new ArrayList<>();
    boolean sameLine = true;
    int minX = Integer.MAX_VALUE;
    TextBlock minBlock = null;
    TextBlock maxBlock = null;
    int maxX = -1;
    double maxAngle = -100;
    for (TextBlock textBlock : textBlocks) {
        //System.out.println(textBlock.getAngle()+ "\t" + textBlock.getFontSize());
        if (textBlock.getTopLeft().x < minX) {
            minX = textBlock.getTopLeft().x;
            minBlock = textBlock;
        }
        if (textBlock.getTopRight().x > maxX) {
            maxX = textBlock.getTopRight().x;
            maxBlock = textBlock;
        }
        if (Math.abs(textBlock.getAngle()) > maxAngle){
            maxAngle = Math.abs(textBlock.getAngle());
        }
        if (lastY == -1) {
            lastY = textBlock.getTopLeft().y;
        } else {
            sameLine = textBlock.getTopLeft().y - lastY <= SAME_LINE_LIMIT;
        }
        if (!sameLine) {
            lineBlock.sort(Comparator.comparingInt(o -> o.getTopLeft().x));
            lineBlocks.add(lineBlock);
            lineBlock = new ArrayList<>();
            sameLine = true;
            lastY = textBlock.getTopLeft().y;
        }
        lineBlock.add(textBlock);
    }

    if (maxAngle >= 0.05){
        //todo 文本倾斜校正
    }

    if (lineBlock.size() > 0) {
        lineBlock.sort(Comparator.comparingInt(o -> o.getTopLeft().x));
        lineBlocks.add(lineBlock);
    }
    StringBuilder sb = new StringBuilder();
    TextBlock lastBlock = null;
    for (List<TextBlock> line : lineBlocks) {
        TextBlock firstBlock = line.get(0);
        if (lastBlock != null) {
            String blockTxt = lastBlock.getText().trim();
            String endTxt = blockTxt.substring(blockTxt.length() - 1);
            if (maxX - lastBlock.getTopRight().x >= CHAR_WIDTH * 2 ||
                    !NORMAL_CHAR.matcher(endTxt).find() ||
                    (NORMAL_CHAR.matcher(endTxt).find() &&
                    (firstBlock.getTopLeft().x - minX) > CHAR_WIDTH * 2)){
                sb.append("\n");
                for (int i = 0, ln = (firstBlock.getTopLeft().x - minX) / CHAR_WIDTH; i < ln; i++) {
                    if (i % 2 == 0){
                        sb.append("    ");
                    }
                }
            }
            else{
                if (CharUtil.isLetterOrNumber(endTxt.charAt(0)) && CharUtil.isLetterOrNumber(firstBlock.getText().charAt(0))){
                    sb.append(" ");
                }
            }
        }
        else{
            for (int i = 0, ln = (firstBlock.getTopLeft().x - minX) / CHAR_WIDTH; i < ln; i++) {
                if (i % 2 == 0){
                    sb.append("    ");
                }
            }
        }

        for (int i = 0; i < line.size(); i++) {
            TextBlock text = line.get(i);
            String ocrText = text.getText();
            if (i > 0) {
                for (int a = 0, ln = (text.getTopLeft().x - line.get(i - 1).getTopRight().x) / (CHAR_WIDTH * 2);
                     a < ln; a++) {
                    sb.append("  ");
                }
            }
            sb.append(ocrText);
        }
        lastBlock = line.get(line.size() - 1);
    }
    return sb.toString();
}
 
Example #11
Source File: MenuServiceImpl.java    From albedo with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean saveByGenScheme(GenSchemeDto schemeDto) {

	String moduleName = schemeDto.getSchemeName(),
		parentMenuId = schemeDto.getParentMenuId(),
		url = schemeDto.getUrl();
	String permission = StringUtil.toCamelCase(StringUtil.lowerFirst(url), CharUtil.DASHED)
		.replace(StringUtil.SLASH, "_").substring(1),
		permissionLike = permission.substring(0, permission.length() - 1);
	List<Menu> currentMenuList = repository.selectList(Wrappers.<Menu>query()
		.lambda().eq(Menu::getName, moduleName).or()
		.likeLeft(Menu::getPermission, permissionLike)
	);
	for (Menu currentMenu : currentMenuList) {
		if (currentMenu != null) {
			List<String> idList = repository.selectList(Wrappers.<Menu>query()
				.lambda()
				.likeLeft(Menu::getPermission, permissionLike)
				.or(i -> i.eq(Menu::getId, currentMenu.getId())
					.or().eq(Menu::getParentId, currentMenu.getId()))
			).stream().map(Menu::getId).collect(Collectors.toList());
			roleMenuRepository
				.delete(Wrappers.<RoleMenu>query()
					.lambda().in(RoleMenu::getMenuId, idList));
			repository.deleteBatchIds(idList);
		}
	}
	Menu parentMenu = repository.selectById(parentMenuId);
	Assert.isTrue(parentMenu != null, StringUtil.toAppendStr("根据模块id[", parentMenuId, "无法查询到模块信息]"));


	Menu module = new Menu();

	module.setName(moduleName);
	module.setParentId(parentMenu.getId());
	module.setType(Menu.TYPE_MENU);
	module.setIcon("icon-right-square");
	module.setPath(StringUtil.toRevertCamelCase(StringUtil.lowerFirst(schemeDto.getClassName()), CharUtil.DASHED));
	module.setComponent(url.substring(1) + "index");
	save(module);

	Menu moduleView = new Menu();
	moduleView.setParent(module);
	moduleView.setName(moduleName + "查看");
	moduleView.setPermission(permission + "view");
	moduleView.setParentId(module.getId());
	moduleView.setType(Menu.TYPE_BUTTON);
	moduleView.setSort(20);
	save(moduleView);
	Menu moduleEdit = new Menu();
	moduleEdit.setParent(module);
	moduleEdit.setName(moduleName + "编辑");
	moduleEdit.setPermission(permission + "edit");
	moduleEdit.setParentId(module.getId());
	moduleEdit.setType(Menu.TYPE_BUTTON);
	moduleEdit.setSort(40);
	save(moduleEdit);
	Menu moduleDelete = new Menu();
	moduleDelete.setParent(module);
	moduleDelete.setName(moduleName + "删除");
	moduleDelete.setPermission(permission + "del");
	moduleDelete.setParentId(module.getId());
	moduleDelete.setType(Menu.TYPE_BUTTON);
	moduleDelete.setSort(80);
	save(moduleDelete);
	return true;
}