001/** 002 * Copyright 2014 DuraSpace, Inc. 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 */ 016package org.fcrepo.kernel.impl.rdf.impl.mappings; 017 018import com.google.common.collect.AbstractIterator; 019import com.google.common.collect.Iterators; 020import org.fcrepo.kernel.exception.RepositoryRuntimeException; 021 022import javax.jcr.Property; 023import javax.jcr.RepositoryException; 024import javax.jcr.Value; 025import java.util.Iterator; 026 027/** 028 * Iterate over all the values in a property or list of properties 029 * 030 * @author cabeer 031 */ 032public class PropertyValueIterator extends AbstractIterator<Value> { 033 private Iterator<Property> properties; 034 private Iterator<Value> currentValues; 035 036 /** 037 * Iterate through a single property's values 038 * @param properties 039 */ 040 public PropertyValueIterator(final Property properties) { 041 this(Iterators.singletonIterator(properties)); 042 } 043 044 /** 045 * Iterate through multiple property's values 046 * @param properties 047 */ 048 public PropertyValueIterator(final Iterator<Property> properties) { 049 this.properties = properties; 050 this.currentValues = null; 051 } 052 053 @Override 054 protected Value computeNext() { 055 try { 056 if (currentValues != null && currentValues.hasNext()) { 057 return currentValues.next(); 058 } 059 if (properties.hasNext()) { 060 final Property property = properties.next(); 061 062 if (property.isMultiple()) { 063 currentValues = Iterators.forArray(property.getValues()); 064 return currentValues.next(); 065 } else { 066 currentValues = null; 067 return property.getValue(); 068 } 069 } 070 071 return endOfData(); 072 } catch (final RepositoryException e) { 073 throw new RepositoryRuntimeException(e); 074 } 075 } 076}