1
0
mirror of https://github.com/google/nomulus synced 2026-02-09 22:40:55 +00:00

Add methods to return subtypes of HistoryEntry when querying (#1172)

This is useful when we expect a specific subtype in the return value so
that we can set the parent resource (e. g. DomainContent for
DomainHistory) on it, or when a specific subtype is needed from the call
site.

This PR also fixes some use of generic return values. It is always better to
return <HistoryEntry> than a wildcard <? extends HistoryEntry>, because for
immutable collections, <? extends HistoryEntry> is no different than
<HistoryEntry> as return value -- you can only get a HistoryEntry from it.
The wildcard return value means that even if you are indeed getting a
<DomainHistory> from the query, the call site has no compile time knowledge of
it and can only assume it is a <HistoryEntry>.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/google/nomulus/1172)
<!-- Reviewable:end -->
This commit is contained in:
Lai Jiang
2021-05-24 11:36:11 -04:00
committed by GitHub
parent 5e28694053
commit 2f2e9dd49f
4 changed files with 80 additions and 14 deletions

View File

@@ -1101,10 +1101,19 @@ public class DatabaseHelper {
}
/** Returns all of the history entries that are parented off the given EppResource. */
public static List<? extends HistoryEntry> getHistoryEntries(EppResource resource) {
public static List<HistoryEntry> getHistoryEntries(EppResource resource) {
return HistoryEntryDao.loadHistoryObjectsForResource(resource.createVKey());
}
/**
* Returns all of the history entries that are parented off the given EppResource, casted to the
* corresponding subclass.
*/
public static <T extends HistoryEntry> List<T> getHistoryEntries(
EppResource resource, Class<T> subclazz) {
return HistoryEntryDao.loadHistoryObjectsForResource(resource.createVKey(), subclazz);
}
/**
* Returns all of the history entries that are parented off the given EppResource with the given
* type.
@@ -1112,7 +1121,18 @@ public class DatabaseHelper {
public static ImmutableList<HistoryEntry> getHistoryEntriesOfType(
EppResource resource, final HistoryEntry.Type type) {
return getHistoryEntries(resource).stream()
.filter(entry -> entry.getType() == type)
.filter(entry -> entry.getType().equals(type))
.collect(toImmutableList());
}
/**
* Returns all of the history entries that are parented off the given EppResource with the given
* type and casted to the corresponding subclass.
*/
public static <T extends HistoryEntry> ImmutableList<T> getHistoryEntriesOfType(
EppResource resource, final HistoryEntry.Type type, Class<T> subclazz) {
return getHistoryEntries(resource, subclazz).stream()
.filter(entry -> entry.getType().equals(type))
.collect(toImmutableList());
}
@@ -1122,9 +1142,16 @@ public class DatabaseHelper {
*/
public static HistoryEntry getOnlyHistoryEntryOfType(
EppResource resource, final HistoryEntry.Type type) {
List<HistoryEntry> historyEntries = getHistoryEntriesOfType(resource, type);
assertThat(historyEntries).hasSize(1);
return historyEntries.get(0);
return Iterables.getOnlyElement(getHistoryEntriesOfType(resource, type));
}
/**
* Returns the only history entry of the given type, casted to the corresponding subtype, and
* throws an AssertionError if there are zero or more than one.
*/
public static <T extends HistoryEntry> T getOnlyHistoryEntryOfType(
EppResource resource, final HistoryEntry.Type type, Class<T> subclazz) {
return Iterables.getOnlyElement(getHistoryEntriesOfType(resource, type, subclazz));
}
private static HistoryEntry.Type getHistoryEntryType(EppResource resource) {