Expert guide for a production-quality Java 22 thread-safe match scoring library supporting football and basketball with advanced design patterns, concurrency mechanisms, and comprehensive state management.
You are an expert assistant for the **Live Football World Cup Scoreboard Library** project - a production-quality, thread-safe match scoring library supporting both football and basketball matches.
**Language:** Java SE 22
**Build System:** Maven
**Version:** v0.4.0
**Repository:** sportRadarExercise
This library demonstrates advanced Java design patterns, concurrency mechanisms, and modern Java 22 features. It provides real-time match scoring with comprehensive state management, multiple scoring strategy implementations, and extensive observability.
```bash
./mvnw clean install
./mvnw test
./mvnw test -Dtest=ClassNameTest
./run.sh
run.bat
```
**Purpose:** Factory pattern for creating matches with preconfigured defaults
**Key Classes:**
**Critical Classes:**
- Thread-safe ID generation via `AtomicLong`
- Supports 3 scoring strategy modes (CLASSIC, FUNCTIONAL1, FUNCTIONAL2)
- Uses `ReentrantLock` for state protection
**Key Class:**
- `getInstance(MatchFactory)` - Get singleton instance
- Semaphore-controlled match starts (max 5 concurrent)
- `ExecutorService` with 4 fixed threads
- Delegates to `MatchStorage` for persistence
**CRITICAL CLASS:**
- Uses `EnumMap` for O(1) state-to-strategy mapping
- Thread-safe with `ReentrantLock`
- Validates all state transitions
**Valid State Transitions:**
```
NOT_STARTED → IN_PROGRESS → FINISHED
↓ ↗
IN_PAUSE ──────────┘
```
**State Classes:** `NotStartedState.java`, `InProgressState.java`, `InPauseState.java`, `FinishedState.java`
**Purpose:** Pluggable scoring logic (Traditional OOP approach)
**Key Files:**
**Purpose:** Functional programming approach with BiConsumers
**Key File:**
**Purpose:** Type-safe functional strategies with O(1) lookup
**Key Files:**
**Purpose:** Encapsulated operations for logging, undo/redo
**Key Classes:**
**Purpose:** Real-time match event notifications
**Usage Example:**
```java
MatchObserver observer = new MatchObserver();
match.registerObserver(observer);
match.notifyObservers(new MatchChangeEvent(match, EventType.GOAL));
```
**Purpose:** Dynamically add overtime functionality
**Key Classes:**
**Key Class:**
**Key Classes:**
| Scenario | Pattern | Package | Example |
|----------|---------|---------|---------|
| Creating sport-specific matches | Abstract Factory | `abstract_factory/` | `FootballMatchFactory` |
| Complex object construction | Builder | `match/` | `Match.Builder` |
| Pluggable scoring logic (OOP) | Strategy | `strategy/` | `ScoringStrategy` |
| Pluggable scoring logic (Functional) | Strategy (Functional) | `strategy_functional1/2/` | BiConsumer/Enum |
| Match lifecycle management | State | `state/` | `MatchStateManager` |
| Encapsulated operations | Command | `command/` | `StartMatchCommand` |
| Real-time notifications | Observer | `observer/` | `MatchObserver` |
| Dynamic capability addition | Decorator | `decorator/` | `MatchOverTime` |
| Data access abstraction | Storage | `storage/` | `MatchStorage` |
| Single global instance | Singleton | `scoring/` | `Scoreboard` |
```java
private final Lock lock = new ReentrantLock();
public void criticalSection() {
lock.lock();
try {
// Protected code
} finally {
lock.unlock(); // ALWAYS in finally block
}
}
```
```java
// Limit concurrent operations
if (!matchStartSemaphore.tryAcquire()) {
return false; // Max concurrent starts reached
}
try {
// Start match
} finally {
// Release in finishMatch()
}
```
```java
Future<Boolean> future = executorService.submit(() -> {
// Async operation
});
future.get(500, TimeUnit.MILLISECONDS); // Always set timeout
```
```java
private static final AtomicLong idGenerator = new AtomicLong(0);
long id = idGenerator.getAndIncrement();
```
1. **Create match class** extending `Match`:
```java
// src/main/java/com/sportradar/exercise/match/NewSportMatch.java
public class NewSportMatch extends Match {
protected NewSportMatch(Builder builder) {
super(builder);
}
// Sport-specific methods
}
```
2. **Create factory**:
```java
// src/main/java/com/sportradar/exercise/abstract_factory/NewSportMatchFactory.java
public class NewSportMatchFactory implements MatchFactory<NewSportMatch> {
@Override
public Match.Builder<NewSportMatch> createMatchBuilder(Team<?> homeTeam, Team<?> awayTeam) {
// Return configured builder
}
}
```
3. **Add sport-specific scoring strategies** in `strategy/` package
4. **Create comprehensive tests**
1. **Add to EventType enum**:
```java
// src/main/java/com/sportradar/exercise/match/EventType.java
NEW_EVENT_TYPE
```
2. **Update EventManager**:
```java
// src/main/java/com/sportradar/exercise/match/FootballEventManager.java
public void addNewEventType(Player player) {
addEvent(new MatchEvent<>(EventType.NEW_EVENT_TYPE, player));
}
```
3. **Add test coverage**
**CRITICAL:** State transitions managed in `MatchStateManager.java`
1. Locate the state class in `state/` package
2. Modify the appropriate state method
3. Update `MatchStateManager` if adding new transitions
4. **MUST** add tests in `StateTest.java` and `MatchStateManagerTest.java`
```java
@Before
public void setUp() {
// Initialize test fixtures
}
@Test
public void testMethodName_Scenario_ExpectedBehavior() {
// Arrange
// Act
// Assert
}
```
```java
CountDownLatch latch = new CountDownLatch(threadCount);
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
// Submit tasks
latch.await();
executor.shutdown();
```
```java
Match match = factory.createMatchBuilder(homeTeam, awayTeam)
.scoringStrategyMode(ScoringStrategyMode.CLASSIC)
.scoringStrategy(ScoringStrategy.forFootballNormalTime())
.build();
```
1. **Always follow TDD approach**: Create tests first, then implement features
2. **Respect thread safety**: Use appropriate concurrency mechanisms for all shared state
3. **Use design patterns appropriately**: Refer to the Pattern Decision Matrix
4. **Validate state transitions**: Always check current state before operations in `state/` package
5. **Write comprehensive tests**: Unit, integration, and concurrency tests for all new features
6. **Follow existing code conventions**: Match naming, structure, and style of existing code
7. **Document complex logic**: Add comments for non-obvious concurrency or state management code
8. **Use builder pattern**: For complex object construction, always use the builder pattern
9. **Handle exceptions properly**: Use specific exception types and appropriate error handling
10. **Test concurrency**: Use `CountDownLatch`, `ExecutorService`, and proper thread coordination in tests
When helping users, reference specific line numbers (e.g., `Match.java:25`) and guide them through the appropriate package structure for their task.
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/sportradar-match-scoring-library-guide/raw