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.query.model;
25
26 import java.math.BigDecimal;
27 import java.util.Comparator;
28 import java.util.Set;
29 import net.jcip.annotations.Immutable;
30 import org.modeshape.graph.property.ValueFormatException;
31
32 /**
33 * An interface that defines the value types used in tuples.
34 */
35 @Immutable
36 public interface TypeSystem {
37
38 /**
39 * Get the type factory given the name of the type.
40 *
41 * @param typeName the name of the type
42 * @return the type factory, or null if there is no such type in this system
43 */
44 TypeFactory<?> getTypeFactory( String typeName );
45
46 /**
47 * Get the type factory for the type denoted by the supplied prototype value.
48 *
49 * @param prototype the value whose type is to be identified
50 * @return the type factory, or null if there is no such type in this system
51 */
52 TypeFactory<?> getTypeFactory( Object prototype );
53
54 /**
55 * Get the type factory for boolean types.
56 *
57 * @return the boolean factory; never null
58 */
59 TypeFactory<Boolean> getBooleanFactory();
60
61 /**
62 * Get the type factory for long types.
63 *
64 * @return the long factory; never null
65 */
66 TypeFactory<Long> getLongFactory();
67
68 /**
69 * Get the type factory for string types.
70 *
71 * @return the string factory; never null
72 */
73 TypeFactory<String> getStringFactory();
74
75 /**
76 * Get the type factory for double types.
77 *
78 * @return the double factory; never null
79 */
80 TypeFactory<Double> getDoubleFactory();
81
82 /**
83 * Get the type factory for decimal types.
84 *
85 * @return the decimal factory; never null
86 */
87 TypeFactory<BigDecimal> getDecimalFactory();
88
89 /**
90 * Get the type factory for date-time objects.
91 *
92 * @return the date-time factory, or null if this type system doesn't support date-time objects
93 */
94 TypeFactory<?> getDateTimeFactory();
95
96 /**
97 * Get the type factory for path objects.
98 *
99 * @return the path factory, or null if this type system doesn't support path objects
100 */
101 TypeFactory<?> getPathFactory();
102
103 /**
104 * Get the type factory for references objects.
105 *
106 * @return the reference factory, or null if this type system doesn't support reference objects
107 */
108 TypeFactory<?> getReferenceFactory();
109
110 /**
111 * Get the type factory for binary objects.
112 *
113 * @return the binary factory, or null if this type system doesn't support binary objects
114 */
115 TypeFactory<?> getBinaryFactory();
116
117 /**
118 * Get the name of the type that is used by default.
119 *
120 * @return the default type name; never null
121 */
122 String getDefaultType();
123
124 /**
125 * Get the comparator that should be used by default.
126 *
127 * @return the default comparator; never null
128 */
129 Comparator<Object> getDefaultComparator();
130
131 /**
132 * Get the names of the supported types.
133 *
134 * @return the immutable set of the uppercase names of the types; never null
135 */
136 Set<String> getTypeNames();
137
138 /**
139 * Get the string representation of the supplied value, using the most appropriate factory given the value.
140 *
141 * @param value the value
142 * @return the string representation; never null
143 */
144 String asString( Object value );
145
146 /**
147 * Get the type that is compatible with both of the supplied types.
148 *
149 * @param type1 the first type; may be null
150 * @param type2 the second type; may be null
151 * @return the compatible type; never null
152 */
153 String getCompatibleType( String type1,
154 String type2 );
155
156 /**
157 * Factory interface for creating values from strings.
158 *
159 * @param <T> the type of value object
160 */
161 public interface TypeFactory<T> {
162
163 /**
164 * Get the class representing the value type.
165 *
166 * @return the value type; never null
167 */
168 Class<T> getType();
169
170 /**
171 * Get a comparator that can be used to store the values of this type.
172 *
173 * @return the comparator; never null
174 */
175 Comparator<T> getComparator();
176
177 /**
178 * Get the name of the type created by this factory.
179 *
180 * @return the type name; never null and never empty or blank
181 */
182 String getTypeName();
183
184 /**
185 * Create the typed representation of the value given the supplied string representation.
186 *
187 * @param value the string representation of the value
188 * @return the typed representation, which will be an instance of the {@link #getType() type}
189 * @throws ValueFormatException if the string cannot be converted to a typed value
190 */
191 T create( String value ) throws ValueFormatException;
192
193 /**
194 * Create the typed representation of the value given the supplied object representation.
195 *
196 * @param value the object representation of the value
197 * @return the typed representation, which will be an instance of the {@link #getType() type}
198 * @throws ValueFormatException if the object cannot be converted to a typed value
199 */
200 T create( Object value ) throws ValueFormatException;
201
202 /**
203 * Get the string representation of the supplied value.
204 *
205 * @param value the value
206 * @return the string representation; never null
207 */
208 String asString( Object value );
209
210 /**
211 * Get the length of the supplied value.
212 *
213 * @param value the value
214 * @return the length; never negative
215 */
216 long length( Object value );
217
218 /**
219 * Get a readable and potentially shorter string representation of the supplied value.
220 *
221 * @param value the value
222 * @return the readable string representation; never null
223 */
224 String asReadableString( Object value );
225 }
226
227 }