|
| 1 | +// Module focus: Tracking ownership and lifetime when multiple references can observe the same value. |
| 2 | +// Why it matters: practicing smart pointers in depth patterns makes exercises and checkpoints easier to reason about. |
| 3 | + |
| 4 | +import java.lang.ref.WeakReference; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +public class Main { |
| 10 | + record Report(String title) { |
| 11 | + } |
| 12 | + |
| 13 | + static final class ReportOwner { |
| 14 | + private final String name; |
| 15 | + private Report current; |
| 16 | + |
| 17 | + ReportOwner(String name, Report current) { |
| 18 | + this.name = name; |
| 19 | + this.current = current; |
| 20 | + } |
| 21 | + |
| 22 | + Optional<Report> current() { |
| 23 | + // Optional keeps "empty owner" checks explicit at API boundaries. |
| 24 | + return Optional.ofNullable(current); |
| 25 | + } |
| 26 | + |
| 27 | + void transferTo(ReportOwner destination) { |
| 28 | + // Empty owners cannot transfer ownership, so the method exits visibly. |
| 29 | + if (current == null) { |
| 30 | + System.out.println(name + " has nothing to transfer."); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Moving the reference leaves the source empty and the destination responsible. |
| 35 | + System.out.println(name + " transfers " + current.title() + " to " + destination.name + "."); |
| 36 | + destination.current = current; |
| 37 | + current = null; |
| 38 | + } |
| 39 | + |
| 40 | + void describe() { |
| 41 | + System.out.println(name + ": " + current().map(Report::title).orElse("empty")); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + static final class ReportArchive { |
| 46 | + private final List<Report> reports = new ArrayList<>(); |
| 47 | + |
| 48 | + void add(Report report) { |
| 49 | + // Strong references in this list keep archived reports alive. |
| 50 | + reports.add(report); |
| 51 | + } |
| 52 | + |
| 53 | + List<Report> snapshot() { |
| 54 | + // A defensive snapshot prevents callers from mutating archive internals. |
| 55 | + return List.copyOf(reports); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + static final class PreviewPane { |
| 60 | + private final WeakReference<Report> currentReport; |
| 61 | + |
| 62 | + PreviewPane(Report report) { |
| 63 | + // The preview can observe the report without becoming an owner. |
| 64 | + this.currentReport = new WeakReference<>(report); |
| 65 | + } |
| 66 | + |
| 67 | + void clearForDemo() { |
| 68 | + // Clearing the weak reference gives this small example deterministic output. |
| 69 | + currentReport.clear(); |
| 70 | + } |
| 71 | + |
| 72 | + void describe() { |
| 73 | + Report report = currentReport.get(); |
| 74 | + if (report == null) { |
| 75 | + System.out.println("Preview target expired."); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + System.out.println("Preview can still see: " + report.title()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static void main(String[] args) { |
| 84 | + ReportOwner inbox = new ReportOwner("Inbox", new Report("Quarterly Summary")); |
| 85 | + ReportOwner archiveSlot = new ReportOwner("Archive", null); |
| 86 | + |
| 87 | + // Report ownership before and after transfer so the lifetime change is visible. |
| 88 | + inbox.describe(); |
| 89 | + archiveSlot.describe(); |
| 90 | + inbox.transferTo(archiveSlot); |
| 91 | + inbox.describe(); |
| 92 | + archiveSlot.describe(); |
| 93 | + |
| 94 | + ReportArchive archive = new ReportArchive(); |
| 95 | + archiveSlot.current().ifPresent(archive::add); |
| 96 | + archive.add(new Report("Budget Notes")); |
| 97 | + // The snapshot is safe to share because callers cannot mutate archive internals. |
| 98 | + System.out.println("Archive snapshot size: " + archive.snapshot().size()); |
| 99 | + |
| 100 | + Report transientDraft = new Report("Transient Draft"); |
| 101 | + PreviewPane preview = new PreviewPane(transientDraft); |
| 102 | + preview.describe(); |
| 103 | + |
| 104 | + // Weak observers must handle the target disappearing without owning it. |
| 105 | + transientDraft = null; |
| 106 | + preview.clearForDemo(); |
| 107 | + preview.describe(); |
| 108 | + } |
| 109 | +} |
0 commit comments