A repository is an object responsible for storing and retrieving a particular kind of value. Here it is an ordinary in-memory Java object, not a database framework. Give its callers a small contract: add a unique ID and retrieve its title. Keep the map private so callers cannot skip validation.
Work through the example
The interface says what add and find mean, including a missing result. This version returns null for a missing ID, which the caller must check before using it. It stores no null values. A future implementation must preserve these rules rather than silently replacing duplicates.
This small repository accepts exact nonnull strings, including blanks; it does not trim them. The capstone will use a Task constructor with stricter nonblank rules. State the policy at each boundary rather than assuming every example implements the same validation.
The map's membership check belongs inside add. Splitting “check for duplicate” and “insert” across every caller would repeat policy and invite mistakes. This example is single-threaded; it does not promise concurrency safety. The course's task tracker will store immutable Task objects under the same style of boundary.
Before Run, write the output and identify the exact line responsible for each result. Each editor is a separate program; names and edits in one box do not carry into the next.
import java.util.Map;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
Titles store = new MemoryTitles();
store.add("A1", "Read");
System.out.println(store.find("A1"));
System.out.println(store.find("missing") == null);
}
}
interface Titles {
void add(String id, String title);
String find(String id);
}
final class MemoryTitles implements Titles {
private final Map<String, String> entries = new LinkedHashMap<>();
public void add(String id, String title) {
if (id == null || title == null || entries.containsKey(id)) {
throw new IllegalArgumentException("unique id and title required");
}
entries.put(id, title);
}
public String find(String id) {
return entries.get(id);
}
}Check your prediction
Read
trueCompare the behavior
A test should check what remains after rejection, not only the exception. This map keeps the first title because duplicate insertion fails before put. If validation happened after put, the exception would still occur while the original value was already lost.
import java.util.Map;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
Map<String, String> entries = new LinkedHashMap<>();
entries.put("A1", "Read");
try {
add(entries, "A1", "Lost");
} catch (IllegalArgumentException expected) {
System.out.println("Duplicate rejected");
}
System.out.println(entries.get("A1"));
}
static void add(Map<String, String> entries, String id, String title) {
if (entries.containsKey(id)) {
throw new IllegalArgumentException("duplicate id");
}
entries.put(id, title);
}
}Check your prediction
Duplicate rejected
ReadState read and write guarantees together
A repository boundary should describe each operation's effect, not only its method names. For add, decide whether a duplicate rejects or replaces. For find, decide how absence is represented. For snapshot, decide whether the returned membership is independent and editable. A caller should not need to inspect the private map to answer those questions.
Test a write rejection and then read the original value through the public lookup. The original must still be present. If a duplicate guard runs after put, the same exception could be thrown after losing the original value. Checking only that catch executed would miss this defect.
A small in-memory repository keeps the examples deterministic while you learn the contract. A future database adapter would need to preserve the same visible rules and add its own failure handling. The interface alone cannot guarantee transactionality or concurrency safety; those require an implementation and evidence.
Practice independently
Repository Contracts: practice
Write a Members interface and a MemoryMembers class backed by a private
LinkedHashMap<String, String> from member ID to name:
add(id, name)rejects null values and an ID already present, with an IllegalArgumentException, and keeps the original entry;nameOf(id)returns the name, or null when the ID is absent;rename(id, newName)is the opposite case: it rejects an ID that is not present, and must not create one.
In main, use the Members type and prove every rule with a labelled check,
reading the repository after each rejection. Finish with
Member checks passed.
import java.util.Map;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
System.out.println("Implement Members and MemoryMembers");
}
}Hint 1: the idea
add and rename guard opposite conditions: add needs the ID to be missing, rename needs it to be present. In both, the guard must run before put, or a rejected call has already changed the map by the time it throws.
Hint 2: a first step
Use the rejection pattern from the Day 1 checkpoint for each rejected call:
make the call inside a try, throw an AssertionError on the very next line
(reached only if nothing was thrown), and catch IllegalArgumentException. Do
the "read it back" check inside that catch, through nameOf, so it runs only
after the rejection really happened. Start with the duplicate add.
Show one solution
import java.util.Map;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
Members members = new MemoryMembers();
members.add("A1", "Ada");
check("found", "Ada".equals(members.nameOf("A1")));
check("absent is null", members.nameOf("B2") == null);
try {
members.add("A1", "Bob");
throw new AssertionError("duplicate accepted");
} catch (IllegalArgumentException expected) {
check("original kept", "Ada".equals(members.nameOf("A1")));
}
try {
members.rename("B2", "Cy");
throw new AssertionError("rename of missing id accepted");
} catch (IllegalArgumentException expected) {
check("no entry created", members.nameOf("B2") == null);
}
members.rename("A1", "Ada L.");
check("renamed", "Ada L.".equals(members.nameOf("A1")));
try {
members.add(null, "Dee");
throw new AssertionError("null id accepted");
} catch (IllegalArgumentException expected) {
check("null not stored", members.nameOf(null) == null);
}
System.out.println("Member checks passed");
}
static void check(String label, boolean condition) {
if (!condition) {
throw new AssertionError(label);
}
}
}
interface Members {
void add(String id, String name);
String nameOf(String id);
void rename(String id, String newName);
}
final class MemoryMembers implements Members {
private final Map<String, String> entries = new LinkedHashMap<>();
public void add(String id, String name) {
if (id == null || name == null || entries.containsKey(id)) {
throw new IllegalArgumentException("unique id and name required");
}
entries.put(id, name);
}
public String nameOf(String id) {
return entries.get(id);
}
public void rename(String id, String newName) {
if (newName == null || !entries.containsKey(id)) {
throw new IllegalArgumentException("existing id and name required");
}
entries.put(id, newName);
}
}Expected output:
Member checks passedThe two guards differ by one !: add throws when containsKey is true, rename
when it is false. Each sits before put, so a rejected call leaves the map as
it was; the "original kept" and "no entry created" checks prove that, which an
exception check alone could not. The throw new AssertionError after each
rejected call matters too: if a guard disappeared, the call would return
normally and that line would fail the program instead of letting it pass. Without its guard, rename("B2", ...) would
quietly add a new member, because Map.put inserts when the key is missing.
Change it and test again
Move rename's guard below its put. Which check fails first, and what does the map hold at that moment? Restore it. Then decide whether two members may share a name (not an ID): state the policy before changing any code.
Explain before moving on
What can a caller do through Members? What extra power would a method returning the internal map expose? Explain how an interface and an ownership rule solve different parts of this design.
Completion is saved in this browser only. It does not measure mastery.