View Javadoc

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.ArrayList;
27  import java.util.Collection;
28  import java.util.Iterator;
29  import java.util.List;
30  import net.jcip.annotations.Immutable;
31  import org.modeshape.common.util.CheckArg;
32  import org.modeshape.graph.property.Name;
33  import org.modeshape.graph.property.Property;
34  import org.modeshape.graph.property.PropertyFactory;
35  import org.modeshape.graph.property.PropertyType;
36  import org.modeshape.graph.property.ValueFactories;
37  import org.modeshape.graph.property.ValueFactory;
38  
39  /**
40   * A basic {@link PropertyFactory} implementation.
41   */
42  @Immutable
43  public class BasicPropertyFactory implements PropertyFactory {
44  
45      private final ValueFactories factories;
46  
47      /**
48       * @param valueFactories the value factories
49       * @throws IllegalArgumentException if the reference to the value factories is null
50       */
51      public BasicPropertyFactory( ValueFactories valueFactories ) {
52          CheckArg.isNotNull(valueFactories, "value factories");
53          this.factories = valueFactories;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      public Property create( Name name,
60                              Iterable<?> values ) {
61          return create(name, PropertyType.OBJECT, values);
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      public Property create( Name name,
68                              Iterator<?> values ) {
69          return create(name, PropertyType.OBJECT, values);
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      public Property create( Name name,
76                              Object... values ) {
77          return create(name, PropertyType.OBJECT, values);
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      public Property create( Name name,
84                              PropertyType desiredType,
85                              Object... values ) {
86          CheckArg.isNotNull(name, "name");
87          if (values == null || values.length == 0) {
88              return new BasicEmptyProperty(name);
89          }
90          final int len = values.length;
91          if (desiredType == null) desiredType = PropertyType.OBJECT;
92          final ValueFactory<?> factory = factories.getValueFactory(desiredType);
93          if (values.length == 1) {
94              Object value = values[0];
95              // Check whether the sole value was a collection ...
96              if (value instanceof Collection<?>) {
97                  // The single value is a collection, so create property with the collection's contents ...
98                  return create(name, (Collection<?>)value);
99              }
100             value = factory.create(values[0]);
101             return new BasicSingleValueProperty(name, value);
102         }
103         List<Object> valueList = new ArrayList<Object>(len);
104         for (int i = 0; i != len; ++i) {
105             Object value = factory.create(values[i]);
106             valueList.add(value);
107         }
108         return new BasicMultiValueProperty(name, valueList);
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @SuppressWarnings( "unchecked" )
115     public Property create( Name name,
116                             PropertyType desiredType,
117                             Iterable<?> values ) {
118         CheckArg.isNotNull(name, "name");
119         List<Object> valueList = null;
120         if (values instanceof Collection) {
121             Collection<Object> originalValues = (Collection<Object>)values;
122             if (originalValues.isEmpty()) {
123                 return new BasicEmptyProperty(name);
124             }
125             valueList = new ArrayList<Object>(originalValues.size());
126         } else {
127             // We don't know the size
128             valueList = new ArrayList<Object>();
129         }
130         // Copy the values, ensuring that the values are the correct type ...
131         if (desiredType == null) desiredType = PropertyType.OBJECT;
132         final ValueFactory<?> factory = factories.getValueFactory(desiredType);
133         for (Object value : values) {
134             valueList.add(factory.create(value));
135         }
136         if (valueList.isEmpty()) { // may not have been a collection earlier
137             return new BasicEmptyProperty(name);
138         }
139         if (valueList.size() == 1) {
140             return new BasicSingleValueProperty(name, valueList.get(0));
141         }
142         return new BasicMultiValueProperty(name, valueList);
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     public Property create( Name name,
149                             PropertyType desiredType,
150                             Iterator<?> values ) {
151         CheckArg.isNotNull(name, "name");
152         final List<Object> valueList = new ArrayList<Object>();
153         if (desiredType == null) desiredType = PropertyType.OBJECT;
154         final ValueFactory<?> factory = factories.getValueFactory(desiredType);
155         while (values.hasNext()) {
156             Object value = values.next();
157             value = factory.create(value);
158             valueList.add(value);
159         }
160         if (valueList.isEmpty()) {
161             return new BasicEmptyProperty(name);
162         }
163         if (valueList.size() == 1) {
164             return new BasicSingleValueProperty(name, valueList.get(0));
165         }
166         return new BasicMultiValueProperty(name, valueList);
167     }
168 
169 }