001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.graph.property.basic;
025    
026    import java.util.Iterator;
027    import org.jboss.dna.common.util.CheckArg;
028    import org.jboss.dna.graph.property.PropertyType;
029    import org.jboss.dna.graph.property.ValueFactories;
030    import org.jboss.dna.graph.property.ValueFactory;
031    
032    /**
033     * Abstract implementation of {@link ValueFactories} that implements all the methods other than the <code>get*Factory()</code>
034     * methods. Subclasses can simply implement these methods and inherit the {@link #iterator()}, {@link #getValueFactory(Object)}
035     * and {@link #getValueFactory(PropertyType)} method implementations.
036     * 
037     * @author Randall Hauch
038     */
039    public abstract class AbstractValueFactories implements ValueFactories {
040    
041        /**
042         * <p>
043         * {@inheritDoc}
044         * </p>
045         * <p>
046         * This implementation always iterates over the instances return by the <code>get*Factory()</code> methods.
047         * </p>
048         */
049        public Iterator<ValueFactory<?>> iterator() {
050            return new ValueFactoryIterator();
051        }
052    
053        /**
054         * {@inheritDoc}
055         */
056        public ValueFactory<?> getValueFactory( PropertyType type ) {
057            CheckArg.isNotNull(type, "type");
058            switch (type) {
059                case BINARY:
060                    return getBinaryFactory();
061                case BOOLEAN:
062                    return getBooleanFactory();
063                case DATE:
064                    return getDateFactory();
065                case DECIMAL:
066                    return getDecimalFactory();
067                case DOUBLE:
068                    return getDoubleFactory();
069                case LONG:
070                    return getLongFactory();
071                case NAME:
072                    return getNameFactory();
073                case PATH:
074                    return getPathFactory();
075                case REFERENCE:
076                    return getReferenceFactory();
077                case STRING:
078                    return getStringFactory();
079                case URI:
080                    return getUriFactory();
081                case UUID:
082                    return getUuidFactory();
083                case OBJECT:
084                    return getObjectFactory();
085            }
086            return getObjectFactory();
087        }
088    
089        /**
090         * {@inheritDoc}
091         */
092        public ValueFactory<?> getValueFactory( Object prototype ) {
093            CheckArg.isNotNull(prototype, "prototype");
094            PropertyType inferredType = PropertyType.discoverType(prototype);
095            assert inferredType != null;
096            return getValueFactory(inferredType);
097        }
098    
099        protected class ValueFactoryIterator implements Iterator<ValueFactory<?>> {
100            private final Iterator<PropertyType> propertyTypeIter = PropertyType.iterator();
101    
102            protected ValueFactoryIterator() {
103            }
104    
105            /**
106             * {@inheritDoc}
107             * 
108             * @see java.util.Iterator#hasNext()
109             */
110            public boolean hasNext() {
111                return propertyTypeIter.hasNext();
112            }
113    
114            /**
115             * {@inheritDoc}
116             * 
117             * @see java.util.Iterator#next()
118             */
119            public ValueFactory<?> next() {
120                PropertyType nextType = propertyTypeIter.next();
121                return getValueFactory(nextType);
122            }
123    
124            /**
125             * {@inheritDoc}
126             * 
127             * @see java.util.Iterator#remove()
128             */
129            public void remove() {
130                throw new UnsupportedOperationException();
131            }
132        }
133    
134    }