blob: bb0498e0512a67e3da0d6ab1479461af4ca3f9da (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.types;
import com.yahoo.search.query.profile.QueryProfile;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.search.query.profile.compiled.CompiledQueryProfileRegistry;
import com.yahoo.search.yql.YqlQuery;
import com.yahoo.tensor.Tensor;
import java.util.Optional;
/**
* Superclass of query type field types.
* Field types are immutable.
*
* @author bratseth
*/
@SuppressWarnings("rawtypes")
public abstract class FieldType {
public static final PrimitiveFieldType stringType = new PrimitiveFieldType(String.class);
public static final PrimitiveFieldType integerType = new PrimitiveFieldType(Integer.class);
public static final PrimitiveFieldType longType = new PrimitiveFieldType(Long.class);
public static final PrimitiveFieldType floatType = new PrimitiveFieldType(Float.class);
public static final PrimitiveFieldType doubleType = new PrimitiveFieldType(Double.class);
public static final PrimitiveFieldType booleanType = new PrimitiveFieldType(Boolean.class);
public static final TensorFieldType genericTensorType = new TensorFieldType(Optional.empty());
public static final QueryFieldType queryType = new QueryFieldType();
public static final QueryProfileFieldType genericQueryProfileType = new QueryProfileFieldType();
/** Returns the class of instance values of this field type */
public abstract Class getValueClass();
/** Returns a string representation of this type which can be converted back to a type class by {@link #fromString} */
public abstract String stringValue();
public abstract String toString();
/** Returns a string describing possible instances of this type, suitable for user error messages */
public abstract String toInstanceDescription();
/** Converts the given type to an instance of this type, if possible. Returns null if not possible. */
public abstract Object convertFrom(Object o, QueryProfileRegistry registry);
/** Converts the given type to an instance of this type, if possible. Returns null if not possible. */
public abstract Object convertFrom(Object o, CompiledQueryProfileRegistry registry);
/**
* Returns the field type for a given string name.
*
* @param typeString a type string - a primitive name, "query-profile" or "query-profile:profile-name"
* @param registry the registry in which query profile references are resolved when the last form above is used,
* or null in which case that form cannot be used
* @throws IllegalArgumentException if the string does not resolve to a type
*/
public static FieldType fromString(String typeString, QueryProfileTypeRegistry registry) {
if ("string".equals(typeString))
return stringType;
if ("integer".equals(typeString))
return integerType;
if ("long".equals(typeString))
return longType;
if ("float".equals(typeString))
return floatType;
if ("double".equals(typeString))
return doubleType;
if ("boolean".equals(typeString))
return booleanType;
if ("query".equals(typeString))
return queryType;
if (typeString.startsWith("tensor"))
return TensorFieldType.fromTypeString(typeString);
if ("query-profile".equals(typeString))
return genericQueryProfileType;
if (typeString.startsWith("query-profile:"))
return QueryProfileFieldType.fromString(typeString.substring("query-profile:".length()),registry);
throw new IllegalArgumentException("Unknown type '" + typeString + "'");
}
/** Returns the field type from a value class, or null if there is no type for it */
public static FieldType fromClass(Class clazz) {
if (clazz == String.class) return stringType;
if (clazz == Integer.class) return integerType;
if (clazz == Long.class) return longType;
if (clazz == Float.class) return floatType;
if (clazz == Double.class) return doubleType;
if (clazz == Boolean.class) return booleanType;
if (clazz == Tensor.class) return genericTensorType;
if (clazz == YqlQuery.class) return queryType;
if (clazz == QueryProfile.class) return genericQueryProfileType;
return null;
}
}
|