Advertisement
asolntsev

Untitled

Apr 5th, 2011
2,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. package ru.nullpointer.shitty;
  2.  
  3. import java.io.PrintStream;
  4. import java.util.Iterator;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         int n = 6;
  10.  
  11.         IteratorStrategy<Integer> pis = new PrintIteratorStrategy(System.out);
  12.         IteratorFactory sif = IteratorFactory.getInstance();
  13.  
  14.         IteratorStrategy<Integer> fis = new ForeachStartegy(sif, pis);
  15.  
  16.         Iterator<Integer> it = new ForwardIterator(n);
  17.  
  18.         fis.execute(it);
  19.     }
  20.  
  21.     static public class ForwardIterator implements Iterator<Integer> {
  22.  
  23.         private int capacity;
  24.         private int counter;
  25.  
  26.         public ForwardIterator(int capacity) {
  27.             if (capacity < 1) {
  28.                 throw new IllegalArgumentException("Capacity must be positive");
  29.             }
  30.             this.capacity = capacity;
  31.             this.counter = 1;
  32.         }
  33.  
  34.         @Override
  35.         public boolean hasNext() {
  36.             return counter <= capacity;
  37.         }
  38.  
  39.         @Override
  40.         public Integer next() {
  41.             return counter++;
  42.         }
  43.  
  44.         @Override
  45.         public void remove() {
  46.             throw new UnsupportedOperationException("Not supported yet.");
  47.         }
  48.     }
  49.  
  50.     static public class BackwardIterator implements Iterator<Integer> {
  51.  
  52.         private int counter;
  53.  
  54.         public BackwardIterator(int capacity) {
  55.             if (capacity < 1) {
  56.                 throw new IllegalArgumentException("Capacity must be positive");
  57.             }
  58.             this.counter = capacity;
  59.         }
  60.  
  61.         @Override
  62.         public boolean hasNext() {
  63.             return counter > 0;
  64.         }
  65.  
  66.         @Override
  67.         public Integer next() {
  68.             return counter--;
  69.         }
  70.  
  71.         @Override
  72.         public void remove() {
  73.             throw new UnsupportedOperationException("Not supported yet.");
  74.         }
  75.     }
  76.  
  77.     static public interface IteratorStrategy<E> {
  78.  
  79.         void execute(Iterator<E> it);
  80.     }
  81.  
  82.     static public class PrintIteratorStrategy implements IteratorStrategy<Integer> {
  83.  
  84.         private PrintStream ps;
  85.  
  86.         public PrintIteratorStrategy(PrintStream ps) {
  87.             if (ps == null) {
  88.                 throw new IllegalArgumentException("Print stream can't be null");
  89.             }
  90.             this.ps = ps;
  91.         }
  92.  
  93.         @Override
  94.         public void execute(Iterator<Integer> it) {
  95.             while (it.hasNext()) {
  96.                 ps.print(it.next());
  97.                 if (it.hasNext()) {
  98.                     ps.print('-');
  99.                 }
  100.             }
  101.             ps.println();
  102.         }
  103.     }
  104.  
  105.     static public class ForeachStartegy implements IteratorStrategy<Integer> {
  106.  
  107.         private IteratorFactory sif;
  108.         private IteratorStrategy<Integer> is;
  109.  
  110.         public ForeachStartegy(IteratorFactory sif, IteratorStrategy<Integer> is) {
  111.             if (sif == null) {
  112.                 throw new IllegalArgumentException("Iterator factory can't be null");
  113.             }
  114.             if (is == null) {
  115.                 throw new IllegalArgumentException("Iterator strategy can't be null");
  116.             }
  117.             this.sif = sif;
  118.             this.is = is;
  119.         }
  120.  
  121.         @Override
  122.         public void execute(Iterator<Integer> it) {
  123.             while (it.hasNext()) {
  124.                 Integer capacity = it.next();
  125.                 is.execute(sif.getIterator(capacity));
  126.             }
  127.         }
  128.     }
  129.  
  130.     static public class IteratorFactory {
  131.  
  132.         private static IteratorFactory instance = null;
  133.  
  134.         private IteratorFactory() {
  135.         }
  136.  
  137.         public static IteratorFactory getInstance() {
  138.             if (instance == null) {
  139.                 instance = new IteratorFactory();
  140.             }
  141.             return instance;
  142.         }
  143.  
  144.         public Iterator<Integer> getIterator(int capacity) {
  145.             if (capacity % 2 == 0) {
  146.                 return new BackwardIterator(capacity);
  147.             } else {
  148.                 return new ForwardIterator(capacity);
  149.             }
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement