Advertisement
Exception_Prototype

ListPaginatorPages

Sep 24th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. /*Вывод:
  2. -> 1 <- | 2 | 3 | 4 | 5
  3. 1 | -> 2 <- | 3 | 4 | 5
  4. 1 | 2 | -> 3 <- | 4 | 5
  5. 2 | 3 | -> 4 <- | 5 | 6
  6. 3 | 4 | -> 5 <- | 6 | 7
  7. 4 | 5 | -> 6 <- | 7 | 8
  8. 5 | 6 | -> 7 <- | 8 | 9
  9. 6 | 7 | -> 8 <- | 9 | 10
  10. 7 | 8 | -> 9 <- | 10 | 11
  11. 8 | 9 | -> 10 <- | 11 | 12
  12. 9 | 10 | -> 11 <- | 12 | 13
  13. 10 | 11 | -> 12 <- | 13 | 14
  14. 11 | 12 | -> 13 <- | 14 | 15
  15. 12 | 13 | -> 14 <- | 15 | 16
  16. 13 | 14 | -> 15 <- | 16 | 17
  17. 14 | 15 | -> 16 <- | 17 | 18
  18. 15 | 16 | -> 17 <- | 18 | 19
  19. 16 | 17 | -> 18 <- | 19 | 20
  20. 16 | 17 | 18 | -> 19 <- | 20
  21. 16 | 17 | 18 | 19 | -> 20 <-*/
  22.  
  23.  
  24. public final class ListPaginatorPages {
  25.  
  26.     public static void main(String[] args) {
  27.  
  28.         final int minPage = 1;
  29.         final int maxPage = 20;
  30.  
  31.         final Page page = new Page(minPage, maxPage);
  32.  
  33.         for (int currentPage = minPage; currentPage <= maxPage; currentPage++) {
  34.             page.apply(currentPage);
  35.             System.out.println(page);
  36.         }
  37.  
  38.     }
  39.  
  40.     private static final class Page {
  41.  
  42.         private final int minPage, maxPage;
  43.  
  44.         private final List<String> pages;
  45.  
  46.         Page(int minPage, int maxPage) {
  47.             this.minPage = minPage;
  48.             this.maxPage = maxPage;
  49.             this.pages = new ArrayList<>();
  50.             apply(minPage);
  51.         }
  52.  
  53.         void apply(int currentPage) {
  54.  
  55.             if (!(currentPage >= this.minPage && currentPage <= this.maxPage)) {
  56.                 throw new IllegalArgumentException();
  57.             }
  58.  
  59.             this.pages.clear();
  60.  
  61.             int minBorder = this.minPage, maxBorder = this.maxPage;
  62.  
  63.             while ((maxBorder - minBorder) != 4) {
  64.  
  65.                 if ((minBorder < (currentPage - 2))) {
  66.                     minBorder++;
  67.                 }
  68.  
  69.                 if ((maxBorder > (currentPage + 2))) {
  70.                     maxBorder--;
  71.                 }
  72.  
  73.             }
  74.  
  75.             for (int i = minBorder; i <= maxBorder; i++) {
  76.                 this.pages.add(i == currentPage ? String.format("-> %d <-", i) : Integer.toString(i));
  77.             }
  78.  
  79.         }
  80.  
  81.         @Override
  82.         public String toString() {
  83.             return String.join(" | ", this.pages);
  84.         }
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement