Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.nullpointer.shitty;
- import java.io.PrintStream;
- import java.util.Iterator;
- public class Main {
- public static void main(String[] args) {
- int n = 6;
- IteratorStrategy<Integer> pis = new PrintIteratorStrategy(System.out);
- IteratorFactory sif = IteratorFactory.getInstance();
- IteratorStrategy<Integer> fis = new ForeachStartegy(sif, pis);
- Iterator<Integer> it = new ForwardIterator(n);
- fis.execute(it);
- }
- static public class ForwardIterator implements Iterator<Integer> {
- private int capacity;
- private int counter;
- public ForwardIterator(int capacity) {
- if (capacity < 1) {
- throw new IllegalArgumentException("Capacity must be positive");
- }
- this.capacity = capacity;
- this.counter = 1;
- }
- @Override
- public boolean hasNext() {
- return counter <= capacity;
- }
- @Override
- public Integer next() {
- return counter++;
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- }
- static public class BackwardIterator implements Iterator<Integer> {
- private int counter;
- public BackwardIterator(int capacity) {
- if (capacity < 1) {
- throw new IllegalArgumentException("Capacity must be positive");
- }
- this.counter = capacity;
- }
- @Override
- public boolean hasNext() {
- return counter > 0;
- }
- @Override
- public Integer next() {
- return counter--;
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- }
- static public interface IteratorStrategy<E> {
- void execute(Iterator<E> it);
- }
- static public class PrintIteratorStrategy implements IteratorStrategy<Integer> {
- private PrintStream ps;
- public PrintIteratorStrategy(PrintStream ps) {
- if (ps == null) {
- throw new IllegalArgumentException("Print stream can't be null");
- }
- this.ps = ps;
- }
- @Override
- public void execute(Iterator<Integer> it) {
- while (it.hasNext()) {
- ps.print(it.next());
- if (it.hasNext()) {
- ps.print('-');
- }
- }
- ps.println();
- }
- }
- static public class ForeachStartegy implements IteratorStrategy<Integer> {
- private IteratorFactory sif;
- private IteratorStrategy<Integer> is;
- public ForeachStartegy(IteratorFactory sif, IteratorStrategy<Integer> is) {
- if (sif == null) {
- throw new IllegalArgumentException("Iterator factory can't be null");
- }
- if (is == null) {
- throw new IllegalArgumentException("Iterator strategy can't be null");
- }
- this.sif = sif;
- this.is = is;
- }
- @Override
- public void execute(Iterator<Integer> it) {
- while (it.hasNext()) {
- Integer capacity = it.next();
- is.execute(sif.getIterator(capacity));
- }
- }
- }
- static public class IteratorFactory {
- private static IteratorFactory instance = null;
- private IteratorFactory() {
- }
- public static IteratorFactory getInstance() {
- if (instance == null) {
- instance = new IteratorFactory();
- }
- return instance;
- }
- public Iterator<Integer> getIterator(int capacity) {
- if (capacity % 2 == 0) {
- return new BackwardIterator(capacity);
- } else {
- return new ForwardIterator(capacity);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement