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