Skip to content

Instantly share code, notes, and snippets.

@pkaramishev
Created February 15, 2018 11:29
Show Gist options
  • Select an option

  • Save pkaramishev/b15fa81f46e69a4e7fe770620a098d1b to your computer and use it in GitHub Desktop.

Select an option

Save pkaramishev/b15fa81f46e69a4e7fe770620a098d1b to your computer and use it in GitHub Desktop.
/**
* This portion of the source code is copyrighted by Thumbtack Technology LLC,
* all rights reserved, and is subject to the terms of a license agreement
* in which it constitutes “Pre-existing work” or "Licensed Product".
*/
package net.thumbtack.adtech.campaign.manager;
import net.thumbtack.adtech.campaign.manager.exceptions.InvalidDBException;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Enum repository.
* @param <T> type
*/
public interface EnumRepository<T extends Entity> extends CampaignManagementRepository<T, Integer> {
/**
* Finds entity by name.
*/
Optional<T> findOneByName(String name);
/**
* Same as findOneByName but throws an exception in case of empty result.
*/
default T findOneByNameUnsafe(String name) {
return findOneByName(name).orElseThrow(() -> new InvalidDBException(name + " not found"));
}
/** Return true if list contains all elements of registry.
* @param items Sorted ids list
*/
default <T extends Entity> boolean containsAll(List<T> items) {
final Set<Integer> existingIds = findAll().stream().map(Entity::getId).collect(Collectors.toSet());
final Set<Integer> givenIds = items.stream().map(T::getId).collect(Collectors.toSet());
return existingIds.equals(givenIds);
}
/**
* Find all entity except specified ids. Sorted by id.
*/
default List<T> findAllExceptAllIds(List<Integer> ids) {
final Set<Integer> idsSet = new HashSet<>(ids);
return findAll().stream()
.filter(entity -> !idsSet.contains(entity.getId()))
.collect(Collectors.toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment