You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

163 lines
4.8 KiB

package repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class Repository<T> {
private static final Logger logger = LoggerFactory.getLogger(Repository.class);
private final List<T> items;
private final Class<T> entityClass;
public Repository(Class<T> entityClass) {
if (entityClass == null) {
throw new IllegalArgumentException("Entity class cannot be null");
}
this.entityClass = entityClass;
this.items = new ArrayList<>();
logger.debug("Repository 初始化: entityClass={}, initialCapacity={}",
entityClass.getSimpleName(), 16);
}
public void add(T item) {
if (item == null) {
logger.error("尝试添加 null 项目到 Repository");
throw new IllegalArgumentException("Cannot add null item to repository");
}
validateItem(item);
items.add(item);
logger.debug("添加项目到 Repository: {}", item.getClass().getSimpleName());
}
public void addAll(List<T> items) {
if (items == null) {
logger.error("尝试添加 null 列表到 Repository");
throw new IllegalArgumentException("Cannot add null list to repository");
}
logger.debug("批量添加 {} 个项目到 Repository", items.size());
for (T item : items) {
add(item);
}
}
public boolean remove(T item) {
if (item == null) {
logger.warn("尝试移除 null 项目");
return false;
}
boolean removed = items.remove(item);
if (removed) {
logger.debug("从 Repository 移除项目: {}", item.getClass().getSimpleName());
}
return removed;
}
public T get(int index) {
validateIndex(index);
return items.get(index);
}
public List<T> getAll() {
logger.debug("获取 Repository 所有项目,当前数量: {}", items.size());
return new ArrayList<>(items);
}
public T getFirst() {
if (items.isEmpty()) {
logger.debug("Repository 为空,无法获取第一个项目");
return null;
}
return items.get(0);
}
public T getLast() {
if (items.isEmpty()) {
logger.debug("Repository 为空,无法获取最后一个项目");
return null;
}
return items.get(items.size() - 1);
}
public int size() {
return items.size();
}
public boolean isEmpty() {
return items.isEmpty();
}
public boolean contains(T item) {
if (item == null) {
return false;
}
return items.contains(item);
}
public void clear() {
int size = items.size();
items.clear();
logger.debug("清空 Repository,移除了 {} 个项目", size);
}
public List<T> findAll(Predicate<T> predicate) {
if (predicate == null) {
logger.error("Predicate 不能为 null");
throw new IllegalArgumentException("Predicate cannot be null");
}
List<T> result = new ArrayList<>();
for (T item : items) {
if (predicate.test(item)) {
result.add(item);
}
}
logger.debug("根据条件过滤,结果数量: {} / {}", result.size(), items.size());
return result;
}
public T findFirst(Predicate<T> predicate) {
if (predicate == null) {
logger.error("Predicate 不能为 null");
throw new IllegalArgumentException("Predicate cannot be null");
}
for (T item : items) {
if (predicate.test(item)) {
return item;
}
}
return null;
}
public void update(int index, T item) {
validateIndex(index);
if (item == null) {
logger.error("尝试用 null 项目更新 Repository");
throw new IllegalArgumentException("Cannot update with null item");
}
validateItem(item);
logger.debug("更新 Repository 索引 {} 处的项目", index);
items.set(index, item);
}
private void validateIndex(int index) {
if (index < 0 || index >= items.size()) {
String error = String.format(
"Index %d out of bounds for repository size %d", index, items.size());
logger.error("索引验证失败: {}", error);
throw new IndexOutOfBoundsException(error);
}
}
private void validateItem(T item) {
if (item == null) {
throw new IllegalArgumentException("Repository does not accept null items");
}
}
public Class<T> getEntityClass() {
return entityClass;
}
}