nl.vpro.domain.media.support.MediaObjectOwnableLists Maven / Gradle / Ivy
Show all versions of media-domain Show documentation
package nl.vpro.domain.media.support;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import nl.vpro.domain.media.MediaObject;
/**
* Utilities related to updating {@link MediaObjectOwnableList}.
*
* @see OwnableLists
* @author Michiel Meeuwissen
* @since 5.11
*/
@Slf4j
public class MediaObjectOwnableLists {
private MediaObjectOwnableLists() {
}
/**
* @return {@code true} as {@link Collection#add(Object)}, if the given set changed.
*/
public static , I extends MediaObjectOwnableListItem>
boolean addValue(@NonNull Set
set,
@NonNull Supplier
creator,
@NonNull I newValue,
@NonNull OwnerType owner) {
Optional
match = set.stream().filter(o -> Objects.equals(o.getOwner(), owner)).findFirst();
if (match.isPresent() && match.get().getValues().contains(newValue)) {
return false;
} else if (match.isPresent()) {
newValue.setParent(match.get());
return match.get().getValues().add(newValue);
} else {
P newList = creator.get();
newValue.setParent(newList);
newList.getValues().add(newValue);
return set.add(newList);
}
}
public static
, I extends MediaObjectOwnableListItem>
MediaObject addOrUpdateOwnableList(@NonNull MediaObject parent, @NonNull Collection
list, @NonNull P newOwnableList) {
Optional
existing = OwnableLists.filterByOwner(list, newOwnableList.getOwner());
if (existing.isPresent()) {
P existingList = existing.get();
if ( ! Objects.equals(existingList.getValues(), newOwnableList.getValues())) {
existing.get().getValues().clear();
newOwnableList.getValues().forEach(nv -> {
nv.setParent(existingList);
existingList.add(nv);
});
}
} else {
newOwnableList.setParent(parent);
list.add(newOwnableList);
}
return parent;
}
@SuppressWarnings("StatementWithEmptyBody")
public static
, I extends MediaObjectOwnableListItem>
void addOrUpdateOwnableListForOwner(
@NonNull MediaObject parent,
@NonNull SortedSet
toUpdate,
@Nullable SortedSet
incoming,
OwnerType forOwner) {
if(incoming == null) {
// Ignore it completely, this mainly is for the case that a media update xml was incoming with no values for the given set at all (the xml wrapper was missing)
// indicating that client is at all interested or aware of the feature, and we don't want to empty the value for the owner
} else {
Optional
is = OwnableLists.filterByOwner(incoming, forOwner);
if (is.isPresent()) {
addOrUpdateOwnableList(parent, toUpdate, is.get());
} else {
log.debug("No value found for {} found in {}", forOwner, incoming);
}
}
}
public static
, I extends MediaObjectOwnableListItem>
boolean remove(
Set
set,
@NonNull I value,
@NonNull OwnerType owner
) {
if (set == null) {
return false;
}
final Optional
maybeValues = set.stream()
.filter(owned -> owned.getOwner().equals(owner))
.findAny();
if(maybeValues.isPresent()) {
P list = maybeValues.get();
return list.getValues().remove(value);
}
return false;
}
public static
, I extends MediaObjectOwnableListItem>
boolean remove(
Set
set,
@NonNull OwnerType owner
) {
if (set == null) {
return false;
}
boolean changed = false;
Iterator
i = set.iterator();
while (i.hasNext()) {
P candidate = i.next();
if (candidate.getOwner() == owner) {
candidate.getValues().forEach(v -> {
//v.setParent(null);
});
//candidate.getValues().clear();
i.remove();
changed = true;
}
}
return changed;
}
/**
* Find an MediaObjectOwnableListItem given id and owner
* @param list collection to search into
* @return Optional/ empty if nothing matched
*/
public static
, I extends MediaObjectOwnableListItem>
Optional find(Collection
list, @NonNull Long id, @NonNull OwnerType owner){
if (list == null || list.isEmpty()) {
return Optional.empty();
}
final Optional> maybeValues = list.stream()
.filter(owned -> owned.getOwner().equals(owner))
.findAny()
.map(OwnableList::getValues);
if(maybeValues.isPresent()) {
final Optional maybeLocationFound =
maybeValues.get().stream().filter(
v -> id.equals(v.getId())
).findAny();
return maybeLocationFound;
}
return Optional.empty();
}
public static , I extends MediaObjectOwnableListItem>
void set(@NonNull MediaObject parent, @NonNull Set
existingCollection, @NonNull Set
newCollection) {
existingCollection.clear();
for (P i : newCollection) {
addOrUpdateOwnableList(parent, existingCollection, i.clone());
}
}
public static
, I extends MediaObjectOwnableListItem>
void setIfNotNull(@NonNull MediaObject parent, @Nullable Set
existingCollection, @NonNull Set
newCollection) {
if (existingCollection != null) {
set(parent, existingCollection, newCollection);
}
}
/**
* Expands the incoming value to contain values for all given owners.
*
* NOTE: This methods _modifies_ the incoming list.
*/
protected static
, I extends MediaObjectOwnableListItem>
SortedSet expandExistingOwnedList(
SortedSet values,
BiFunction, OL> creator,
List ownersToExpand) {
if(values == null || values.isEmpty()) return null;
SortedSet additions = new TreeSet<>();
for(OwnerType owner: ownersToExpand){
if(values.stream().anyMatch(value -> value.getOwner() == owner)) {
continue;
}
additions.add(creator.apply(owner, values.first().getValues()));
}
values.addAll(additions);
return values;
}
public static , I extends MediaObjectOwnableListItem>
SortedSet expandOwnedList(
SortedSet incoming,
BiFunction, OL> creator,
List ownersToExpand) {
TreeSet result = new TreeSet<>();
if (incoming != null) {
result.addAll(incoming);
}
return expandExistingOwnedList(result, creator, ownersToExpand);
}
}