|
| 1 | +# 30 Seconds of Java - Priming Context for AI Agents |
| 2 | + |
| 3 | +## Quick Overview |
| 4 | +30 Seconds of Java is a curated collection of reusable, copy-pasteable Java 21 snippets designed to be understood in 30 seconds or less. These snippets cover a wide range of common tasks and are automatically published to the project's website via the `README.md` file. |
| 5 | + |
| 6 | +## Stack |
| 7 | +- **Language**: Java 21 |
| 8 | +- **Build System**: Gradle 8.x |
| 9 | +- **Testing**: JUnit 5 (Jupiter), Hamcrest |
| 10 | +- **Quality Assurance**: Checkstyle, SonarCloud |
| 11 | + |
| 12 | +## Trusted Sources |
| 13 | +- [Java 21 SE API Documentation](https://docs.oracle.com/en/java/javase/21/docs/api/index.html) |
| 14 | +- [JUnit 5 User Guide](https://junit.org/junit5/docs/current/user-guide/) |
| 15 | +- [Gradle User Manual](https://docs.gradle.org/current/userguide/userguide.html) |
| 16 | +- [Hamcrest Matchers Reference](http://hamcrest.org/JavaHamcrest/javadoc/2.2/org/hamcrest/Matchers.html) |
| 17 | + |
| 18 | +## Structure |
| 19 | +- `/src/main/java/{category}/`: Source code for the snippets, organized by category (e.g., `string`, `math`, `network`). |
| 20 | +- `/src/test/java/{category}/`: Unit tests for each snippet. |
| 21 | +- `README.md`: The main documentation file containing all snippets, used for website generation. |
| 22 | +- `build.gradle`: Project dependencies and build configuration. |
| 23 | + |
| 24 | +## Patterns |
| 25 | +- **Stateless Utility Methods**: Implement snippets as `public static` methods within a dedicated class. |
| 26 | +- **Locale Independence**: Always use `Locale.ENGLISH` when formatting strings (e.g., `String.format`) to ensure consistent behavior across different environments. |
| 27 | +- **Modern Java Features**: Leverage Java 21 features (e.g., `var`, `Stream` API, modern `HttpClient`) where appropriate. |
| 28 | +- **Focused Scope**: Each snippet class should perform exactly one task or a small set of highly related tasks. |
| 29 | + |
| 30 | +## Anti-patterns |
| 31 | +- **External Dependencies**: Do not add third-party libraries; rely exclusively on the Java Standard Library. |
| 32 | +- **Global State**: Snippets must not maintain or depend on mutable global state. |
| 33 | +- **Interactive Logic**: Avoid snippets that require terminal input or user interaction. |
| 34 | +- **Untested Code**: Every snippet must be accompanied by a comprehensive JUnit test. |
| 35 | + |
| 36 | +## Example Snippet |
| 37 | +A complete snippet implementation consists of three parts: |
| 38 | + |
| 39 | +1. **Source Code** (`src/main/java/string/FormatBytesSnippet.java`): |
| 40 | +```java |
| 41 | +public class FormatBytesSnippet { |
| 42 | + public static String formatBytes(long bytes) { |
| 43 | + double kb = 1024; |
| 44 | + // ... logic ... |
| 45 | + return String.format(Locale.ENGLISH, "%.2f MB", bytes / mb); |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +2. **Test File** (`src/test/java/string/FormatBytesSnippetTest.java`): |
| 51 | +```java |
| 52 | +class FormatBytesSnippetTest { |
| 53 | + @Test |
| 54 | + void formatBytes() { |
| 55 | + assertEquals("1.46 MB", FormatBytesSnippet.formatBytes(1536000)); |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +3. **README.md Entry**: |
| 61 | +```markdown |
| 62 | +### Format Bytes |
| 63 | + |
| 64 | +```java |
| 65 | +// [Full code of the snippet] |
| 66 | +``` |
| 67 | +``` |
0 commit comments