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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/*
Copyright (c) 2016 James Ahlborn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.healthmarketscience.jackcess.expr;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* Wrapper for a typed primitive value used within the expression evaluation
* engine. Note that the "Null" value is represented by an actual Value
* instance with the type of {@link Type#NULL}. Also note that all the
* conversion methods will throw an {@link EvalException} if the conversion is
* not supported for the current value.
*
* @author James Ahlborn
*/
public interface Value
{
/** the types supported within the expression evaluation engine */
public enum Type
{
NULL, STRING, DATE, TIME, DATE_TIME, LONG, DOUBLE, BIG_DEC;
public boolean isString() {
return (this == STRING);
}
public boolean isNumeric() {
return inRange(LONG, BIG_DEC);
}
public boolean isIntegral() {
return (this == LONG);
}
public boolean isTemporal() {
return inRange(DATE, DATE_TIME);
}
public Type getPreferredFPType() {
return((ordinal() <= DOUBLE.ordinal()) ? DOUBLE : BIG_DEC);
}
public Type getPreferredNumericType() {
if(isNumeric()) {
return this;
}
if(isTemporal()) {
return ((this == DATE) ? LONG : DOUBLE);
}
return null;
}
private boolean inRange(Type start, Type end) {
return ((start.ordinal() <= ordinal()) && (ordinal() <= end.ordinal()));
}
}
/**
* @return the type of this value
*/
public Type getType();
/**
* @return the raw primitive value
*/
public Object get();
/**
* @return {@code true} if this value represents a "Null" value,
* {@code false} otherwise.
*/
public boolean isNull();
/**
* @return this primitive value converted to a boolean
*/
public boolean getAsBoolean(LocaleContext ctx);
/**
* @return this primitive value converted to a String
*/
public String getAsString(LocaleContext ctx);
/**
* @return this primitive value converted to a LocalDateTime
*/
public LocalDateTime getAsLocalDateTime(LocaleContext ctx);
/**
* Since date/time values have different types, it may be more convenient to
* get the date/time primitive value with the appropriate type information.
*
* @return this value converted to a date/time value
*/
public Value getAsDateTimeValue(LocaleContext ctx);
/**
* @return this primitive value converted (rounded) to an int
*/
public Integer getAsLongInt(LocaleContext ctx);
/**
* @return this primitive value converted (rounded) to a double
*/
public Double getAsDouble(LocaleContext ctx);
/**
* @return this primitive value converted to a BigDecimal
*/
public BigDecimal getAsBigDecimal(LocaleContext ctx);
}
|