001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape.rdf.impl.mappings;
019
020import static com.google.common.collect.Iterators.singletonIterator;
021
022import com.google.common.collect.AbstractIterator;
023import com.google.common.collect.Iterators;
024
025import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
026import javax.jcr.Property;
027import javax.jcr.RepositoryException;
028import javax.jcr.Value;
029
030import java.util.Iterator;
031
032/**
033 * Iterate over all the values in a property or list of properties
034 *
035 * @author cabeer
036 */
037public class PropertyValueIterator extends AbstractIterator<Value> {
038    private final Iterator<Property> properties;
039    private Iterator<Value> currentValues;
040
041    /**
042     * Iterate through multiple properties' values
043     * @param properties the properties
044     */
045    public PropertyValueIterator(final Iterator<Property> properties) {
046        this.properties = properties;
047        this.currentValues = null;
048    }
049
050    /**
051     * Iterate through a property's values
052     * @param property the properties
053     */
054    public PropertyValueIterator(final Property property) {
055        this.properties = singletonIterator(property);
056        this.currentValues = null;
057    }
058
059    @Override
060    protected Value computeNext() {
061        try {
062            if (currentValues != null && currentValues.hasNext()) {
063                return currentValues.next();
064            }
065            if (properties.hasNext()) {
066                final Property property = properties.next();
067
068                if (property.isMultiple()) {
069                    currentValues = Iterators.forArray(property.getValues());
070                    if (currentValues.hasNext()) {
071                        return currentValues.next();
072                    }
073                    return endOfData();
074                }
075                currentValues = null;
076                return property.getValue();
077            }
078
079            return endOfData();
080        } catch (final RepositoryException e) {
081            throw new RepositoryRuntimeException(e);
082        }
083    }
084}