001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.api.model.impl;
023
024import java.util.Iterator;
025import java.util.NoSuchElementException;
026
027import org.nuxeo.ecm.core.api.model.Property;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public class DirtyPropertyIterator implements Iterator<Property> {
033
034    private final Iterator<Property> it;
035
036    private Property property; // the current property - null if no intialized
037
038    private Property next; // the last seen property by hasNext() - null if no initialized
039
040    public DirtyPropertyIterator(Iterator<Property> it) {
041        this.it = it;
042    }
043
044    @Override
045    public boolean hasNext() {
046        if (next != null) {
047            return true;
048        }
049        while (it.hasNext()) {
050            next = it.next();
051            if (next.isDirty()) {
052                return true;
053            }
054        }
055        next = null;
056        return false;
057    }
058
059    @Override
060    public Property next() {
061        if (!hasNext()) {
062            throw new NoSuchElementException("No more elements to iterate over");
063        }
064        property = next;
065        next = null;
066        return property;
067    }
068
069    @Override
070    public void remove() {
071        if (property == null) {
072            throw new IllegalStateException("Cannot call remove on a non initialized iterator");
073        }
074        property.remove();
075    }
076
077}