From aed94bc233d3512ab5ad9067868cee4cc7b686e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E5=90=9B=E7=A6=B9?= <2673609096@qq.com> Date: Mon, 1 Sep 2025 15:43:21 +0800 Subject: [PATCH] =?UTF-8?q?farm=E6=A8=A1=E5=9D=97-=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E9=9B=86=E5=90=88=E8=BD=AC=E6=8D=A2=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/util/StringCollectionUtil.java | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 yudao-module-farm/src/main/java/cn/iocoder/yudao/module/farm/framework/util/StringCollectionUtil.java diff --git a/yudao-module-farm/src/main/java/cn/iocoder/yudao/module/farm/framework/util/StringCollectionUtil.java b/yudao-module-farm/src/main/java/cn/iocoder/yudao/module/farm/framework/util/StringCollectionUtil.java new file mode 100644 index 00000000..6a4dd46e --- /dev/null +++ b/yudao-module-farm/src/main/java/cn/iocoder/yudao/module/farm/framework/util/StringCollectionUtil.java @@ -0,0 +1,170 @@ +package cn.iocoder.yudao.module.farm.framework.util; + +import cn.hutool.core.util.StrUtil; +import lombok.extern.slf4j.Slf4j; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 字符串集合转换工具类 + * 用于在数据库字符串(逗号分隔)和Java集合之间转换 + * + * @author 芋道源码 + */ +@Slf4j +public class StringCollectionUtil { + + /** + * 分隔符 + */ + private static final String SEPARATOR = ","; + + /** + * 将字符串集合转换为数据库存储的字符串 + * + * @param stringList 字符串集合 + * @return 逗号分隔的字符串,如果集合为空则返回null + */ + public static String toDatabaseString(List stringList) { + if (stringList == null || stringList.isEmpty()) { + return null; + } + // 过滤空字符串并去重 + List filteredList = stringList.stream() + .filter(StrUtil::isNotBlank) + .distinct() + .collect(Collectors.toList()); + + if (filteredList.isEmpty()) { + return null; + } + + return String.join(SEPARATOR, filteredList); + } + + /** + * 将数据库字符串转换为字符串集合 + * + * @param databaseString 数据库中的字符串 + * @return 字符串集合,如果字符串为空则返回空集合 + */ + public static List toStringList(String databaseString) { + if (StrUtil.isBlank(databaseString)) { + return new ArrayList<>(); + } + + try { + return Arrays.stream(databaseString.split(SEPARATOR)) + .map(String::trim) + .filter(StrUtil::isNotBlank) + .distinct() + .collect(Collectors.toList()); + } catch (Exception e) { + log.error("转换字符串集合失败,原始字符串: {}", databaseString, e); + return new ArrayList<>(); + } + } + + /** + * 将单个字符串转换为集合(兼容旧数据) + * + * @param singleString 单个字符串 + * @return 字符串集合 + */ + public static List singleToStringList(String singleString) { + if (StrUtil.isBlank(singleString)) { + return new ArrayList<>(); + } + + // 如果已经是逗号分隔的格式,直接转换 + if (singleString.contains(SEPARATOR)) { + return toStringList(singleString); + } + + // 单个字符串,包装成集合 + List stringList = new ArrayList<>(); + stringList.add(singleString); + return stringList; + } + + /** + * 将字符串集合转换为单个字符串(兼容旧接口) + * 取第一个字符串,如果没有则返回null + * + * @param stringList 字符串集合 + * @return 第一个字符串或null + */ + public static String stringListToSingle(List stringList) { + if (stringList == null || stringList.isEmpty()) { + return null; + } + return stringList.get(0); + } + + /** + * 验证字符串集合是否有效 + * + * @param stringList 字符串集合 + * @return 是否有效 + */ + public static boolean isValidStringList(List stringList) { + if (stringList == null) { + return false; + } + + // 检查是否有空字符串 + return stringList.stream().allMatch(StrUtil::isNotBlank); + } + + /** + * 获取字符串集合的大小 + * + * @param stringList 字符串集合 + * @return 字符串数量 + */ + public static int getStringCount(List stringList) { + if (stringList == null) { + return 0; + } + return stringList.size(); + } + + /** + * 添加字符串到集合 + * + * @param stringList 原字符串集合 + * @param newString 新字符串 + * @return 新的字符串集合 + */ + public static List addString(List stringList, String newString) { + if (StrUtil.isBlank(newString)) { + return stringList != null ? new ArrayList<>(stringList) : new ArrayList<>(); + } + + List result = stringList != null ? new ArrayList<>(stringList) : new ArrayList<>(); + if (!result.contains(newString)) { + result.add(newString); + } + return result; + } + + /** + * 从集合中移除字符串 + * + * @param stringList 原字符串集合 + * @param stringToRemove 要移除的字符串 + * @return 新的字符串集合 + */ + public static List removeString(List stringList, String stringToRemove) { + if (stringList == null || StrUtil.isBlank(stringToRemove)) { + return stringList != null ? new ArrayList<>(stringList) : new ArrayList<>(); + } + + List result = new ArrayList<>(stringList); + result.remove(stringToRemove); + return result; + } +}