001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.api.model.impl;
016
017import java.util.Iterator;
018import java.util.NoSuchElementException;
019
020import org.nuxeo.ecm.core.api.model.Property;
021
022/**
023 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
024 */
025public class DirtyPropertyIterator implements Iterator<Property> {
026
027    private final Iterator<Property> it;
028
029    private Property property; // the current property - null if no intialized
030
031    private Property next; // the last seen property by hasNext() - null if no initialized
032
033    public DirtyPropertyIterator(Iterator<Property> it) {
034        this.it = it;
035    }
036
037    @Override
038    public boolean hasNext() {
039        if (next != null) {
040            return true;
041        }
042        while (it.hasNext()) {
043            next = it.next();
044            if (next.isDirty()) {
045                return true;
046            }
047        }
048        next = null;
049        return false;
050    }
051
052    @Override
053    public Property next() {
054        if (!hasNext()) {
055            throw new NoSuchElementException("No more elements to iterate over");
056        }
057        property = next;
058        next = null;
059        return property;
060    }
061
062    @Override
063    public void remove() {
064        if (property == null) {
065            throw new IllegalStateException("Cannot call remove on a non initialized iterator");
066        }
067        property.remove();
068    }
069
070}