Practical Usage Patterns
Common Scenarios for Immutable Tuples
graph TD
A[Immutable Tuple Usage] --> B[Configuration Management]
A --> C[Method Return Values]
A --> D[Caching]
A --> E[Functional Programming]
1. Configuration Management
public final class DatabaseConfig {
private final String host;
private final int port;
private final boolean sslEnabled;
public DatabaseConfig(String host, int port, boolean sslEnabled) {
this.host = host;
this.port = port;
this.sslEnabled = sslEnabled;
}
// Getters for immutable configuration
public String getHost() { return host; }
public int getPort() { return port; }
public boolean isSslEnabled() { return sslEnabled; }
}
2. Multiple Return Values
public class DataProcessor {
public ImmutablePair<Boolean, String> processData(String input) {
try {
// Processing logic
return new ImmutablePair<>(true, "Success");
} catch (Exception e) {
return new ImmutablePair<>(false, e.getMessage());
}
}
}
Usage Patterns Comparison
Pattern |
Use Case |
Advantages |
Configuration |
System Settings |
Thread-safe, Predictable |
Return Values |
Complex Operations |
Clear Error Handling |
Caching |
Memoization |
Performance Optimization |
Event Handling |
State Snapshots |
Immutable State Tracking |
3. Functional Programming Techniques
public class FunctionalExample {
public List<ImmutablePair<String, Integer>> transformData(List<String> input) {
return input.stream()
.map(s -> new ImmutablePair<>(s, s.length()))
.collect(Collectors.toList());
}
}
4. Caching Mechanism
public class CacheManager {
private final Map<String, ImmutablePair<Object, Long>> cache = new ConcurrentHashMap<>();
public void cacheResult(String key, Object value) {
cache.put(key, new ImmutablePair<>(value, System.currentTimeMillis()));
}
public Optional<Object> getCachedValue(String key) {
return Optional.ofNullable(cache.get(key))
.map(ImmutablePair::getFirst);
}
}
LabEx Integration Patterns
Distributed Computing Scenario
public class DistributedTask {
public ImmutablePair<TaskStatus, String> executeRemoteTask(String taskId) {
try {
// Simulated distributed task execution
return new ImmutablePair<>(TaskStatus.COMPLETED, "Task processed successfully");
} catch (Exception e) {
return new ImmutablePair<>(TaskStatus.FAILED, e.getMessage());
}
}
enum TaskStatus {
PENDING, RUNNING, COMPLETED, FAILED
}
}
Best Practices
-
Use immutable tuples for:
- Configuration management
- Method return values with multiple components
- Caching mechanisms
- Functional programming transformations
-
Avoid mutable state in tuple implementations
-
Consider performance implications for large-scale applications
Error Handling and Validation
public final class ValidationResult {
private final boolean isValid;
private final List<String> errors;
public ValidationResult(boolean isValid, List<String> errors) {
this.isValid = isValid;
this.errors = List.copyOf(errors); // Defensive copying
}
public boolean isValid() { return isValid; }
public List<String> getErrors() { return new ArrayList<>(errors); }
}