Commit 3944ebf8 authored by Colin DAMON's avatar Colin DAMON

Merge branch '31-java-pagination-seven' into 'master'

Resolve "Java pagination seven"

Closes #31

See merge request !26
parents 4df4c5ae b66485b7
......@@ -9,3 +9,4 @@ include:
- local: "/gilded-rose/.gitlab-ci.yml"
- local: "/ugly-trivia/.gitlab-ci.yml"
- local: "/borestop/.gitlab-ci.yml"
- local: "/java-pagination-seven/.gitlab-ci.yml"
......@@ -24,3 +24,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
6. [GildedRose](/gilded-rose)
7. [UglyTrivia](/ugly-trivia)
8. [Borestop](/borestop)
9. [Java pagination Seven](/java-pagination-seven)
package-java-pagination-seven:
variables:
PROJECT_FOLDER: "java-pagination-seven"
extends: .java
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "java-pagination-seven/**/*"
# Java pagination Seven
Résolution en TDD et en Java du kata [Pagination Seven](https://codingdojo.org/kata/PaginationSeven/).
- **Auteurs** : Julie CONTE et Colin DAMON
- **Date** : 08/08/2020
- **Langage** : Java
- **Niveau** : Débutant
- **Replay** : [Pagination Seven en Java - Julie et Colin](https://www.youtube.com/watch?v=tK7Ao9F_Dhg)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<version>1.0.0</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>java-parent</artifactId>
<relativePath>../java-parent</relativePath>
</parent>
<version>1.0.0-SNAPSHOT</version>
<artifactId>java-pagination-seven</artifactId>
<name>JavaPaginationSeven</name>
<developers>
<developer>
<email>jconte@ippon.fr</email>
<name>Julie CONTE</name>
</developer>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
</developers>
</project>
package fr.ippon.kata.pagination;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Pagination {
private static final String SPACE = " ";
private static final String ELLIPSIS = " … ";
private final String representation;
private Pagination(PaginationBuilder builder) {
representation = buildRepresentation(builder);
}
private String buildRepresentation(PaginationBuilder builder) {
if (builder.total > 7 && builder.current > builder.total - 4) {
return endingEllipsis(builder);
}
if (builder.total <= 7) {
return fullPages(builder);
}
if (builder.current < 5) {
return startingEllipsis(builder);
}
return twoEllipsisPagination(builder);
}
private String endingEllipsis(PaginationBuilder builder) {
return "1" + ELLIPSIS + rangeRepresentation(builder.total - 4, builder.total, builder.current);
}
private String fullPages(PaginationBuilder builder) {
int end = builder.total;
return rangeRepresentation(1, end, builder.current);
}
private String startingEllipsis(PaginationBuilder builder) {
return rangeRepresentation(1, 5, builder.current) + ELLIPSIS + builder.total;
}
private String twoEllipsisPagination(PaginationBuilder builder) {
int start = builder.current - 1;
int end = builder.current + 1;
return "1" + ELLIPSIS + rangeRepresentation(start, end, builder.current) + ELLIPSIS + builder.total;
}
private String rangeRepresentation(int start, int end, int current) {
return IntStream.range(start, end + 1).mapToObj(toPage(current)).collect(Collectors.joining(SPACE));
}
private IntFunction<String> toPage(int current) {
return value -> {
if (current == value) {
return currentPageRepresentation(value);
}
return String.valueOf(value);
};
}
private String currentPageRepresentation(int value) {
return "(" + value + ")";
}
public static PaginationBuilder current(int current) {
return new PaginationBuilder(current);
}
private String display() {
return representation;
}
public static class PaginationBuilder {
private final int current;
private int total;
public PaginationBuilder(int current) {
this.current = current;
}
public PaginationBuilder total(int total) {
this.total = total;
return this;
}
public String display() {
return new Pagination(this).display();
}
}
}
package fr.ippon.kata.pagination;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PaginationTest {
@Test
void shouldGetPageTwoOfFive() {
assertThat(Pagination.current(2).total(5).display()).isEqualTo("1 (2) 3 4 5");
}
@Test
void shouldDisplayFourtyTwoOfHundred() {
assertThat(Pagination.current(42).total(100).display()).isEqualTo("1 … 41 (42) 43 … 100");
}
@Test
void shouldDisplayPageTwoOfNine() {
assertThat(Pagination.current(2).total(9).display()).isEqualTo("1 (2) 3 4 5 … 9");
}
@Test
void shouldDisplayPageOneOfSeven() {
assertThat(Pagination.current(1).total(7).display()).isEqualTo("(1) 2 3 4 5 6 7");
}
@Test
void shouldDisplayPageEightOfNine() {
assertThat(Pagination.current(8).total(9).display()).isEqualTo("1 … 5 6 7 (8) 9");
}
@Test
void shouldDisplayPageFiveOfNine() {
assertThat(Pagination.current(5).total(9).display()).isEqualTo("1 … 4 (5) 6 … 9");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment