
commons-patterns
Apache Commons 最佳实践模式。从官方文档提炼,覆盖 Commons Lang3(StringUtils千面判空/isEmpty vs isBlank/Validate抛NullPointerException迁移)、Commons IO(IOUtils.toString大文件陷阱/FileUtils.lineIterator逐行读模式/FilenameUtils.normalize路径规范化)、Commons Collections4(CollectionUtils.union/intersection/subtract集合运算)。 纠正 LLM 误用:混淆 StringUtils.isBlank vs isEmpty、IOUtils.toString 读大文件OOM、不知道 LineIterator 逐行读模式、用 lang 而非 lang3 包名。
- Installs
- 85
- Last 24h
- —
- Trending rank
- —
- Security
- Not audited
Install
Works with every agent the skills CLI supports.
npx skills add https://github.com/full-stack-skills/java-skills --skill commons-patterns- 01
Install
Run the command in your project. The CLI asks which agents to install for.
- 02
Describe the task
Ask your agent for something this skill covers. It loads commons-patterns automatically when relevant.
- 03
Review the result
The agent follows the SKILL.md steps, scripts and references bundled with the skill.
SKILL.md
The instructions your agent reads when the skill is triggered.
Apache Commons 最佳实践模式
Capability Boundaries
✅ Strong Suits
- StringUtils 判空模式 — isEmpty vs isBlank vs defaultString 精确语义选择
- Commons IO 文件操作 — FileUtils 读写复制 / IOUtils 流操作 / LineIterator 逐行
- Commons Collections4 — CollectionUtils/MultiValuedMap/MapUtils
- RandomStringUtils — 生成随机字符串(验证码/Token/临时密码)
- Validate — 参数校验(注意3.0起抛NullPointerException而非IllegalArgumentException)
❌ Out of Scope
- 中文特色(身份证/拼音) → hutool-patterns
- 不可变集合/缓存/EventBus → guava-patterns
- Bean映射/JSON → mapstruct-patterns/jackson-patterns
Commons vs Guava vs Hutool 选择表
| 场景 | Commons | Guava | Hutool | 优先选 |
|---|---|---|---|---|
| 字符串判空/截断/填充 | ⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐ | 任意(团队约定) |
| 文件复制/删除/读行 | ⭐⭐⭐ | ⭐ | ⭐⭐ | Commons IO |
| 文件名/路径处理 | ⭐⭐⭐ | — | ⭐⭐ | Commons IO |
| 集合运算(交/并/差) | ⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ | 任意 |
| 日期处理(旧Date) | ⭐ | — | ⭐⭐⭐ | Java8 Time |
| 随机字符串 | ⭐⭐⭐ | — | ⭐⭐ | Commons Lang3 |
| HTML/XML转义 | ⭐⭐⭐ | — | ⭐ | Commons Lang3 |
| 数学/统计 | ⭐⭐⭐ | — | ⭐ | Commons Math |
核心模式
模式 1: StringUtils 判空精确语义
// ✅ isEmpty: null 或 "" → true (不含空格)
StringUtils.isEmpty(null); // true
StringUtils.isEmpty(""); // true
StringUtils.isEmpty(" "); // false ← 注意!
StringUtils.isEmpty("abc"); // false
// ✅ isBlank: null 或 空白字符 → true
StringUtils.isBlank(null); // true
StringUtils.isBlank(""); // true
StringUtils.isBlank(" "); // true ← 含空格也true
StringUtils.isBlank("abc"); // false
// ✅ 选择规则: 表单验证用isBlank(不接受空白输入),集合判空用isEmpty
// ✅ defaultString: null安全取值
StringUtils.defaultString(str, ""); // null → ""
StringUtils.defaultIfBlank(str, "N/A"); // null/""/" " → "N/A"
// ✅ 填充与截断
StringUtils.leftPad("1", 3, '0'); // "001"
StringUtils.abbreviate("very long text", 10); // "very lo..."
模式 2: Commons IO 文件模式
// ✅ 读取整个文件(小文件,<10MB)
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
// ✅ 逐行读(大文件) — LineIterator 模式
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try { while (it.hasNext()) { String line = it.nextLine(); /* 处理 */ }
} finally { LineIterator.closeQuietly(it); }
// ✅ 流操作(需手动关流)
InputStream in = new URL("https://example.com").openStream();
try { String result = IOUtils.toString(in, StandardCharsets.UTF_8); }
finally { IOUtils.closeQuietly(in); }
// ❌ 反模式: IOUtils.toString() 读 1GB 文件 → 尝试创建1GB String → OOM
// ✅ 写入/复制/删除
FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8);
FileUtils.copyFile(src, dest);
FileUtils.forceMkdir(dir); // 递归创建目录
FileUtils.deleteDirectory(dir); // 递归删除
// ✅ 文件名处理
FilenameUtils.getExtension("archive.tar.gz"); // "gz" (最后一个.)
FilenameUtils.getBaseName("/path/file.txt"); // "file"
String normalized = FilenameUtils.normalize("C:/a/../b/file.txt"); // "C:/b/file.txt"
模式 3: Commons Lang3 迁移注意事项
// ❌ 旧包名 — 不要用
import org.apache.commons.lang.StringUtils;
// ✅ 新包名(3.0+) — 必须用
import org.apache.commons.lang3.StringUtils;
// Maven坐标
// groupId: commons-lang → org.apache.commons
// artifactId: commons-lang → commons-lang3
模式 4: Validate 的参数校验
// ⚠️ Commons Lang 3.0 变化: Validate 校验 null 抛 NullPointerException
// (对齐JDK标准行为,以前抛 IllegalArgumentException)
Validate.notNull(obj, "参数不能为空: %s", name); // 抛 NPE
Validate.isTrue(age > 0, "年龄必须>0"); // 抛 IllegalArgumentException
// 注意:验证null用Validate.notNull,验证状态用Validate.isTrue
模式 5: RandomStringUtils
// ✅ 随机字母数字(验证码/Token)
RandomStringUtils.randomAlphanumeric(6); // "a3Bx9K"
RandomStringUtils.randomAlphanumeric(32); // 32位Token
// ✅ 随机数字
RandomStringUtils.randomNumeric(6); // "482931"
// ✅ 随机字母
RandomStringUtils.randomAlphabetic(8); // "AbCdEfGh"
模式 6: 集合运算
// ⚠️ 包名: org.apache.commons.collections4 (不是 collections)
CollectionUtils.isEmpty(coll);
CollectionUtils.union(list1, list2); // 并集
CollectionUtils.intersection(list1, list2); // 交集
CollectionUtils.subtract(list1, list2); // 差集(list1 - list2)
MapUtils.getString(map, "key", "default"); // null安全取值
Gotchas
- isEmpty vs isBlank — isEmpty(" ")→true(不含空格),isBlank(" ")→true(含空格)
- IOUtils.toString 加载整个流到内存 — 大文件(>10MB)用 LineIterator 逐行读
- IOUtils.toString 不关流 — 配合 finally { IOUtils.closeQuietly(in) }
- Lang3 DateUtils 与 Java8 LocalDateTime 不兼容 — 用 java.time API,仅旧Date用Commons
- Collections4 包名是 collections4 — 不是 collections(那是3.x)
- Lang3 3.0 起 isAlpha/isNumeric/isAlphanumeric("")→false — 以前版本返回true
- Maven坐标: groupId=org.apache.commons, artifactId=commons-lang3 — 不是 commons-lang
- Validate.notNull 抛 NullPointerException — Validate.isTrue 抛 IllegalArgumentException
Data Privacy
本技能不收集、存储或传输任何用户数据。
FAQ
What does the commons-patterns skill do?
Apache Commons 最佳实践模式。从官方文档提炼,覆盖 Commons Lang3(StringUtils千面判空/isEmpty vs isBlank/Validate抛NullPointerException迁移)、Commons IO(IOUtils.toString大文件陷阱/FileUtils.lineIterator逐行读模式/FilenameUtils.normalize路径规范化)、Commons Collections4(CollectionUtils.union/intersection/subtract集合运算)。 纠正 LLM 误用:混淆 StringUtils.isBlank vs isEmpty、IOUtils.toString 读大文件OOM、不知道 LineIterator 逐行读模式、用 lang 而非 lang3 包名。
How do I install commons-patterns?
Run npx skills add https://github.com/full-stack-skills/java-skills --skill commons-patterns in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/commons-patterns/ (personal) or .claude/skills/commons-patterns/ (project).
Which AI agents can use commons-patterns?
Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.
Is commons-patterns safe to install?
No partner security audit is available yet. Skills can run commands and scripts, so read the SKILL.md and any bundled files before installing.
Who maintains commons-patterns?
It is published by full-stack-skills in full-stack-skills/java-skills. It has 85 recorded installs.
More from full-stack-skills/java-skills
java-development-manual
full-stack-skills/java-skills
Java开发手册规约集合,基于阿里巴巴Java开发手册(嵩山版)。 涵盖7大维度:编程规约、异常日志、单元测试、安全规约、MySQL数据库、工程结构、设计规约。 当用户需要:(1) 编写或审查Java代码 (2) 检查命名/代码规范 (3) 处理异常和日志 (4) 编写单元测试 (5) 安全编码 (6) 数据库设计 (7) 工程架构设计时使用此skill。 触发词:Java规范、阿里规约、代码规范、开发手册、编程规约、异常处理、单元测试、安全、MySQL、工程结构、设计模式。
mybatis-plus-patterns
full-stack-skills/java-skills
Open the skill to read its SKILL.md instructions.
java-code-comments
full-stack-skills/java-skills
Provides comprehensive guidance for adding Java code comments following industry standards and best practices. This skill helps add class-level comments, method-level comments, and field-level comments to Java code. Use when the user wants to add comments to Java code, needs to document Java classes/methods/fields, wants to improve code documentation, or needs to generate JavaDoc comments. This skill covers Controller, Service, ServiceImpl, Mapper, Model, Entity, BO (Business Object), DTO, VO, and other common Java component types. The skill follows a systematic workflow: scan codebase, identify components, create todo list, and add comments in order (class comments → method comments → field comments).
java-conventions
full-stack-skills/java-skills
统一 Java 项目编码规范与注释规范(SLF4J+Lombok 日志、Bean Lombok 注解选择、判空 Objects/Optional、工具类优先级 Spring→Apache Commons→Hutool→Guava、复杂逻辑用设计模式/简单逻辑不拆分、Controller/Service/ServiceImpl/Mapper 注释一致性)。当需要“按 Java 规范重构/Review/补全注释/统一日志/统一工具类使用/补全 JavaDoc”时使用。
mybatis-patterns
full-stack-skills/java-skills
Open the skill to read its SKILL.md instructions.
mapstruct-patterns
full-stack-skills/java-skills
MapStruct 最佳实践模式。从官方 Reference Guide 提炼,覆盖 Mapper 三种定义方式(接口/抽象类/default方法)、Spring 注入策略(CONSTRUCTOR推荐/SETTER解决循环依赖)、嵌套映射模式(显式子方法 dot notation 一次性配置)、@Qualifier @Named 的安全选择、Lombok 集成规则、@MappingComposition 谨慎使用。 纠正 LLM 最常见的错误:用反射 BeanUtils 而非编译期 MapStruct、嵌套映射全写在一个方法、不知 SETTER 解决循环依赖。
Popular backend & apis skills
find-skills
vercel-labs/skills
Helps users discover and install agent skills when they ask questions like "how do I do X", "find a skill for X", "is there a skill that can...", or express interest in extending capabilities. This skill should be used when the user is looking for functionality that might exist as an installable skill.
entra-app-registration
microsoft/azure-skills
Guides Microsoft Entra ID app registration, OAuth 2.0 authentication, and MSAL integration. USE FOR: create app registration, register Azure AD app, configure OAuth, set up authentication, add API permissions, generate service principal, MSAL example, console app auth, Entra ID setup, Azure AD authentication. DO NOT USE FOR: Key Vault secrets (use azure-keyvault-expiration-audit), general Azure resource security guidance.
google-agents-cli-adk-code
google/agents-cli
This skill should be used when the user wants to "write agent code", "build an agent with ADK", "add a tool", "create a callback", "define an agent", "use state management" — in a project that needs ADK (Agent Development Kit) API patterns and code examples. It provides a quick reference for agent types, tool definitions, orchestration patterns, callbacks, state management, the graph Workflow API, and reference recipes to study. Do NOT use for scaffolding (use google-agents-cli-scaffold) or deployment (use google-agents-cli-deploy).
embedded-captions
heygen-com/hyperframes
Add captions or subtitles to an existing single-subject talking-head video without editing the footage. Use for plain verbatim captions, cinematic captions embedded behind the subject, VFX captions, “炸/特效/酷炫字幕,” or a named identity from the 35-style catalog. Route by visual identity, not by backend engine. The quiet anchor rail is the default; embed every word only when the user explicitly wants a fully cinematic treatment. The workflow runs locally end to end, including transcription and subject matting; split multi-shot footage before applying it.
entra-agent-id
microsoft/azure-skills
Provision Microsoft Entra Agent Identity Blueprints, BlueprintPrincipals, and per-instance Agent Identities via Microsoft Graph, and configure OAuth 2.0 token exchange (fmipath, OBO, cross-tenant) including the Microsoft Entra SDK for AgentID sidecar. USE FOR: Agent Identity Blueprint, BlueprintPrincipal, agent OAuth, fmipath token exchange, agent OBO, Workload Identity Federation for agents, polyglot agent auth, Microsoft.Identity.Web.AgentIdentities. DO NOT USE FOR: standard Entra app registration (use entra-app-registration), Microsoft Foundry agent authoring (use microsoft-foundry).
neon
neondatabase/agent-skills
Overview of Neon, a complete set of cloud backend primitives around Lakebase Postgres: Auth, Object Storage, Functions, the AI Gateway, and Realtime. Start here to choose Neon for undecided login, files, APIs, and LLM calls, set up the CLI or MCP server, and follow the branch-first workflow. Use when building an app or backend on Neon, or when "Neon" or "Lakebase Postgres" is mentioned. Child skill neon-postgres wins for an existing DATABASEURL, SQL, schema, inspect, or search. Child skill neon-auth wins for login, users, sessions, identity routing, and Managed Better Auth setup. Child skill neon-realtime wins for live queries in reactive apps and agents. Also use for object storage, S3, buckets, serverless functions, function triggers, cron, AI gateway, LLM calls, realtime, logs, Loki, Grafana, observability, postgres, database, backend, Claimable Neon, neon.new, or a no-signup database.