1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.graph.property.basic;
25
26 import java.util.Iterator;
27 import net.jcip.annotations.ThreadSafe;
28 import org.modeshape.common.util.CheckArg;
29 import org.modeshape.graph.property.PropertyType;
30 import org.modeshape.graph.property.ValueFactories;
31 import org.modeshape.graph.property.ValueFactory;
32
33 /**
34 * Abstract implementation of {@link ValueFactories} that implements all the methods other than the <code>get*Factory()</code>
35 * methods. Subclasses can simply implement these methods and inherit the {@link #iterator()}, {@link #getValueFactory(Object)}
36 * and {@link #getValueFactory(PropertyType)} method implementations.
37 */
38 @ThreadSafe
39 public abstract class AbstractValueFactories implements ValueFactories {
40
41 /**
42 * <p>
43 * {@inheritDoc}
44 * </p>
45 * <p>
46 * This implementation always iterates over the instances return by the <code>get*Factory()</code> methods.
47 * </p>
48 */
49 public Iterator<ValueFactory<?>> iterator() {
50 return new ValueFactoryIterator();
51 }
52
53 /**
54 * {@inheritDoc}
55 */
56 public ValueFactory<?> getValueFactory( PropertyType type ) {
57 CheckArg.isNotNull(type, "type");
58 switch (type) {
59 case BINARY:
60 return getBinaryFactory();
61 case BOOLEAN:
62 return getBooleanFactory();
63 case DATE:
64 return getDateFactory();
65 case DECIMAL:
66 return getDecimalFactory();
67 case DOUBLE:
68 return getDoubleFactory();
69 case LONG:
70 return getLongFactory();
71 case NAME:
72 return getNameFactory();
73 case PATH:
74 return getPathFactory();
75 case REFERENCE:
76 return getReferenceFactory();
77 case STRING:
78 return getStringFactory();
79 case URI:
80 return getUriFactory();
81 case UUID:
82 return getUuidFactory();
83 case OBJECT:
84 return getObjectFactory();
85 }
86 return getObjectFactory();
87 }
88
89 /**
90 * {@inheritDoc}
91 */
92 public ValueFactory<?> getValueFactory( Object prototype ) {
93 CheckArg.isNotNull(prototype, "prototype");
94 PropertyType inferredType = PropertyType.discoverType(prototype);
95 assert inferredType != null;
96 return getValueFactory(inferredType);
97 }
98
99 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 }