Spaces:
Paused
Paused
File size: 4,637 Bytes
93d826e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | package example;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.stream.*;
import java.time.*;
// Generic interface with type bounds
interface DataProcessor<T extends Comparable<T>> {
CompletableFuture<T> processAsync(T input);
boolean validate(T input);
}
// Enum with methods and fields
enum Status {
PENDING("P"),
ACTIVE("A"),
COMPLETED("C"),
FAILED("F");
private final String code;
Status(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
// Abstract class with generic type
abstract class BaseEntity<ID> {
protected ID id;
protected LocalDateTime createdAt;
protected LocalDateTime updatedAt;
public abstract void validate();
}
// Record type (Java 16+)
record UserDTO(
String name,
String email,
Set<String> roles
) {}
// Annotation definition
@interface Audited {
String value() default "";
boolean required() default true;
}
// Main class with various Java features
public class Example extends BaseEntity<UUID> implements DataProcessor<String> {
// Static fields and initialization block
private static final int MAX_RETRIES = 3;
private static final Map<String, Integer> CACHE;
static {
CACHE = new ConcurrentHashMap<>();
}
// Instance fields with different access modifiers
private final Queue<String> queue;
protected Status status;
@Audited
public String name;
// Constructor with builder pattern
private Example(Builder builder) {
this.queue = new LinkedBlockingQueue<>();
this.status = Status.PENDING;
this.name = builder.name;
}
// Builder static class
public static class Builder {
private String name;
public Builder name(String name) {
this.name = name;
return this;
}
public Example build() {
return new Example(this);
}
}
// Interface implementation
@Override
public CompletableFuture<String> processAsync(String input) {
return CompletableFuture.supplyAsync(() -> {
try {
queue.offer(input);
return input.toUpperCase();
} catch (Exception e) {
throw new CompletionException(e);
}
});
}
@Override
public boolean validate(String input) {
return input != null && !input.isEmpty();
}
// Abstract method implementation
@Override
public void validate() {
if (name == null || name.isEmpty()) {
throw new IllegalStateException("Name is required");
}
}
// Generic method with wildcards
public <T extends Comparable<? super T>> List<T> sort(Collection<T> items) {
return items.stream()
.sorted()
.collect(Collectors.toList());
}
// Method with functional interfaces
public void processItems(
List<String> items,
Predicate<String> filter,
Consumer<String> processor
) {
items.stream()
.filter(filter)
.forEach(processor);
}
// Exception class
public static class ProcessingException extends RuntimeException {
public ProcessingException(String message) {
super(message);
}
}
// Main method demonstrating usage
public static void main(String[] args) {
var example = new Builder()
.name("Test Example")
.build();
// Lambda and method reference usage
List<String> items = Arrays.asList("a", "b", "c");
example.processItems(
items,
String::isEmpty,
System.out::println
);
// Stream API usage
Map<Status, Long> statusCounts = items.stream()
.map(s -> Status.PENDING)
.collect(Collectors.groupingBy(
status -> status,
Collectors.counting()
));
// CompletableFuture with exception handling
example.processAsync("test")
.thenApply(String::toLowerCase)
.exceptionally(throwable -> {
System.err.println("Error: " + throwable.getMessage());
return "";
});
// Try-with-resources and Optional usage
try (var scanner = new Scanner(System.in)) {
Optional.of(scanner.nextLine())
.filter(example::validate)
.ifPresent(example::processAsync);
}
}
}
|