package java_course.module09; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; /** * ☕ JAVA 09: GENERICS & TYPE SAFETY - MUSTERLÖSUNG * ================================================ */ public class Musterloesung { // ======================================================================== // 🎯 LÖSUNG 1: Generische Container-Klasse 'Box' // ======================================================================== public static class Box { private T content; public Box() { this.content = null; } public Box(T content) { this.content = content; } public T getContent() { return content; } public void setContent(T content) { this.content = content; } public boolean isEmpty() { return content == null; } public Optional toOptional() { return Optional.ofNullable(content); } } // ======================================================================== // 🎯 LÖSUNG 2: Generischer Cache mit Schlüssel-Wert-Paaren 'GenericCache' // ======================================================================== public static class GenericCache { private static class Entry { final K key; V value; Entry(K key, V value) { this.key = key; this.value = value; } } private final List> entries = new ArrayList<>(); public void put(K key, V value) { for (Entry entry : entries) { if (Objects.equals(entry.key, key)) { entry.value = value; return; } } entries.add(new Entry<>(key, value)); } public Optional get(K key) { for (Entry entry : entries) { if (Objects.equals(entry.key, key)) { return Optional.ofNullable(entry.value); } } return Optional.empty(); } public boolean containsKey(K key) { for (Entry entry : entries) { if (Objects.equals(entry.key, key)) { return true; } } return false; } public int size() { return entries.size(); } public void clear() { entries.clear(); } } // ======================================================================== // 🎯 LÖSUNG 3: Bounded Generics - Maximum & Statistik Finder // ======================================================================== public static class GenericsMath { public static > T findMax(List items) { if (items == null || items.isEmpty()) { throw new IllegalArgumentException("Liste darf weder null noch leer sein."); } T max = items.get(0); for (T item : items) { if (item != null && (max == null || item.compareTo(max) > 0)) { max = item; } } return max; } public static double calculateSum(List numbers) { if (numbers == null || numbers.isEmpty()) { return 0.0; } double sum = 0.0; for (N number : numbers) { if (number != null) { sum += number.doubleValue(); } } return sum; } } // ======================================================================== // 🎯 LÖSUNG 4: Wildcards & PECS-Prinzip // ======================================================================== public static class WildcardUtils { public static void copyList(List src, List dest) { if (src == null || dest == null) { return; } for (T item : src) { dest.add(item); } } public static int countElements(List list) { if (list == null) { return 0; } return list.size(); } } public static void main(String[] args) { System.out.println("☕ Java 09: Generics & Type Safety (Musterlösung)"); System.out.println("-------------------------------------------------"); Box stringBox = new Box<>("Enterprise Java 21"); System.out.println("Box Inhalt: " + stringBox.getContent()); GenericCache cache = new GenericCache<>(); cache.put("User_101", 42); System.out.println("Cache Größe: " + cache.size()); List zahlen = List.of(12, 45, 7, 89, 23); System.out.println("Maximum: " + GenericsMath.findMax(zahlen)); System.out.println("Summe: " + GenericsMath.calculateSum(zahlen)); } }