Skip to content

Commit 8265960

Browse files
committed
initial commit
0 parents  commit 8265960

File tree

14 files changed

+2635
-0
lines changed

14 files changed

+2635
-0
lines changed

.editorconfig

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# EditorConfig helps maintain consistent coding styles across different editors
2+
# https://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[*.java]
13+
indent_style = space
14+
indent_size = 4
15+
max_line_length = 120
16+
ij_continuation_indent_size = 8
17+
18+
[*.xml]
19+
indent_style = space
20+
indent_size = 4
21+
22+
[*.{md,markdown}]
23+
indent_style = space
24+
indent_size = 2
25+
trim_trailing_whitespace = false
26+
27+
[*.{yml,yaml}]
28+
indent_style = space
29+
indent_size = 2
30+
31+
[*.properties]
32+
indent_style = space
33+
indent_size = 4
34+
35+
[{pom.xml,settings.xml}]
36+
indent_style = space
37+
indent_size = 4

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- 'feature/**'
8+
- 'bugfix/**'
9+
pull_request:
10+
branches:
11+
- main
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Read Java version from .sdkmanrc
21+
id: sdkman
22+
run: |
23+
JAVA_VERSION=$(grep "^java=" .sdkmanrc | cut -d'=' -f2 | cut -d'-' -f1)
24+
echo "version=$JAVA_VERSION" >> $GITHUB_OUTPUT
25+
26+
- name: Set up JDK from .sdkmanrc
27+
uses: actions/setup-java@v4
28+
with:
29+
distribution: 'temurin'
30+
java-version: ${{ steps.sdkman.outputs.version }}
31+
32+
- name: Build with Maven Wrapper
33+
run: ./mvnw clean verify --batch-mode

.github/workflows/release.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: Manual Release
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
new_version:
7+
description: 'New version (e.g., 1.2.0)'
8+
required: true
9+
release_name:
10+
description: 'Release name'
11+
required: true
12+
13+
jobs:
14+
release:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: write
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
with:
22+
token: ${{ secrets.GITHUB_TOKEN }}
23+
fetch-depth: 0
24+
25+
- name: Check if tag already exists
26+
run: |
27+
if git rev-parse "v${{ github.event.inputs.new_version }}" >/dev/null 2>&1; then
28+
echo "Error: Tag v${{ github.event.inputs.new_version }} already exists. Releases are immutable."
29+
exit 1
30+
fi
31+
32+
- name: Read Java version from .sdkmanrc
33+
id: sdkman
34+
run: |
35+
JAVA_VERSION=$(grep "^java=" .sdkmanrc | cut -d'=' -f2 | cut -d'-' -f1)
36+
echo "version=$JAVA_VERSION" >> $GITHUB_OUTPUT
37+
38+
- name: Set up JDK from .sdkmanrc
39+
uses: actions/setup-java@v4
40+
with:
41+
distribution: 'temurin'
42+
java-version: ${{ steps.sdkman.outputs.version }}
43+
server-id: central
44+
server-username: MAVEN_USERNAME
45+
server-password: MAVEN_PASSWORD
46+
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
47+
gpg-passphrase: MAVEN_GPG_PASSPHRASE
48+
49+
- name: Configure Git
50+
run: |
51+
git config user.name "${{ github.actor }}"
52+
git config user.email "${{ github.actor }}@users.noreply.github.com"
53+
54+
- name: Update Version Files
55+
run: |
56+
./mvnw versions:set -DnewVersion=${{ github.event.inputs.new_version }} -DgenerateBackupPoms=false --batch-mode
57+
./mvnw versions:commit --batch-mode
58+
59+
sed -i '/either<\/artifactId>/,/<version>/ s/<version>[^<]*<\/version>/<version>${{ github.event.inputs.new_version }}<\/version>/' README.md
60+
61+
git add pom.xml README.md
62+
git diff --staged --quiet || git commit -m "Bump version to ${{ github.event.inputs.new_version }}"
63+
git push origin main
64+
65+
- name: Build and Deploy to Maven Central
66+
run: ./mvnw clean deploy -Prelease --batch-mode
67+
env:
68+
MAVEN_USERNAME: ${{ secrets.CENTRAL_TOKEN_USERNAME }}
69+
MAVEN_PASSWORD: ${{ secrets.CENTRAL_TOKEN_PASSWORD }}
70+
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
71+
72+
- name: Create GitHub Release
73+
uses: softprops/action-gh-release@v1
74+
with:
75+
tag_name: v${{ github.event.inputs.new_version }}
76+
name: ${{ github.event.inputs.release_name }}
77+
files: target/*.jar
78+
env:
79+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
.kotlin
6+
7+
### IntelliJ IDEA ###
8+
.idea/
9+
*.iws
10+
*.iml
11+
*.ipr
12+
13+
### Eclipse ###
14+
.apt_generated
15+
.classpath
16+
.factorypath
17+
.project
18+
.settings
19+
.springBeans
20+
.sts4-cache
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/
34+
35+
### Mac OS ###
36+
.DS_Store
37+
38+
### Windows ###
39+
Thumbs.db
40+
41+
### Logs ###
42+
*.log
43+
44+
### Temporary files ###
45+
*.tmp
46+
*.temp
47+
*~
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
wrapperVersion=3.3.4
2+
distributionType=only-script
3+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip

.sdkmanrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Enable auto-env through the sdkman_auto_env config
2+
# Add key=value pairs of SDKs to use below
3+
java=21.0.8-tem

CLAUDE.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Either - Java Implementation
2+
3+
## Overview
4+
5+
This project provides a Java implementation of Scala's `Either` type, a functional data structure representing a value of one of two possible types (a disjoint union). By convention, `Left` represents failure/error cases and `Right` represents success cases.
6+
7+
## Project Structure
8+
9+
```
10+
either/
11+
├── src/main/java/stream/header/
12+
│ └── Either.java # Main Either implementation
13+
├── pom.xml # Maven configuration
14+
└── CLAUDE.md # This file
15+
```
16+
17+
## Key Information
18+
19+
- **Language**: Java 17+
20+
- **Build Tool**: Maven (use `./mvnw` wrapper)
21+
- **Package**: `stream.header`
22+
- **Main Class**: `Either<L, R>` (sealed interface with `Left` and `Right` records)
23+
24+
## Either API
25+
26+
### Creation
27+
```java
28+
Either<String, Integer> right = Either.right(42);
29+
Either<String, Integer> left = Either.left("error");
30+
```
31+
32+
### Core Operations (Right-biased)
33+
- `map(Function)` - Transform Right value
34+
- `flatMap(Function)` - Chain Either-returning operations
35+
- `mapLeft(Function)` - Transform Left value
36+
- `fold(Function, Function)` - Pattern match on Left or Right
37+
- `getOrElse(T)` - Extract Right or default value
38+
- `filterOrElse(Predicate, Supplier)` - Filter Right value
39+
40+
### Querying
41+
- `isLeft()` / `isRight()` - Check which side
42+
- `contains(R)` - Check if Right equals value
43+
- `exists(Predicate)` - Test Right value
44+
- `getLeft()` / `getRight()` - Unsafe extraction (throws if wrong side)
45+
46+
### Conversion
47+
- `toOptional()` - Convert Right to Optional
48+
- `toOptionalLeft()` - Convert Left to Optional
49+
- `swap()` - Exchange Left and Right
50+
51+
### Side Effects
52+
- `forEach(Consumer)` - Apply action to Right
53+
- `forEachLeft(Consumer)` - Apply action to Left
54+
55+
## Build Commands
56+
57+
```bash
58+
# Compile
59+
./mvnw compile
60+
61+
# Run tests
62+
./mvnw test
63+
64+
# Package
65+
./mvnw package
66+
67+
# Clean
68+
./mvnw clean
69+
```
70+
71+
## Design Decisions
72+
73+
1. **Sealed Interface**: Uses Java 17 sealed interfaces for exhaustive pattern matching
74+
2. **Records**: Left and Right are immutable records
75+
3. **Right-biased**: All monadic operations (map, flatMap) operate on Right, matching modern Scala behavior
76+
4. **Null Safety**: Both Left and Right reject null values
77+
5. **Fail-fast**: Methods validate null parameters immediately
78+
79+
## Usage Example
80+
81+
```java
82+
import stream.header.Either;
83+
84+
public class Example {
85+
public static Either<String, Integer> divide(int a, int b) {
86+
if (b == 0) {
87+
return Either.left("Division by zero");
88+
}
89+
return Either.right(a / b);
90+
}
91+
92+
public static void main(String[] args) {
93+
Either<String, Integer> result = divide(10, 2)
94+
.map(x -> x * 2)
95+
.flatMap(x -> divide(x, 5));
96+
97+
String message = result.fold(
98+
error -> "Error: " + error,
99+
value -> "Result: " + value
100+
);
101+
102+
System.out.println(message); // "Result: 4"
103+
}
104+
}
105+
```
106+
107+
## Conventions
108+
109+
- **Left**: Represents errors, failures, or exceptional cases (typically `String`, `Exception`, or custom error types)
110+
- **Right**: Represents successful results and normal values
111+
- All operations are right-biased (operate on Right, short-circuit on Left)

LICENSE.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2025 stream.header.either contributors
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)