View Javadoc
1   /*
2   Copyright (c) 2016 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.expr;
18  
19  import java.math.BigDecimal;
20  import java.time.LocalDateTime;
21  
22  /**
23   * Wrapper for a typed primitive value used within the expression evaluation
24   * engine.  Note that the "Null" value is represented by an actual Value
25   * instance with the type of {@link Type#NULL}.  Also note that all the
26   * conversion methods will throw an {@link EvalException} if the conversion is
27   * not supported for the current value.
28   *
29   * @author James Ahlborn
30   */
31  public interface Value
32  {
33    /** the types supported within the expression evaluation engine */
34    public enum Type
35    {
36      NULL, STRING, DATE, TIME, DATE_TIME, LONG, DOUBLE, BIG_DEC;
37  
38      public boolean isString() {
39        return (this == STRING);
40      }
41  
42      public boolean isNumeric() {
43        return inRange(LONG, BIG_DEC);
44      }
45  
46      public boolean isIntegral() {
47        return (this == LONG);
48      }
49  
50      public boolean isTemporal() {
51        return inRange(DATE, DATE_TIME);
52      }
53  
54      public Type getPreferredFPType() {
55        return((ordinal() <= DOUBLE.ordinal()) ? DOUBLE : BIG_DEC);
56      }
57  
58      public Type getPreferredNumericType() {
59        if(isNumeric()) {
60          return this;
61        }
62        if(isTemporal()) {
63          return ((this == DATE) ? LONG : DOUBLE);
64        }
65        return null;
66      }
67  
68      private boolean inRange(Type start, Type end) {
69        return ((start.ordinal() <= ordinal()) && (ordinal() <= end.ordinal()));
70      }
71    }
72  
73    /**
74     * @return the type of this value
75     */
76    public Type getType();
77  
78    /**
79     * @return the raw primitive value
80     */
81    public Object get();
82  
83    /**
84     * @return {@code true} if this value represents a "Null" value,
85     *         {@code false} otherwise.
86     */
87    public boolean isNull();
88  
89    /**
90     * @return this primitive value converted to a boolean
91     */
92    public boolean getAsBoolean(LocaleContext ctx);
93  
94    /**
95     * @return this primitive value converted to a String
96     */
97    public String getAsString(LocaleContext ctx);
98  
99    /**
100    * @return this primitive value converted to a LocalDateTime
101    */
102   public LocalDateTime getAsLocalDateTime(LocaleContext ctx);
103 
104   /**
105    * Since date/time values have different types, it may be more convenient to
106    * get the date/time primitive value with the appropriate type information.
107    *
108    * @return this value converted to a date/time value
109    */
110   public Value getAsDateTimeValue(LocaleContext ctx);
111 
112   /**
113    * @return this primitive value converted (rounded) to an int
114    */
115   public Integer getAsLongInt(LocaleContext ctx);
116 
117   /**
118    * @return this primitive value converted (rounded) to a double
119    */
120   public Double getAsDouble(LocaleContext ctx);
121 
122   /**
123    * @return this primitive value converted to a BigDecimal
124    */
125   public BigDecimal getAsBigDecimal(LocaleContext ctx);
126 }