/*
* Copyright (C) 2021 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import java.util.NoSuchElementException;
/**
*
* @author Gabriel Venberg
*/
public class LuckyNumberList {
private LinkedPositionalList luckyNumbers;
public LuckyNumberList(){
luckyNumbers = new LinkedPositionalList();
}
public void addLuckyNumber(LuckyNumber l){
luckyNumbers.addLast(l);
}
private class positionIterator implements Iterator>{
private Position cursor = luckyNumbers.first();
private Position recent = null;
public boolean hasNext(){return(cursor!=null);}
public Position next() throws NoSuchElementException {
if(cursor==null){throw new NoSuchElementException("nothing left");}
recent = cursor;
cursor = luckyNumbers.after(cursor);
return recent;
}
public void remove() throws IllegalStateException{
if(recent==null){throw new IllegalStateException("nothing to remove");}
luckyNumbers.remove(recent);
recent=null;
}
}
private class positionIterable implements Iterable>{
public Iterator> iterator(){return new positionIterator();}
}
public Iterable> positions(){
return new positionIterable();
}
private class EvenPositionIterator implements Iterator>{
private Position cursor = luckyNumbers.first();
private Position recent = null;
public boolean hasNext(){return(cursor!=null);}
public Position next() throws NoSuchElementException {
if(cursor==null){throw new NoSuchElementException("nothing left");}
if(recent==null){
while((cursor != null) && !(cursor.getElement().getLuckyNumber()%2==0)){
cursor = luckyNumbers.after(cursor);
}
}
recent = cursor;
cursor = luckyNumbers.after(cursor);
//advance cursor to next even number
while((cursor != null) && !(cursor.getElement().getLuckyNumber()%2==0)){
cursor = luckyNumbers.after(cursor);
}
return recent;
}
public void remove() throws IllegalStateException{
if(recent==null){throw new IllegalStateException("nothing to remove");}
luckyNumbers.remove(recent);
recent=null;
}
}
private class EvenPositionIterable implements Iterable>{
public Iterator> iterator(){return new EvenPositionIterator();}
}
public Iterable> EvenPositions(){
return new EvenPositionIterable();
}
//utility class
private boolean isPrime(int n){
for(int i=2; i>{
private Position cursor = luckyNumbers.first();
private Position recent = null;
public boolean hasNext(){return(cursor!=null);}
public Position next() throws NoSuchElementException {
if(cursor==null){throw new NoSuchElementException("nothing left");}
if(recent==null){
while(cursor != null && !isPrime(cursor.getElement().getLuckyNumber())){
cursor = luckyNumbers.after(cursor);
}
}
recent = cursor;
cursor = luckyNumbers.after(cursor);
//advance cursor to next even number
while(cursor != null && !isPrime(cursor.getElement().getLuckyNumber())){
cursor = luckyNumbers.after(cursor);
}
return recent;
}
public void remove() throws IllegalStateException{
if(recent==null){throw new IllegalStateException("nothing to remove");}
luckyNumbers.remove(recent);
recent=null;
}
}
private class PrimePositionIterable implements Iterable>{
public Iterator> iterator(){return new PrimePositionIterator();}
}
public Iterable> PrimePositions(){
return new PrimePositionIterable();
}
}