Java Code Examples for cn.hutool.core.collection.CollUtil#newHashSet()

The following examples show how to use cn.hutool.core.collection.CollUtil#newHashSet() . 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: HutoolController.java    From mall-learning with Apache License 2.0 6 votes vote down vote up
@ApiOperation("CollUtil使用:集合工具类")
@GetMapping("/collUtil")
public CommonResult collUtil() {
    //数组转换为列表
    String[] array = new String[]{"a", "b", "c", "d", "e"};
    List<String> list = CollUtil.newArrayList(array);
    //join:数组转字符串时添加连接符号
    String joinStr = CollUtil.join(list, ",");
    LOGGER.info("collUtil join:{}", joinStr);
    //将以连接符号分隔的字符串再转换为列表
    List<String> splitList = StrUtil.split(joinStr, ',');
    LOGGER.info("collUtil split:{}", splitList);
    //创建新的Map、Set、List
    HashMap<Object, Object> newMap = CollUtil.newHashMap();
    HashSet<Object> newHashSet = CollUtil.newHashSet();
    ArrayList<Object> newList = CollUtil.newArrayList();
    //判断列表是否为空
    CollUtil.isEmpty(list);
    CollUtil.isNotEmpty(list);
    return CommonResult.success(null, "操作成功");
}
 
Example 2
Source File: DynamicRouteLocator.java    From Taroco with Apache License 2.0 5 votes vote down vote up
/**
 * 从Redis中读取缓存的路由信息,没有从rbac拉取,避免启动链路依赖问题(取舍),网关依赖业务模块的问题
 *
 * @return 缓存中的路由表
 */
private Map<String, ZuulProperties.ZuulRoute> locateRoutesFromCache() {
    Map<String, ZuulProperties.ZuulRoute> routes = new LinkedHashMap<>();

    String vals = redisRepository.get(CacheConstants.ROUTE_KEY);
    if (vals == null) {
        return routes;
    }

    List<SysRoute> results = JSONArray.parseArray(vals, SysRoute.class);
    for (SysRoute result : results) {
        if (StrUtil.isBlank(result.getPath()) && StrUtil.isBlank(result.getUrl())) {
            continue;
        }

        ZuulProperties.ZuulRoute zuulRoute = new ZuulProperties.ZuulRoute();
        try {
            zuulRoute.setId(result.getServiceId());
            zuulRoute.setPath(result.getPath());
            zuulRoute.setServiceId(result.getServiceId());
            zuulRoute.setRetryable(StrUtil.equals(result.getRetryable(), "0") ? Boolean.FALSE : Boolean.TRUE);
            zuulRoute.setStripPrefix(StrUtil.equals(result.getStripPrefix(), "0") ? Boolean.FALSE : Boolean.TRUE);
            zuulRoute.setUrl(result.getUrl());
            List<String> sensitiveHeadersList = StrUtil.splitTrim(result.getSensitiveheadersList(), ",");
            if (sensitiveHeadersList != null) {
                Set<String> sensitiveHeaderSet = CollUtil.newHashSet();
                sensitiveHeaderSet.addAll(sensitiveHeadersList);
                zuulRoute.setSensitiveHeaders(sensitiveHeaderSet);
                zuulRoute.setCustomSensitiveHeaders(true);
            }
        } catch (Exception e) {
            log.error("从数据库加载路由配置异常", e);
        }
        log.debug("添加数据库自定义的路由配置,path:{},serviceId:{}", zuulRoute.getPath(), zuulRoute.getServiceId());
        routes.put(zuulRoute.getPath(), zuulRoute);
    }
    return routes;
}
 
Example 3
Source File: BeanCopier.java    From spring-cloud-shop with MIT License 4 votes vote down vote up
/**
 * 值提供器转Bean
 *
 * @param valueProvider 值提供器
 * @param bean          Bean
 */
private void valueProviderToBean(ValueProvider<String> valueProvider, Object bean) {
    if (null == valueProvider) {
        return;
    }

    Class<?> actualEditable = bean.getClass();
    if (copyOptions.editable != null) {
        // 检查限制类是否为target的父类或接口
        if (!copyOptions.editable.isInstance(bean)) {
            throw new IllegalArgumentException(StrUtil.format("Target class [{}] not assignable to Editable class [{}]", bean.getClass().getName(), copyOptions.editable.getName()));
        }
        actualEditable = copyOptions.editable;
    }
    final HashSet<String> ignoreSet = (null != copyOptions.ignoreProperties) ? CollUtil.newHashSet(copyOptions.ignoreProperties) : null;
    final Map<String, String> fieldReverseMapping = copyOptions.getReversedMapping();

    final Collection<BeanDesc.PropDesc> props = BeanUtil.getBeanDesc(actualEditable).getProps();
    String fieldName;
    Object value;
    Method setterMethod;
    Class<?> propClass;
    for (BeanDesc.PropDesc prop : props) {
        // 获取值
        fieldName = prop.getFieldName();
        if (CollUtil.contains(ignoreSet, fieldName)) {
            // 目标属性值被忽略或值提供者无此key时跳过
            continue;
        }
        final String providerKey = mappingKey(fieldReverseMapping, fieldName);
        if (!valueProvider.containsKey(providerKey)) {
            // 无对应值可提供
            continue;
        }
        setterMethod = prop.getSetter();
        if (null == setterMethod) {
            // Setter方法不存在跳过
            continue;
        }
        value = valueProvider.value(providerKey, TypeUtil.getFirstParamType(setterMethod));
        // 如果是字符串
        if (value instanceof String) {
            String v = (String) value;
            // 字符串为空,则跳过
            if (StrUtil.isBlank(v) && copyOptions.ignoreNullValue) {
                continue;
            }
        }
        if (null == value && copyOptions.ignoreNullValue) {
            // 当允许跳过空时,跳过
            continue;
        }

        try {
            // valueProvider在没有对值做转换且当类型不匹配的时候,执行默认转换
            propClass = prop.getFieldClass();
            if (!propClass.isInstance(value)) {
                value = Convert.convert(propClass, value);
                if (null == value && copyOptions.ignoreNullValue) {
                    // 当允许跳过空时,跳过
                    continue;
                }
            }

            // 执行set方法注入值
            setterMethod.invoke(bean, value);
        } catch (Exception e) {
            if (copyOptions.ignoreError) {
                // 忽略注入失败
                continue;
            } else {
                throw new UtilException(e, "Inject [{}] error!", prop.getFieldName());
            }
        }
    }
}