Skip to content

core-java-samples/in-memory-repository-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

in-memory-repository-sample

A minimal demonstration of a repository abstraction backed by an in-memory collection.

What It Demonstrates

The repository interface hides the data source behind a contract.

The caller works against OwnerRepository and has no knowledge of how or where data is stored.

FakeOwnerRepository simulates a database using a LinkedHashMap — useful for prototyping and testing without a real persistence layer.

Structure

All code is in a single file: FakeRepositorySampleApplication.java

FakeRepositorySampleApplication.java
│
├── Owner                          # Entity — record with id and name
├── OwnerRepository                # Repository interface — findAll()
├── FakeOwnerRepository            # In-memory implementation using LinkedHashMap
└── FakeRepositorySampleApplication  # Entry point + demo()

Key Points

Interface as contract. The calling code works against OwnerRepository — not the implementation. Swapping the backing store requires no changes to the caller.

LinkedHashMap preserves insertion order. Records are returned in the order they were added — predictable and consistent across calls.

List.copyOf protects the internal state. The repository returns an unmodifiable copy — the caller cannot mutate the underlying collection.

Console Output

Owner[id=1, name=jack1]
Owner[id=2, name=jack2]
Owner[id=3, name=jack3]
Owner[id=4, name=jack4]
Owner[id=5, name=jack5]

Run

./mvnw spring-boot:run

See also

About

Repository abstraction backed by in-memory collections.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages