// Path: src/main/java/org/letustakearest/application/service/Pagination.java
// @Getter
// public final class Pagination {
// 
//     private int offset;
// 
//     private int limit;
// 
//     public Pagination(final int offset, final int limit) {
//         this.offset = offset;
//         this.limit = limit;
//     }
// 
//     public static final Pagination DEFAULT = new Pagination(0, 2);
// 
//     public static Pagination getPagination(final Integer offset, final Integer limit) {
//         return new Pagination(offset == null ? DEFAULT.offset : offset, limit == null ? DEFAULT.limit : limit);
//     }
// 
// }
// 
// Path: src/main/java/org/letustakearest/domain/EntityNotFoundException.java
// public class EntityNotFoundException extends Exception {
// 
//     public EntityNotFoundException() {
//         super();
//     }
// 
//     public EntityNotFoundException(final String message) {
//         super(message);
//     }
// 
//     public EntityNotFoundException(final String message, final Throwable cause) {
//         super(message, cause);
//     }
// 
//     public EntityNotFoundException(final Throwable cause) {
//         super(cause);
//     }
// 
//     protected EntityNotFoundException(final String message, final Throwable cause, final boolean enableSuppression,
//                                       final boolean writableStackTrace) {
//         super(message, cause, enableSuppression, writableStackTrace);
//     }
// 
// }
// 
// Path: src/main/java/org/letustakearest/domain/IdentifiableAndVersioned.java
// @Getter
// @Setter
// public abstract class IdentifiableAndVersioned {
// 
//     private Long id;
// 
//     private int version;
// 
// }
// 
// Path: src/main/java/org/letustakearest/domain/PaginatedResult.java
// @Getter
// public final class PaginatedResult<E extends IdentifiableAndVersioned> {
// 
//     private final List<E> list;
// 
//     private final Pagination actualPagination;
// 
//     private final int total;
// 
//     public PaginatedResult(final List<E> list, final Pagination actualPagination, final int total) {
//         this.list = list;
//         this.actualPagination = actualPagination;
//         this.total = total;
//     }
// 
// }
// 
// Path: src/main/java/org/letustakearest/domain/repository/BaseRepository.java
// public interface BaseRepository<E extends IdentifiableAndVersioned> {
// 
//     E create(E entity);
// 
//     E update(E entity);
// 
//     E findById(Long id) throws EntityNotFoundException;
// 
//     Collection<E> findAll();
// 
//     PaginatedResult<E> findSeveral(Pagination pagination);
// 
//     void delete(Long id);
// 
// }


import org.letustakearest.application.service.Pagination;
import org.letustakearest.domain.EntityNotFoundException;
import org.letustakearest.domain.IdentifiableAndVersioned;
import org.letustakearest.domain.PaginatedResult;
import org.letustakearest.domain.repository.BaseRepository;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
    public E update(final E entity) {
        return store(entity);
    }

    private E store(final E entity) {
        store.put(entity.getId(), entity);
        entity.setVersion(entity.getVersion() + 1);
        return entity;
    }

    @Override
    public E findById(final Long id) throws EntityNotFoundException {
        final E result = store.getOrDefault(id, null);
        if (result != null) {
            return result;
        }
        else {
            throw new EntityNotFoundException(String.format("%s with id %d not found", entityName, id));
        }
    }

    @Override
    public Collection<E> findAll() {
        return store.values();
    }

    @Override
    public PaginatedResult<E> findSeveral(final Pagination pagination) {
        final int startIndex = startIndex(pagination.getOffset());
        final int endIndex = endIndex(startIndex, pagination.getLimit());
// Path: src/main/java/org/letustakearest/application/service/Pagination.java
// @Getter
// public final class Pagination {
// 
//     private int offset;
// 
//     private int limit;
// 
//     public Pagination(final int offset, final int limit) {
//         this.offset = offset;
//         this.limit = limit;
//     }
// 
//     public static final Pagination DEFAULT = new Pagination(0, 2);
// 
//     public static Pagination getPagination(final Integer offset, final Integer limit) {
//         return new Pagination(offset == null ? DEFAULT.offset : offset, limit == null ? DEFAULT.limit : limit);
//     }
// 
// }
// 
// Path: src/main/java/org/letustakearest/domain/EntityNotFoundException.java
// public class EntityNotFoundException extends Exception {
// 
//     public EntityNotFoundException() {
//         super();
//     }
// 
//     public EntityNotFoundException(final String message) {
//         super(message);
//     }
// 
//     public EntityNotFoundException(final String message, final Throwable cause) {
//         super(message, cause);
//     }
// 
//     public EntityNotFoundException(final Throwable cause) {
//         super(cause);
//     }
// 
//     protected EntityNotFoundException(final String message, final Throwable cause, final boolean enableSuppression,
//                                       final boolean writableStackTrace) {
//         super(message, cause, enableSuppression, writableStackTrace);
//     }
// 
// }
// 
// Path: src/main/java/org/letustakearest/domain/IdentifiableAndVersioned.java
// @Getter
// @Setter
// public abstract class IdentifiableAndVersioned {
// 
//     private Long id;
// 
//     private int version;
// 
// }
// 
// Path: src/main/java/org/letustakearest/domain/PaginatedResult.java
// @Getter
// public final class PaginatedResult<E extends IdentifiableAndVersioned> {
// 
//     private final List<E> list;
// 
//     private final Pagination actualPagination;
// 
//     private final int total;
// 
//     public PaginatedResult(final List<E> list, final Pagination actualPagination, final int total) {
//         this.list = list;
//         this.actualPagination = actualPagination;
//         this.total = total;
//     }
// 
// }
// 
// Path: src/main/java/org/letustakearest/domain/repository/BaseRepository.java
// public interface BaseRepository<E extends IdentifiableAndVersioned> {
// 
//     E create(E entity);
// 
//     E update(E entity);
// 
//     E findById(Long id) throws EntityNotFoundException;
// 
//     Collection<E> findAll();
// 
//     PaginatedResult<E> findSeveral(Pagination pagination);
// 
//     void delete(Long id);
// 
// }


// Path: src/main/java/org/letustakearest/domain/repository/impl/BaseInMemoryRepository.java
import org.letustakearest.application.service.Pagination;
import org.letustakearest.domain.EntityNotFoundException;
import org.letustakearest.domain.IdentifiableAndVersioned;
import org.letustakearest.domain.PaginatedResult;
import org.letustakearest.domain.repository.BaseRepository;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

    public E update(final E entity) {
        return store(entity);
    }

    private E store(final E entity) {
        store.put(entity.getId(), entity);
        entity.setVersion(entity.getVersion() + 1);
        return entity;
    }

    @Override
    public E findById(final Long id) throws EntityNotFoundException {
        final E result = store.getOrDefault(id, null);
        if (result != null) {
            return result;
        }
        else {
            throw new EntityNotFoundException(String.format("%s with id %d not found", entityName, id));
        }
    }

    @Override
    public Collection<E> findAll() {
        return store.values();
    }

    @Override
    public PaginatedResult<E> findSeveral(final Pagination pagination) {
        final int startIndex = startIndex(pagination.getOffset());
        final int endIndex = endIndex(startIndex, pagination.getLimit());
        return new PaginatedResult<>(new ArrayList<>(store.values()).subList(startIndex, endIndex),