@booklib/core 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (374) hide show
  1. package/.cursor/rules/booklib-standards.mdc +40 -0
  2. package/.gemini/context.md +372 -0
  3. package/AGENTS.md +166 -0
  4. package/CHANGELOG.md +226 -0
  5. package/CLAUDE.md +81 -0
  6. package/CODE_OF_CONDUCT.md +31 -0
  7. package/CONTRIBUTING.md +304 -0
  8. package/LICENSE +21 -0
  9. package/PLAN.md +28 -0
  10. package/README.ja.md +198 -0
  11. package/README.ko.md +198 -0
  12. package/README.md +503 -0
  13. package/README.pt-BR.md +198 -0
  14. package/README.uk.md +241 -0
  15. package/README.zh-CN.md +198 -0
  16. package/SECURITY.md +9 -0
  17. package/agents/architecture-reviewer.md +136 -0
  18. package/agents/booklib-reviewer.md +90 -0
  19. package/agents/data-reviewer.md +107 -0
  20. package/agents/jvm-reviewer.md +146 -0
  21. package/agents/python-reviewer.md +128 -0
  22. package/agents/rust-reviewer.md +115 -0
  23. package/agents/ts-reviewer.md +110 -0
  24. package/agents/ui-reviewer.md +117 -0
  25. package/assets/logo.svg +36 -0
  26. package/bin/booklib-mcp.js +304 -0
  27. package/bin/booklib.js +1705 -0
  28. package/bin/skills.cjs +1292 -0
  29. package/booklib-router.mdc +36 -0
  30. package/booklib.config.json +19 -0
  31. package/commands/animation-at-work.md +10 -0
  32. package/commands/clean-code-reviewer.md +10 -0
  33. package/commands/data-intensive-patterns.md +10 -0
  34. package/commands/data-pipelines.md +10 -0
  35. package/commands/design-patterns.md +10 -0
  36. package/commands/domain-driven-design.md +10 -0
  37. package/commands/effective-java.md +10 -0
  38. package/commands/effective-kotlin.md +10 -0
  39. package/commands/effective-python.md +10 -0
  40. package/commands/effective-typescript.md +10 -0
  41. package/commands/kotlin-in-action.md +10 -0
  42. package/commands/lean-startup.md +10 -0
  43. package/commands/microservices-patterns.md +10 -0
  44. package/commands/programming-with-rust.md +10 -0
  45. package/commands/refactoring-ui.md +10 -0
  46. package/commands/rust-in-action.md +10 -0
  47. package/commands/skill-router.md +10 -0
  48. package/commands/spring-boot-in-action.md +10 -0
  49. package/commands/storytelling-with-data.md +10 -0
  50. package/commands/system-design-interview.md +10 -0
  51. package/commands/using-asyncio-python.md +10 -0
  52. package/commands/web-scraping-python.md +10 -0
  53. package/community/registry.json +1616 -0
  54. package/hooks/hooks.json +23 -0
  55. package/hooks/posttooluse-capture.mjs +67 -0
  56. package/hooks/suggest.js +153 -0
  57. package/lib/agent-behaviors.js +40 -0
  58. package/lib/agent-detector.js +96 -0
  59. package/lib/config-loader.js +39 -0
  60. package/lib/conflict-resolver.js +148 -0
  61. package/lib/context-builder.js +574 -0
  62. package/lib/discovery-engine.js +298 -0
  63. package/lib/doctor/hook-installer.js +83 -0
  64. package/lib/doctor/usage-tracker.js +87 -0
  65. package/lib/engine/ai-features.js +253 -0
  66. package/lib/engine/auditor.js +103 -0
  67. package/lib/engine/bm25-index.js +178 -0
  68. package/lib/engine/capture.js +120 -0
  69. package/lib/engine/corrections.js +198 -0
  70. package/lib/engine/doctor.js +195 -0
  71. package/lib/engine/graph-injector.js +137 -0
  72. package/lib/engine/graph.js +161 -0
  73. package/lib/engine/handoff.js +405 -0
  74. package/lib/engine/indexer.js +242 -0
  75. package/lib/engine/parser.js +53 -0
  76. package/lib/engine/query-expander.js +42 -0
  77. package/lib/engine/reranker.js +40 -0
  78. package/lib/engine/rrf.js +59 -0
  79. package/lib/engine/scanner.js +151 -0
  80. package/lib/engine/searcher.js +139 -0
  81. package/lib/engine/session-coordinator.js +306 -0
  82. package/lib/engine/session-manager.js +429 -0
  83. package/lib/engine/synthesizer.js +70 -0
  84. package/lib/installer.js +70 -0
  85. package/lib/instinct-block.js +33 -0
  86. package/lib/mcp-config-writer.js +88 -0
  87. package/lib/paths.js +57 -0
  88. package/lib/profiles/design.md +19 -0
  89. package/lib/profiles/general.md +16 -0
  90. package/lib/profiles/research-analysis.md +22 -0
  91. package/lib/profiles/software-development.md +23 -0
  92. package/lib/profiles/writing-content.md +19 -0
  93. package/lib/project-initializer.js +916 -0
  94. package/lib/registry/skills.js +102 -0
  95. package/lib/registry-searcher.js +99 -0
  96. package/lib/rules/rules-manager.js +169 -0
  97. package/lib/skill-fetcher.js +333 -0
  98. package/lib/well-known-builder.js +70 -0
  99. package/lib/wizard/index.js +404 -0
  100. package/lib/wizard/integration-detector.js +41 -0
  101. package/lib/wizard/project-detector.js +100 -0
  102. package/lib/wizard/prompt.js +156 -0
  103. package/lib/wizard/registry-embeddings.js +107 -0
  104. package/lib/wizard/skill-recommender.js +69 -0
  105. package/llms-full.txt +254 -0
  106. package/llms.txt +70 -0
  107. package/package.json +45 -0
  108. package/research-reports/2026-04-01-current-architecture.md +160 -0
  109. package/research-reports/IDEAS.md +93 -0
  110. package/rules/common/clean-code.md +42 -0
  111. package/rules/java/effective-java.md +42 -0
  112. package/rules/kotlin/effective-kotlin.md +37 -0
  113. package/rules/python/effective-python.md +38 -0
  114. package/rules/rust/rust.md +37 -0
  115. package/rules/typescript/effective-typescript.md +42 -0
  116. package/scripts/gen-llms-full.mjs +36 -0
  117. package/scripts/gen-og.mjs +142 -0
  118. package/scripts/validate-frontmatter.js +25 -0
  119. package/skills/animation-at-work/SKILL.md +270 -0
  120. package/skills/animation-at-work/assets/example_asset.txt +1 -0
  121. package/skills/animation-at-work/evals/evals.json +44 -0
  122. package/skills/animation-at-work/evals/results.json +13 -0
  123. package/skills/animation-at-work/examples/after.md +64 -0
  124. package/skills/animation-at-work/examples/before.md +35 -0
  125. package/skills/animation-at-work/references/api_reference.md +369 -0
  126. package/skills/animation-at-work/references/review-checklist.md +79 -0
  127. package/skills/animation-at-work/scripts/audit_animations.py +295 -0
  128. package/skills/animation-at-work/scripts/example.py +1 -0
  129. package/skills/clean-code-reviewer/SKILL.md +444 -0
  130. package/skills/clean-code-reviewer/audit.json +35 -0
  131. package/skills/clean-code-reviewer/evals/evals.json +185 -0
  132. package/skills/clean-code-reviewer/evals/results.json +13 -0
  133. package/skills/clean-code-reviewer/examples/after.md +48 -0
  134. package/skills/clean-code-reviewer/examples/before.md +33 -0
  135. package/skills/clean-code-reviewer/references/api_reference.md +158 -0
  136. package/skills/clean-code-reviewer/references/practices-catalog.md +282 -0
  137. package/skills/clean-code-reviewer/references/review-checklist.md +254 -0
  138. package/skills/clean-code-reviewer/scripts/pre-review.py +206 -0
  139. package/skills/data-intensive-patterns/SKILL.md +267 -0
  140. package/skills/data-intensive-patterns/assets/example_asset.txt +1 -0
  141. package/skills/data-intensive-patterns/evals/evals.json +54 -0
  142. package/skills/data-intensive-patterns/evals/results.json +13 -0
  143. package/skills/data-intensive-patterns/examples/after.md +61 -0
  144. package/skills/data-intensive-patterns/examples/before.md +38 -0
  145. package/skills/data-intensive-patterns/references/api_reference.md +34 -0
  146. package/skills/data-intensive-patterns/references/patterns-catalog.md +551 -0
  147. package/skills/data-intensive-patterns/references/review-checklist.md +193 -0
  148. package/skills/data-intensive-patterns/scripts/adr.py +213 -0
  149. package/skills/data-intensive-patterns/scripts/example.py +1 -0
  150. package/skills/data-pipelines/SKILL.md +259 -0
  151. package/skills/data-pipelines/assets/example_asset.txt +1 -0
  152. package/skills/data-pipelines/evals/evals.json +45 -0
  153. package/skills/data-pipelines/evals/results.json +13 -0
  154. package/skills/data-pipelines/examples/after.md +97 -0
  155. package/skills/data-pipelines/examples/before.md +37 -0
  156. package/skills/data-pipelines/references/api_reference.md +301 -0
  157. package/skills/data-pipelines/references/review-checklist.md +181 -0
  158. package/skills/data-pipelines/scripts/example.py +1 -0
  159. package/skills/data-pipelines/scripts/new_pipeline.py +444 -0
  160. package/skills/design-patterns/SKILL.md +271 -0
  161. package/skills/design-patterns/assets/example_asset.txt +1 -0
  162. package/skills/design-patterns/evals/evals.json +46 -0
  163. package/skills/design-patterns/evals/results.json +13 -0
  164. package/skills/design-patterns/examples/after.md +52 -0
  165. package/skills/design-patterns/examples/before.md +29 -0
  166. package/skills/design-patterns/references/api_reference.md +1 -0
  167. package/skills/design-patterns/references/patterns-catalog.md +726 -0
  168. package/skills/design-patterns/references/review-checklist.md +173 -0
  169. package/skills/design-patterns/scripts/example.py +1 -0
  170. package/skills/design-patterns/scripts/scaffold.py +807 -0
  171. package/skills/domain-driven-design/SKILL.md +142 -0
  172. package/skills/domain-driven-design/assets/example_asset.txt +1 -0
  173. package/skills/domain-driven-design/evals/evals.json +48 -0
  174. package/skills/domain-driven-design/evals/results.json +13 -0
  175. package/skills/domain-driven-design/examples/after.md +80 -0
  176. package/skills/domain-driven-design/examples/before.md +43 -0
  177. package/skills/domain-driven-design/references/api_reference.md +1 -0
  178. package/skills/domain-driven-design/references/patterns-catalog.md +545 -0
  179. package/skills/domain-driven-design/references/review-checklist.md +158 -0
  180. package/skills/domain-driven-design/scripts/example.py +1 -0
  181. package/skills/domain-driven-design/scripts/scaffold.py +421 -0
  182. package/skills/effective-java/SKILL.md +227 -0
  183. package/skills/effective-java/assets/example_asset.txt +1 -0
  184. package/skills/effective-java/evals/evals.json +46 -0
  185. package/skills/effective-java/evals/results.json +13 -0
  186. package/skills/effective-java/examples/after.md +83 -0
  187. package/skills/effective-java/examples/before.md +37 -0
  188. package/skills/effective-java/references/api_reference.md +1 -0
  189. package/skills/effective-java/references/items-catalog.md +955 -0
  190. package/skills/effective-java/references/review-checklist.md +216 -0
  191. package/skills/effective-java/scripts/checkstyle_setup.py +211 -0
  192. package/skills/effective-java/scripts/example.py +1 -0
  193. package/skills/effective-kotlin/SKILL.md +271 -0
  194. package/skills/effective-kotlin/assets/example_asset.txt +1 -0
  195. package/skills/effective-kotlin/audit.json +29 -0
  196. package/skills/effective-kotlin/evals/evals.json +45 -0
  197. package/skills/effective-kotlin/evals/results.json +13 -0
  198. package/skills/effective-kotlin/examples/after.md +36 -0
  199. package/skills/effective-kotlin/examples/before.md +38 -0
  200. package/skills/effective-kotlin/references/api_reference.md +1 -0
  201. package/skills/effective-kotlin/references/practices-catalog.md +1228 -0
  202. package/skills/effective-kotlin/references/review-checklist.md +126 -0
  203. package/skills/effective-kotlin/scripts/example.py +1 -0
  204. package/skills/effective-python/SKILL.md +441 -0
  205. package/skills/effective-python/evals/evals.json +44 -0
  206. package/skills/effective-python/evals/results.json +13 -0
  207. package/skills/effective-python/examples/after.md +56 -0
  208. package/skills/effective-python/examples/before.md +40 -0
  209. package/skills/effective-python/ref-01-pythonic-thinking.md +202 -0
  210. package/skills/effective-python/ref-02-lists-and-dicts.md +146 -0
  211. package/skills/effective-python/ref-03-functions.md +186 -0
  212. package/skills/effective-python/ref-04-comprehensions-generators.md +211 -0
  213. package/skills/effective-python/ref-05-classes-interfaces.md +188 -0
  214. package/skills/effective-python/ref-06-metaclasses-attributes.md +209 -0
  215. package/skills/effective-python/ref-07-concurrency.md +213 -0
  216. package/skills/effective-python/ref-08-robustness-performance.md +248 -0
  217. package/skills/effective-python/ref-09-testing-debugging.md +253 -0
  218. package/skills/effective-python/ref-10-collaboration.md +175 -0
  219. package/skills/effective-python/references/api_reference.md +218 -0
  220. package/skills/effective-python/references/practices-catalog.md +483 -0
  221. package/skills/effective-python/references/review-checklist.md +190 -0
  222. package/skills/effective-python/scripts/lint.py +173 -0
  223. package/skills/effective-typescript/SKILL.md +262 -0
  224. package/skills/effective-typescript/audit.json +29 -0
  225. package/skills/effective-typescript/evals/evals.json +37 -0
  226. package/skills/effective-typescript/evals/results.json +13 -0
  227. package/skills/effective-typescript/examples/after.md +70 -0
  228. package/skills/effective-typescript/examples/before.md +47 -0
  229. package/skills/effective-typescript/references/api_reference.md +118 -0
  230. package/skills/effective-typescript/references/practices-catalog.md +371 -0
  231. package/skills/effective-typescript/scripts/review.py +169 -0
  232. package/skills/kotlin-in-action/SKILL.md +261 -0
  233. package/skills/kotlin-in-action/assets/example_asset.txt +1 -0
  234. package/skills/kotlin-in-action/evals/evals.json +43 -0
  235. package/skills/kotlin-in-action/evals/results.json +13 -0
  236. package/skills/kotlin-in-action/examples/after.md +53 -0
  237. package/skills/kotlin-in-action/examples/before.md +39 -0
  238. package/skills/kotlin-in-action/references/api_reference.md +1 -0
  239. package/skills/kotlin-in-action/references/practices-catalog.md +436 -0
  240. package/skills/kotlin-in-action/references/review-checklist.md +204 -0
  241. package/skills/kotlin-in-action/scripts/example.py +1 -0
  242. package/skills/kotlin-in-action/scripts/setup_detekt.py +224 -0
  243. package/skills/lean-startup/SKILL.md +160 -0
  244. package/skills/lean-startup/assets/example_asset.txt +1 -0
  245. package/skills/lean-startup/evals/evals.json +43 -0
  246. package/skills/lean-startup/evals/results.json +13 -0
  247. package/skills/lean-startup/examples/after.md +80 -0
  248. package/skills/lean-startup/examples/before.md +34 -0
  249. package/skills/lean-startup/references/api_reference.md +319 -0
  250. package/skills/lean-startup/references/review-checklist.md +137 -0
  251. package/skills/lean-startup/scripts/example.py +1 -0
  252. package/skills/lean-startup/scripts/new_experiment.py +286 -0
  253. package/skills/microservices-patterns/SKILL.md +384 -0
  254. package/skills/microservices-patterns/evals/evals.json +45 -0
  255. package/skills/microservices-patterns/evals/results.json +13 -0
  256. package/skills/microservices-patterns/examples/after.md +69 -0
  257. package/skills/microservices-patterns/examples/before.md +40 -0
  258. package/skills/microservices-patterns/references/patterns-catalog.md +391 -0
  259. package/skills/microservices-patterns/references/review-checklist.md +169 -0
  260. package/skills/microservices-patterns/scripts/new_service.py +583 -0
  261. package/skills/programming-with-rust/SKILL.md +209 -0
  262. package/skills/programming-with-rust/evals/evals.json +37 -0
  263. package/skills/programming-with-rust/evals/results.json +13 -0
  264. package/skills/programming-with-rust/examples/after.md +107 -0
  265. package/skills/programming-with-rust/examples/before.md +59 -0
  266. package/skills/programming-with-rust/references/api_reference.md +152 -0
  267. package/skills/programming-with-rust/references/practices-catalog.md +335 -0
  268. package/skills/programming-with-rust/scripts/review.py +142 -0
  269. package/skills/refactoring-ui/SKILL.md +362 -0
  270. package/skills/refactoring-ui/assets/example_asset.txt +1 -0
  271. package/skills/refactoring-ui/evals/evals.json +45 -0
  272. package/skills/refactoring-ui/evals/results.json +13 -0
  273. package/skills/refactoring-ui/examples/after.md +85 -0
  274. package/skills/refactoring-ui/examples/before.md +58 -0
  275. package/skills/refactoring-ui/references/api_reference.md +355 -0
  276. package/skills/refactoring-ui/references/review-checklist.md +114 -0
  277. package/skills/refactoring-ui/scripts/audit_css.py +250 -0
  278. package/skills/refactoring-ui/scripts/example.py +1 -0
  279. package/skills/rust-in-action/SKILL.md +350 -0
  280. package/skills/rust-in-action/evals/evals.json +38 -0
  281. package/skills/rust-in-action/evals/results.json +13 -0
  282. package/skills/rust-in-action/examples/after.md +156 -0
  283. package/skills/rust-in-action/examples/before.md +56 -0
  284. package/skills/rust-in-action/references/practices-catalog.md +346 -0
  285. package/skills/rust-in-action/scripts/review.py +147 -0
  286. package/skills/skill-router/SKILL.md +186 -0
  287. package/skills/skill-router/evals/evals.json +38 -0
  288. package/skills/skill-router/evals/results.json +13 -0
  289. package/skills/skill-router/examples/after.md +63 -0
  290. package/skills/skill-router/examples/before.md +39 -0
  291. package/skills/skill-router/references/api_reference.md +24 -0
  292. package/skills/skill-router/references/routing-heuristics.md +89 -0
  293. package/skills/skill-router/references/skill-catalog.md +174 -0
  294. package/skills/skill-router/scripts/route.py +266 -0
  295. package/skills/spring-boot-in-action/SKILL.md +340 -0
  296. package/skills/spring-boot-in-action/evals/evals.json +39 -0
  297. package/skills/spring-boot-in-action/evals/results.json +13 -0
  298. package/skills/spring-boot-in-action/examples/after.md +185 -0
  299. package/skills/spring-boot-in-action/examples/before.md +84 -0
  300. package/skills/spring-boot-in-action/references/practices-catalog.md +403 -0
  301. package/skills/spring-boot-in-action/scripts/review.py +184 -0
  302. package/skills/storytelling-with-data/SKILL.md +241 -0
  303. package/skills/storytelling-with-data/assets/example_asset.txt +1 -0
  304. package/skills/storytelling-with-data/evals/evals.json +47 -0
  305. package/skills/storytelling-with-data/evals/results.json +13 -0
  306. package/skills/storytelling-with-data/examples/after.md +50 -0
  307. package/skills/storytelling-with-data/examples/before.md +33 -0
  308. package/skills/storytelling-with-data/references/api_reference.md +379 -0
  309. package/skills/storytelling-with-data/references/review-checklist.md +111 -0
  310. package/skills/storytelling-with-data/scripts/chart_review.py +301 -0
  311. package/skills/storytelling-with-data/scripts/example.py +1 -0
  312. package/skills/system-design-interview/SKILL.md +233 -0
  313. package/skills/system-design-interview/assets/example_asset.txt +1 -0
  314. package/skills/system-design-interview/evals/evals.json +46 -0
  315. package/skills/system-design-interview/evals/results.json +13 -0
  316. package/skills/system-design-interview/examples/after.md +94 -0
  317. package/skills/system-design-interview/examples/before.md +27 -0
  318. package/skills/system-design-interview/references/api_reference.md +582 -0
  319. package/skills/system-design-interview/references/review-checklist.md +201 -0
  320. package/skills/system-design-interview/scripts/example.py +1 -0
  321. package/skills/system-design-interview/scripts/new_design.py +421 -0
  322. package/skills/using-asyncio-python/SKILL.md +290 -0
  323. package/skills/using-asyncio-python/assets/example_asset.txt +1 -0
  324. package/skills/using-asyncio-python/evals/evals.json +43 -0
  325. package/skills/using-asyncio-python/evals/results.json +13 -0
  326. package/skills/using-asyncio-python/examples/after.md +68 -0
  327. package/skills/using-asyncio-python/examples/before.md +39 -0
  328. package/skills/using-asyncio-python/references/api_reference.md +267 -0
  329. package/skills/using-asyncio-python/references/review-checklist.md +149 -0
  330. package/skills/using-asyncio-python/scripts/check_blocking.py +270 -0
  331. package/skills/using-asyncio-python/scripts/example.py +1 -0
  332. package/skills/web-scraping-python/SKILL.md +280 -0
  333. package/skills/web-scraping-python/assets/example_asset.txt +1 -0
  334. package/skills/web-scraping-python/evals/evals.json +46 -0
  335. package/skills/web-scraping-python/evals/results.json +13 -0
  336. package/skills/web-scraping-python/examples/after.md +109 -0
  337. package/skills/web-scraping-python/examples/before.md +40 -0
  338. package/skills/web-scraping-python/references/api_reference.md +393 -0
  339. package/skills/web-scraping-python/references/review-checklist.md +163 -0
  340. package/skills/web-scraping-python/scripts/example.py +1 -0
  341. package/skills/web-scraping-python/scripts/new_scraper.py +231 -0
  342. package/skills/writing-plans/audit.json +34 -0
  343. package/tests/agent-detector.test.js +83 -0
  344. package/tests/corrections.test.js +245 -0
  345. package/tests/doctor/hook-installer.test.js +72 -0
  346. package/tests/doctor/usage-tracker.test.js +140 -0
  347. package/tests/engine/benchmark-eval.test.js +31 -0
  348. package/tests/engine/bm25-index.test.js +85 -0
  349. package/tests/engine/capture-command.test.js +35 -0
  350. package/tests/engine/capture.test.js +17 -0
  351. package/tests/engine/graph-augmented-search.test.js +107 -0
  352. package/tests/engine/graph-injector.test.js +44 -0
  353. package/tests/engine/graph.test.js +216 -0
  354. package/tests/engine/hybrid-searcher.test.js +74 -0
  355. package/tests/engine/indexer-bm25.test.js +37 -0
  356. package/tests/engine/mcp-tools.test.js +73 -0
  357. package/tests/engine/project-initializer-mcp.test.js +99 -0
  358. package/tests/engine/query-expander.test.js +36 -0
  359. package/tests/engine/reranker.test.js +51 -0
  360. package/tests/engine/rrf.test.js +49 -0
  361. package/tests/engine/srag-prefix.test.js +47 -0
  362. package/tests/instinct-block.test.js +23 -0
  363. package/tests/mcp-config-writer.test.js +60 -0
  364. package/tests/project-initializer-new-agents.test.js +48 -0
  365. package/tests/rules/rules-manager.test.js +230 -0
  366. package/tests/well-known-builder.test.js +40 -0
  367. package/tests/wizard/integration-detector.test.js +31 -0
  368. package/tests/wizard/project-detector.test.js +51 -0
  369. package/tests/wizard/prompt-session.test.js +61 -0
  370. package/tests/wizard/prompt.test.js +16 -0
  371. package/tests/wizard/registry-embeddings.test.js +35 -0
  372. package/tests/wizard/skill-recommender.test.js +34 -0
  373. package/tests/wizard/slot-count.test.js +25 -0
  374. package/vercel.json +21 -0
@@ -0,0 +1,227 @@
1
+ ---
2
+ name: effective-java
3
+ version: "1.0"
4
+ license: MIT
5
+ tags: [java, jvm, oop]
6
+ description: >
7
+ Generate and review Java code using patterns and best practices from Joshua Bloch's
8
+ "Effective Java" (3rd Edition). Use this skill whenever the user asks about Java best
9
+ practices, API design, object creation patterns, generics, enums, lambdas, streams,
10
+ concurrency, serialization, method design, exception handling, or writing clean,
11
+ maintainable Java code. Trigger on phrases like "Effective Java", "Java best practices",
12
+ "builder pattern", "static factory", "defensive copy", "immutable class", "enum type",
13
+ "generics", "bounded wildcard", "PECS", "stream pipeline", "optional", "thread safety",
14
+ "serialization proxy", "checked exception", "try-with-resources", "composition over
15
+ inheritance", "method reference", "functional interface", or "Java API design."
16
+ ---
17
+
18
+ # Effective Java Skill
19
+
20
+ You are an expert Java architect grounded in the 90 items from Joshua Bloch's
21
+ *Effective Java* (3rd Edition). You help developers in two modes:
22
+
23
+ 1. **Code Generation** — Produce well-structured Java code following Effective Java principles
24
+ 2. **Code Review** — Analyze existing Java code and recommend improvements
25
+
26
+ ## How to Decide Which Mode
27
+
28
+ - If the user asks you to *build*, *create*, *generate*, *implement*, or *scaffold* something → **Code Generation**
29
+ - If the user asks you to *review*, *check*, *improve*, *audit*, or *critique* code → **Code Review**
30
+ - If ambiguous, ask briefly which mode they'd prefer
31
+
32
+ ---
33
+
34
+ ## Mode 1: Code Generation
35
+
36
+ When generating Java code, follow this decision flow:
37
+
38
+ ### Step 1 — Understand the Requirements
39
+
40
+ Ask (or infer from context) what the component needs:
41
+
42
+ - **Object creation** — How should instances be created? (Factory, Builder, Singleton, DI)
43
+ - **Mutability** — Does the class need to be mutable or can it be immutable?
44
+ - **Inheritance model** — Composition, interface-based, or class hierarchy?
45
+ - **Concurrency** — Will this be accessed from multiple threads?
46
+ - **API surface** — Is this internal or part of a public API?
47
+
48
+ ### Step 2 — Select the Right Patterns
49
+
50
+ Read `references/items-catalog.md` for full item details. Quick decision guide:
51
+
52
+ | Problem | Items to Apply |
53
+ |---------|---------------|
54
+ | How to create objects? | Item 1 (static factories), Item 2 (Builder), Item 3 (Singleton), Item 5 (DI) |
55
+ | How to design an immutable class? | Item 17 (minimize mutability), Item 50 (defensive copies) |
56
+ | How to model types? | Item 20 (interfaces over abstract classes), Item 23 (class hierarchies over tagged classes) |
57
+ | How to use generics safely? | Item 26 (no raw types), Item 31 (bounded wildcards / PECS), Item 28 (lists over arrays) |
58
+ | How to use enums effectively? | Item 34 (enums over int constants), Item 37 (EnumSet), Item 38 (EnumMap) |
59
+ | How to use lambdas and streams? | Item 42 (lambdas over anon classes), Item 43 (method refs), Item 45 (streams judiciously) |
60
+ | How to design methods? | Item 49 (validate params), Item 50 (defensive copies), Item 51 (design signatures carefully) |
61
+ | How to handle errors? | Item 69 (exceptions for exceptional conditions), Item 71 (avoid unnecessary checked), Item 73 (translate exceptions) |
62
+ | How to handle concurrency? | Item 78 (synchronize shared mutable data), Item 79 (avoid excessive sync), Item 81 (concurrency utilities) |
63
+ | How to handle serialization? | Item 85 (prefer alternatives), Item 90 (serialization proxies) |
64
+
65
+ ### Step 3 — Generate the Code
66
+
67
+ <core_principles>
68
+ Follow these principles when writing Java code:
69
+
70
+ - **Static factories over constructors** — Use `of`, `from`, `valueOf`, `create` naming. Return interface types. Cache instances when possible (Item 1)
71
+ - **Builder for many parameters** — Any constructor with more than 3-4 parameters should use the Builder pattern with fluent API (Item 2). When the class has many optional parameters, Builder is strongly preferred over a Java Record, which requires all components and doesn't provide fluent optional-field configuration.
72
+ - **Immutable by default** — Make fields `final`, make classes `final`, no setters, defensive copies in constructors and accessors (Item 17)
73
+ - **Composition over inheritance** — Wrap existing classes with forwarding methods instead of extending them. Use the decorator pattern (Item 18)
74
+ - **Program to interfaces** — Declare variables, parameters, and return types as interfaces, not concrete classes (Item 64)
75
+ - **Generics everywhere** — No raw types. Use `<? extends T>` for producers, `<? super T>` for consumers (PECS). Prefer `List<E>` over `E[]` (Items 26, 28, 31)
76
+ - **Enums over constants** — Never `public static final int`. Use enums with behavior, strategy enum pattern, and EnumSet/EnumMap (Items 34, 36, 37)
77
+ - **Lambdas and method references** — Prefer lambdas to anonymous classes, method references to lambdas when clearer. Use standard functional interfaces (Items 42-44)
78
+ - **Streams judiciously** — Don't overuse. Keep side-effect-free. Return `Collection` over `Stream` from APIs. Be careful with parallel streams (Items 45-48)
79
+ - **Defensive programming** — Validate parameters with `Objects.requireNonNull`, use `@Nullable` annotations, return empty collections not null, use Optional for return types (Items 49, 54, 55)
80
+ - **Exceptions done right** — Use unchecked for programming errors, checked for recoverable conditions. Translate exceptions at abstraction boundaries. Include failure-capture info (Items 69-77)
81
+ - **Thread safety by design** — Document thread safety levels. Prefer concurrency utilities (`ConcurrentHashMap`, `CountDownLatch`) over `wait`/`notify`. Use lazy initialization only when needed (Items 78-84)
82
+ - **Avoid Java serialization** — Use JSON, protobuf, or other formats. If you must use Serializable, use serialization proxies (Items 85-90)
83
+ </core_principles>
84
+
85
+ When generating code, produce:
86
+
87
+ 1. **Class design** — Access levels, mutability, type hierarchy
88
+ 2. **Object creation** — Factory methods, builders, dependency injection
89
+ 3. **API methods** — Parameter validation, return types, documentation
90
+ 4. **Error handling** — Exception hierarchy, translation, failure atomicity
91
+ 5. **Concurrency model** — Thread safety annotations, synchronization strategy
92
+
93
+ ### Code Generation Examples
94
+
95
+ <examples>
96
+ <example id="1" title="Immutable Value Class with Builder">
97
+ ```
98
+ User: "Create a class to represent a nutritional facts label"
99
+
100
+ You should generate:
101
+ - Immutable class with private final fields (Item 17)
102
+ - Builder pattern with fluent API for optional params (Item 2)
103
+ - Static factory method NutritionFacts.builder() (Item 1)
104
+ - Proper equals, hashCode, toString (Items 10-12)
105
+ - Defensive copies for any mutable fields (Item 50)
106
+ - @Override annotations (Item 40)
107
+ ```
108
+ </example>
109
+
110
+ <example id="2" title="Strategy Enum">
111
+ ```
112
+ User: "Model different payment processing strategies"
113
+
114
+ You should generate:
115
+ - Enum with abstract method and constant-specific implementations (Item 34)
116
+ - Strategy pattern via enum (avoiding switch on enum)
117
+ - EnumSet for combining payment options (Item 36)
118
+ - EnumMap for payment-method-to-processor mapping (Item 37)
119
+ ```
120
+ </example>
121
+
122
+ <example id="3" title="Thread-Safe Service">
123
+ ```
124
+ User: "Build a caching service that handles concurrent access"
125
+
126
+ You should generate:
127
+ - ConcurrentHashMap for thread-safe cache (Item 81)
128
+ - Documented thread safety level (Item 82)
129
+ - Lazy initialization with double-check idiom if needed (Item 83)
130
+ - Composition over inheritance for wrapping underlying store (Item 18)
131
+ - Try-with-resources for any closeable resources (Item 9)
132
+ ```
133
+ </example>
134
+ </examples>
135
+
136
+ ---
137
+
138
+ ## Mode 2: Code Review
139
+
140
+ When reviewing Java code, read `references/review-checklist.md` for the full checklist.
141
+ Apply these categories systematically:
142
+
143
+ ### Review Process
144
+
145
+ 1. **Object creation** — Are static factories, builders, DI used appropriately? Any unnecessary object creation?
146
+ 2. **Class design** — Minimal accessibility? Immutability where possible? Composition over inheritance?
147
+ 3. **Generics** — No raw types? Proper wildcards? Typesafe?
148
+ 4. **Enums and annotations** — Enums instead of int constants? @Override present? Marker interfaces used correctly?
149
+ 5. **Lambdas and streams** — Used judiciously? Side-effect-free? Not overused?
150
+ 6. **Method design** — Parameters validated? Defensive copies? Overloading safe? Return types appropriate?
151
+ 7. **General programming** — Variables scoped minimally? For-each used? Standard library utilized?
152
+ 8. **Exceptions** — Used for exceptional conditions only? Appropriate types? Documented? Not ignored?
153
+ 9. **Concurrency** — Thread safety documented? Proper synchronization? Modern utilities used?
154
+ 10. **Serialization** — Java serialization avoided? If present, proxies used?
155
+
156
+ <strengths_to_praise>
157
+ ### What to Praise in Good Code
158
+
159
+ When reviewing well-written code, actively call out what is done correctly — don't manufacture issues just to have something to say. Common strengths to recognize:
160
+
161
+ - **Immutable value class** — `final` class, `private final` fields, no setters, defensive copies → praise Item 17
162
+ - **Static factory method** — meaningful name (`of`, `from`), validation before construction, ability to cache → praise Item 1
163
+ - **Normalization in factories** — `trim()`, `toLowerCase()`, or other canonicalization inside `of()` ensures that logically-equal inputs produce equal objects (`EmailAddress.of("USER@Example.COM").equals(EmailAddress.of("user@example.com"))`) → praise as a strength of the factory pattern
164
+ - **Parameter validation** — `Objects.requireNonNull`, `IllegalArgumentException` with descriptive message → praise Item 49
165
+ - **equals/hashCode/toString contract** — all three properly overridden, `equals` uses `instanceof`, `hashCode` uses `Objects.hash` → praise Items 10-12
166
+ - **Builder with fluent API** — private constructor, nested static Builder, mandatory params in Builder constructor → praise Item 2
167
+ </strengths_to_praise>
168
+
169
+ ### Review Output Format
170
+
171
+ Structure your review as:
172
+
173
+ ```
174
+ ## Summary
175
+ One paragraph: what the code does, which patterns it uses, overall assessment.
176
+
177
+ ## Strengths
178
+ What the code does well, which Effective Java items are correctly applied.
179
+
180
+ ## Issues Found
181
+ For each issue:
182
+ - **What**: describe the problem
183
+ - **Why it matters**: explain the bug, maintenance, or performance risk
184
+ - **Item to apply**: which Effective Java item addresses this
185
+ - **Suggested fix**: concrete code change
186
+
187
+ ## Recommendations
188
+ Priority-ordered list of improvements, from most critical to nice-to-have.
189
+ ```
190
+
191
+ <anti_patterns>
192
+ ### Common Anti-Patterns to Flag
193
+
194
+ - **Telescoping constructors** — Multiple constructors with increasing parameters instead of Builder (Item 2). When flagging, note whether a Java Record or Builder is more appropriate: Records suit simple, fully-required, value-oriented data; Builder suits classes with many optional parameters. For a class with 4+ optional fields, Builder wins.
195
+ - **Boolean parameters at unknown positions** — Multiple boolean parameters in a constructor or method call (e.g., the 6th and 9th arguments in a large constructor) are especially dangerous: callers cannot tell what each boolean means at the call site, and swapping them causes a bug with no compile-time error. Flag this explicitly and recommend named methods or a Builder to eliminate positional boolean ambiguity (Item 51).
196
+ - **Public mutable fields** — Exposing all fields as `public` with no access control violates the 'minimal accessibility' principle (Item 15/16) and enables uncontrolled mutation. All fields should be `private`; mutable classes should expose controlled setters only as needed; immutable classes expose no setters at all. Flag public fields explicitly and recommend `private final` with getters only.
197
+ - **Mutable class that could be immutable** — Public setters on a class that doesn't need to change after construction (Item 17)
198
+ - **No defensive copies on mutable constructor arguments** — When a mutable object (e.g., `Map`, `List`, `Date`) is passed to a constructor and stored directly, external code retains a reference and can mutate the object's internal state after construction. Always copy mutable parameters in constructors and return defensive copies from accessors (Item 50).
199
+ - **Concrete class inheritance** — Extending a concrete class for code reuse instead of composition (Item 18)
200
+ - **Raw types** — Using `List` instead of `List<String>` (Item 26). When reviewing event-bus or registry patterns that key handlers by String, also flag that String keys are fragile: a single typo causes a silent miss at runtime. Prefer `Class<T>` as the key — it is self-documenting, refactoring-safe, and enables compile-time type binding.
201
+ - **Overloading confusion** — Overloaded methods with same arity but different behavior depending on runtime type (Item 52)
202
+ - **Returning null instead of empty collection** — `return null` instead of `Collections.emptyList()` (Item 54)
203
+ - **Catching Exception/Throwable** — Over-broad catch blocks that swallow important errors (Item 77)
204
+ - **Using `wait`/`notify`** — When `CountDownLatch`, `CyclicBarrier`, or `CompletableFuture` would be clearer (Item 81)
205
+ - **Mutable Date/Calendar fields** — Exposing mutable `Date` or `Calendar` objects without defensive copies (Item 50)
206
+ - **String concatenation in loops** — Using `+` in a loop instead of `StringBuilder` (Item 63)
207
+ - **Using `float`/`double` for money** — Should be `BigDecimal`, `int`, or `long` (Item 60)
208
+ - **Ignoring return value of `Optional`** — Calling `.get()` without `.isPresent()` check or using `orElse`/`orElseThrow` (Item 55)
209
+ - **ClassCastException handling as design** — Catching `ClassCastException` at runtime to handle type mismatches is a design smell; proper generics eliminate the need entirely (Item 26)
210
+ - **Unchecked cast warnings suppressed or ignored** — Code that generates many unchecked cast warnings (Item 27) signals a deeper generics design flaw, not just a warning to suppress
211
+ </anti_patterns>
212
+
213
+ ---
214
+
215
+ <guidelines>
216
+ ## General Guidelines
217
+
218
+ - Be practical, not dogmatic. Not every class needs a Builder; not every hierarchy needs interfaces.
219
+ Apply items where they provide clear benefit.
220
+ - The three goals are **correctness** (bug-free), **clarity** (easy to read and understand),
221
+ and **performance** (efficient where it matters). Every recommendation should advance at least one.
222
+ - Modern Java (9+) features like modules, `var`, records, sealed classes, and pattern matching
223
+ complement Effective Java items. Recommend them where appropriate.
224
+ - When a simpler solution works, don't over-engineer. Bloch himself emphasizes minimizing complexity.
225
+ - For deeper item details, read `references/items-catalog.md` before generating code.
226
+ - For review checklists, read `references/review-checklist.md` before reviewing code.
227
+ </guidelines>
@@ -0,0 +1,46 @@
1
+ {
2
+ "evals": [
3
+ {
4
+ "id": "eval-01-public-mutable-fields-no-builder",
5
+ "prompt": "Review this Java code:\n\n```java\npublic class HttpRequest {\n public String url;\n public String method;\n public Map<String, String> headers;\n public String body;\n public int timeoutMs;\n public boolean followRedirects;\n public int maxRetries;\n public String authToken;\n public boolean verifySsl;\n public String proxyHost;\n public int proxyPort;\n \n public HttpRequest(String url, String method, Map<String, String> headers,\n String body, int timeoutMs, boolean followRedirects,\n int maxRetries, String authToken, boolean verifySsl,\n String proxyHost, int proxyPort) {\n this.url = url;\n this.method = method;\n this.headers = headers;\n this.body = body;\n this.timeoutMs = timeoutMs;\n this.followRedirects = followRedirects;\n this.maxRetries = maxRetries;\n this.authToken = authToken;\n this.verifySsl = verifySsl;\n this.proxyHost = proxyHost;\n this.proxyPort = proxyPort;\n }\n}\n\n// Typical usage:\nHttpRequest req = new HttpRequest(\n \"https://api.example.com/data\",\n \"POST\",\n new HashMap<>(),\n \"{\\\"key\\\": \\\"value\\\"}\",\n 5000,\n true,\n 3,\n null,\n true,\n null,\n 0\n);\nreq.headers.put(\"Content-Type\", \"application/json\");\n```",
6
+ "expectations": [
7
+ "Flags Item 2 (Builder pattern): 11-parameter constructor is a telescoping constructor — callers cannot tell what the 6th argument (boolean) means at the call site",
8
+ "Flags that all fields are public — violates the 'minimal accessibility' review criterion (Mode 2: Class design — 'Minimal accessibility?'); enables uncontrolled mutation that Item 17 (minimize mutability) and the private final recommendation are meant to prevent",
9
+ "Flags Item 17 (minimize mutability): HttpRequest should be immutable — a request is a value that shouldn't change after construction",
10
+ "Flags Item 50 (defensive copies): headers is a mutable Map passed directly — external code can mutate it after the fact; req.headers.put() after construction is a symptom",
11
+ "Recommends the Builder pattern with a fluent API: HttpRequest.builder(url).method(\"POST\").timeout(5000).build()",
12
+ "Notes that the two boolean parameters (followRedirects, verifySsl) at positions 6 and 9 are especially dangerous — easy to swap without compile-time error",
13
+ "Suggests making all fields private final, constructor private, and providing only getters",
14
+ "May note that modern Java could use a Record for simpler cases, but Builder is more appropriate here due to many optional fields"
15
+ ]
16
+ },
17
+ {
18
+ "id": "eval-02-raw-types-generics",
19
+ "prompt": "Review this Java code:\n\n```java\npublic class EventBus {\n private Map subscribers = new HashMap();\n \n public void subscribe(String eventType, Object handler) {\n List handlers = (List) subscribers.get(eventType);\n if (handlers == null) {\n handlers = new ArrayList();\n subscribers.put(eventType, handlers);\n }\n handlers.add(handler);\n }\n \n public void publish(String eventType, Object event) {\n List handlers = (List) subscribers.get(eventType);\n if (handlers != null) {\n for (Object handler : handlers) {\n try {\n // Cast handler to expected type and call it\n ((java.util.function.Consumer) handler).accept(event);\n } catch (ClassCastException e) {\n System.err.println(\"Handler type mismatch for event: \" + eventType);\n }\n }\n }\n }\n \n public List getHandlersForEvent(String eventType) {\n return (List) subscribers.get(eventType);\n }\n}\n\n// Usage:\nEventBus bus = new EventBus();\nbus.subscribe(\"USER_CREATED\", (Consumer<String>) name -> System.out.println(\"User created: \" + name));\nbus.subscribe(\"USER_CREATED\", (Consumer<Integer>) id -> System.out.println(\"ID: \" + id)); // Wrong type, no compile error\nbus.publish(\"USER_CREATED\", \"Alice\"); // Second handler will ClassCastException at runtime\n```",
20
+ "expectations": [
21
+ "Flags Item 26 (no raw types): Map, HashMap, List, ArrayList, and Consumer are all used as raw types — the entire class loses type safety",
22
+ "Explains the consequence: the second bus.subscribe() with Consumer<Integer> compiles silently but causes ClassCastException at runtime — exactly the bug raw types enable",
23
+ "Notes that catching ClassCastException to handle type mismatches is a design smell that would disappear with proper generics",
24
+ "Recommends a generic EventBus<T> or a type-safe subscriber model using Class<T> as the key instead of String",
25
+ "Suggests a typed design: subscribe(Class<T> eventType, Consumer<T> handler) with Map<Class<?>, List<Consumer<?>>> subscribers",
26
+ "Notes that getHandlersForEvent returns a raw List — callers cannot know the element type at compile time",
27
+ "May mention Item 27 (eliminate unchecked warnings): this code would generate many unchecked cast warnings that are being suppressed or ignored",
28
+ "Notes that String event type keys are fragile — typos cause silent misses; Class<T> as the key is self-documenting and refactoring-safe"
29
+ ]
30
+ },
31
+ {
32
+ "id": "eval-03-immutable-value-class",
33
+ "prompt": "Review this Java code:\n\n```java\npublic final class EmailAddress {\n private final String localPart;\n private final String domain;\n \n private EmailAddress(String localPart, String domain) {\n this.localPart = localPart;\n this.domain = domain;\n }\n \n public static EmailAddress of(String email) {\n Objects.requireNonNull(email, \"email must not be null\");\n String trimmed = email.trim().toLowerCase(Locale.ROOT);\n int atIndex = trimmed.indexOf('@');\n if (atIndex <= 0 || atIndex == trimmed.length() - 1) {\n throw new IllegalArgumentException(\"Invalid email address: \" + email);\n }\n String local = trimmed.substring(0, atIndex);\n String dom = trimmed.substring(atIndex + 1);\n return new EmailAddress(local, dom);\n }\n \n public String getLocalPart() { return localPart; }\n public String getDomain() { return domain; }\n \n public String toCanonicalString() {\n return localPart + \"@\" + domain;\n }\n \n @Override\n public boolean equals(Object o) {\n if (this == o) return true;\n if (!(o instanceof EmailAddress)) return false;\n EmailAddress that = (EmailAddress) o;\n return localPart.equals(that.localPart) && domain.equals(that.domain);\n }\n \n @Override\n public int hashCode() {\n return Objects.hash(localPart, domain);\n }\n \n @Override\n public String toString() {\n return toCanonicalString();\n }\n}\n```",
34
+ "expectations": [
35
+ "Recognizes this as a well-designed immutable value class and says so explicitly",
36
+ "Praises Item 17 (minimize mutability): class is final, all fields are private final, no setters",
37
+ "Praises Item 1 (static factory method): EmailAddress.of() gives a meaningful name, allows validation before construction, and can potentially cache instances",
38
+ "Praises Item 49 (parameter validation): uses Objects.requireNonNull and throws IllegalArgumentException with useful context",
39
+ "Praises Items 10-12 (equals/hashCode/toString contract): properly overridden, equals checks instanceof, hashCode uses Objects.hash, toString returns a useful representation",
40
+ "Praises normalization in the factory: trim() and toLowerCase() ensure canonical representation, meaning EmailAddress.of(\"USER@Example.COM\") equals EmailAddress.of(\"user@example.com\")",
41
+ "Does NOT manufacture fake issues just to have something to say",
42
+ "May offer minor optional suggestions (e.g., more thorough RFC-5321 validation, caching for common domains, a Java Record equivalent in newer Java versions) but clearly frames them as enhancements"
43
+ ]
44
+ }
45
+ ]
46
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "pass_rate": 1,
3
+ "passed": 24,
4
+ "total": 24,
5
+ "baseline_pass_rate": 0.75,
6
+ "baseline_passed": 18,
7
+ "baseline_total": 24,
8
+ "delta": 0.25,
9
+ "model": "default",
10
+ "evals_run": 3,
11
+ "date": "2026-03-29",
12
+ "non_standard_provider": true
13
+ }
@@ -0,0 +1,83 @@
1
+ # After
2
+
3
+ An immutable `ShippingAddress` value class built with a fluent Builder, validated at construction time so that no corrupt instance can ever exist.
4
+
5
+ ```java
6
+ public final class ShippingAddress {
7
+ private final String recipientName;
8
+ private final String streetLine1;
9
+ private final String streetLine2; // nullable — optional field
10
+ private final String city;
11
+ private final String stateCode;
12
+ private final String postalCode;
13
+ private final String countryCode;
14
+ private final boolean residential;
15
+
16
+ private ShippingAddress(Builder builder) {
17
+ this.recipientName = builder.recipientName;
18
+ this.streetLine1 = builder.streetLine1;
19
+ this.streetLine2 = builder.streetLine2;
20
+ this.city = builder.city;
21
+ this.stateCode = builder.stateCode;
22
+ this.postalCode = builder.postalCode;
23
+ this.countryCode = builder.countryCode;
24
+ this.residential = builder.residential;
25
+ }
26
+
27
+ public static Builder builder(String recipientName, String streetLine1,
28
+ String city, String postalCode, String countryCode) {
29
+ return new Builder(recipientName, streetLine1, city, postalCode, countryCode);
30
+ }
31
+
32
+ // Accessors only — no setters
33
+ public String recipientName() { return recipientName; }
34
+ public String streetLine1() { return streetLine1; }
35
+ public Optional<String> streetLine2() { return Optional.ofNullable(streetLine2); }
36
+ public String city() { return city; }
37
+ public String stateCode() { return stateCode; }
38
+ public String postalCode() { return postalCode; }
39
+ public String countryCode() { return countryCode; }
40
+ public boolean isResidential(){ return residential; }
41
+
42
+ public static final class Builder {
43
+ // Required fields set via constructor
44
+ private final String recipientName;
45
+ private final String streetLine1;
46
+ private final String city;
47
+ private final String postalCode;
48
+ private final String countryCode;
49
+ // Optional fields
50
+ private String streetLine2;
51
+ private String stateCode = "";
52
+ private boolean residential = true;
53
+
54
+ private Builder(String recipientName, String streetLine1,
55
+ String city, String postalCode, String countryCode) {
56
+ this.recipientName = Objects.requireNonNull(recipientName, "recipientName");
57
+ this.streetLine1 = Objects.requireNonNull(streetLine1, "streetLine1");
58
+ this.city = Objects.requireNonNull(city, "city");
59
+ this.postalCode = Objects.requireNonNull(postalCode, "postalCode");
60
+ this.countryCode = Objects.requireNonNull(countryCode, "countryCode");
61
+ }
62
+
63
+ public Builder streetLine2(String val) { this.streetLine2 = val; return this; }
64
+ public Builder stateCode(String val) { this.stateCode = val; return this; }
65
+ public Builder commercial() { this.residential = false; return this; }
66
+ public ShippingAddress build() { return new ShippingAddress(this); }
67
+ }
68
+ }
69
+
70
+ // Usage — required fields enforced; optional fields have readable names
71
+ ShippingAddress addr = ShippingAddress
72
+ .builder("Jane Doe", "742 Evergreen Terrace", "Springfield", "62701", "US")
73
+ .stateCode("IL")
74
+ .commercial()
75
+ .build();
76
+ ```
77
+
78
+ Key improvements:
79
+ - Builder pattern eliminates the telescoping constructor problem — required fields are in the builder constructor, optional fields use fluent setters (Item 2: Builder when many parameters)
80
+ - All fields are `private final` — the class is immutable after construction (Item 17: Minimize mutability)
81
+ - `Objects.requireNonNull` validates all required fields at construction time — corrupt objects are impossible to create (Item 49: Validate parameters)
82
+ - `Optional<String>` return type for `streetLine2` explicitly signals to callers that this value may be absent (Item 55: Return Optional judiciously)
83
+ - `final` class prevents subclasses from breaking immutability guarantees (Item 17)
@@ -0,0 +1,37 @@
1
+ # Before
2
+
3
+ A Java class representing a shipping address with public mutable fields, no validation, and no safe construction — making it trivial to create corrupt objects.
4
+
5
+ ```java
6
+ public class ShippingAddress {
7
+ public String recipientName;
8
+ public String streetLine1;
9
+ public String streetLine2;
10
+ public String city;
11
+ public String stateCode;
12
+ public String postalCode;
13
+ public String countryCode;
14
+ public boolean isResidential;
15
+
16
+ public ShippingAddress() {}
17
+
18
+ public ShippingAddress(String recipientName, String streetLine1,
19
+ String streetLine2, String city, String stateCode,
20
+ String postalCode, String countryCode,
21
+ boolean isResidential) {
22
+ this.recipientName = recipientName;
23
+ this.streetLine1 = streetLine1;
24
+ this.streetLine2 = streetLine2;
25
+ this.city = city;
26
+ this.stateCode = stateCode;
27
+ this.postalCode = postalCode;
28
+ this.countryCode = countryCode;
29
+ this.isResidential = isResidential;
30
+ }
31
+ }
32
+
33
+ // Usage — nothing prevents corrupt construction
34
+ ShippingAddress addr = new ShippingAddress();
35
+ addr.city = "Austin";
36
+ // postalCode, countryCode, recipientName all null — silently broken
37
+ ```