-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#16 Refactoring: extract Storage interface
- Loading branch information
1 parent
47d5023
commit 89a2673
Showing
9 changed files
with
99 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
logic/src/main/java/org/itsallcode/whiterabbit/logic/storage/CachingStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.itsallcode.whiterabbit.logic.storage; | ||
|
||
import java.time.YearMonth; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.itsallcode.whiterabbit.logic.model.MonthIndex; | ||
import org.itsallcode.whiterabbit.logic.model.MultiMonthIndex; | ||
|
||
class CachingStorage implements Storage | ||
{ | ||
private final Storage delegateStorage; | ||
private final MonthCache cache; | ||
|
||
CachingStorage(Storage delegateStorage) | ||
{ | ||
this(delegateStorage, new MonthCache()); | ||
} | ||
|
||
CachingStorage(Storage delegateStorage, MonthCache cache) | ||
{ | ||
this.delegateStorage = delegateStorage; | ||
this.cache = cache; | ||
} | ||
|
||
@Override | ||
public Optional<MonthIndex> loadMonth(YearMonth date) | ||
{ | ||
return delegateStorage.loadMonth(date).map(this::updateCache); | ||
} | ||
|
||
@Override | ||
public MonthIndex loadOrCreate(final YearMonth yearMonth) | ||
{ | ||
return updateCache(delegateStorage.loadOrCreate(yearMonth)); | ||
} | ||
|
||
@Override | ||
public void storeMonth(MonthIndex month) | ||
{ | ||
delegateStorage.storeMonth(updateCache(month)); | ||
} | ||
|
||
@Override | ||
public MultiMonthIndex loadAll() | ||
{ | ||
return updateCache(delegateStorage.loadAll()); | ||
} | ||
|
||
private MultiMonthIndex updateCache(MultiMonthIndex index) | ||
{ | ||
index.getMonths().forEach(this::updateCache); | ||
return index; | ||
} | ||
|
||
private MonthIndex updateCache(MonthIndex month) | ||
{ | ||
cache.update(month); | ||
return month; | ||
} | ||
|
||
@Override | ||
public List<YearMonth> getAvailableDataYearMonth() | ||
{ | ||
return delegateStorage.getAvailableDataYearMonth(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...rabbit/logic/autocomplete/MonthCache.java → ...whiterabbit/logic/storage/MonthCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
logic/src/main/java/org/itsallcode/whiterabbit/logic/storage/StorageLoadingListener.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters