1
1
package com .relogiclabs .json .schema .function ;
2
2
3
3
import com .relogiclabs .json .schema .exception .JsonSchemaException ;
4
+ import com .relogiclabs .json .schema .internal .function .DateTimeAgent ;
4
5
import com .relogiclabs .json .schema .internal .time .DateTimeParser ;
5
- import com .relogiclabs .json .schema .internal .time .DateTimeType ;
6
6
import com .relogiclabs .json .schema .message .ActualDetail ;
7
7
import com .relogiclabs .json .schema .message .ErrorDetail ;
8
8
import com .relogiclabs .json .schema .message .ExpectedDetail ;
9
+ import com .relogiclabs .json .schema .time .DateTimeType ;
9
10
import com .relogiclabs .json .schema .tree .RuntimeContext ;
10
- import com .relogiclabs .json .schema .types .JDateTime ;
11
- import com .relogiclabs .json .schema .types .JString ;
12
- import com .relogiclabs .json .schema .types .JUndefined ;
11
+ import com .relogiclabs .json .schema .type .JDateTime ;
12
+ import com .relogiclabs .json .schema .type .JString ;
13
+ import com .relogiclabs .json .schema .type .JUndefined ;
13
14
14
- import static com .relogiclabs .json .schema .internal .time .DateTimeType .DATE_TYPE ;
15
15
import static com .relogiclabs .json .schema .message .ErrorCode .AFTR01 ;
16
16
import static com .relogiclabs .json .schema .message .ErrorCode .AFTR02 ;
17
17
import static com .relogiclabs .json .schema .message .ErrorCode .BFOR01 ;
28
28
import static com .relogiclabs .json .schema .message .ErrorCode .ENDE02 ;
29
29
import static com .relogiclabs .json .schema .message .ErrorCode .STRT01 ;
30
30
import static com .relogiclabs .json .schema .message .ErrorCode .STRT02 ;
31
+ import static com .relogiclabs .json .schema .time .DateTimeType .DATE_TYPE ;
31
32
32
33
public class CoreFunctions4 extends CoreFunctions3 {
33
34
public CoreFunctions4 (RuntimeContext runtime ) {
@@ -77,48 +78,47 @@ public boolean range(JDateTime target, JString start, JString end) {
77
78
if (rStart == null ) return false ;
78
79
var rEnd = getDateTime (target .getDateTimeParser (), end );
79
80
if (rEnd == null ) return false ;
80
- boolean result = true ;
81
- result &= isValidStart (target , rStart , DRNG01 , DRNG02 );
82
- result &= isValidEnd (target , rEnd , DRNG03 , DRNG04 );
83
- return result ;
81
+ if (target .getDateTime ().compare (rStart .getDateTime ()) < 0 )
82
+ return failOnStartDate (target , rStart , getErrorCode (target , DRNG01 , DRNG02 ));
83
+ if (target .getDateTime ().compare (rEnd .getDateTime ()) > 0 )
84
+ return failOnEndDate (target , rEnd , getErrorCode (target , DRNG03 , DRNG04 ));
85
+ return true ;
86
+ }
87
+
88
+ private static String getErrorCode (JDateTime target , String date , String time ) {
89
+ return target .getDateTime ().getType () == DATE_TYPE ? date : time ;
90
+ }
91
+
92
+ private boolean failOnStartDate (JDateTime target , JDateTime start , String code ) {
93
+ var type = target .getDateTime ().getType ();
94
+ return failWith (new JsonSchemaException (
95
+ new ErrorDetail (code , type , " is earlier than start " , type ),
96
+ new ExpectedDetail (start , "a " , type , " from or after " , start ),
97
+ new ActualDetail (target , "found " , target , " which is before start " , type )
98
+ ));
99
+ }
100
+
101
+ private boolean failOnEndDate (JDateTime target , JDateTime end , String code ) {
102
+ var type = target .getDateTime ().getType ();
103
+ return failWith (new JsonSchemaException (
104
+ new ErrorDetail (code , type , " is later than end " , type ),
105
+ new ExpectedDetail (end , "a " , type , " until or before " , end ),
106
+ new ActualDetail (target , "found " , target , " which is after end " , type )
107
+ ));
84
108
}
85
109
86
110
public boolean range (JDateTime target , JUndefined start , JString end ) {
87
111
var rEnd = getDateTime (target .getDateTimeParser (), end );
88
112
if (rEnd == null ) return false ;
89
- return isValidEnd (target , rEnd , DRNG05 , DRNG06 );
113
+ if (target .getDateTime ().compare (rEnd .getDateTime ()) <= 0 ) return true ;
114
+ return failOnEndDate (target , rEnd , getErrorCode (target , DRNG05 , DRNG06 ));
90
115
}
91
116
92
117
public boolean range (JDateTime target , JString start , JUndefined end ) {
93
118
var rStart = getDateTime (target .getDateTimeParser (), start );
94
119
if (rStart == null ) return false ;
95
- return isValidStart (target , rStart , DRNG07 , DRNG08 );
96
- }
97
-
98
- private boolean isValidStart (JDateTime target , JDateTime start , String codeDate , String codeTime ) {
99
- if (target .getDateTime ().compare (start .getDateTime ()) < 0 ) {
100
- var type = target .getDateTime ().getType ();
101
- var code = type == DATE_TYPE ? codeDate : codeTime ;
102
- return failWith (new JsonSchemaException (
103
- new ErrorDetail (code , type , " is earlier than start " , type ),
104
- new ExpectedDetail (start , "a " , type , " from or after " , start ),
105
- new ActualDetail (target , "found " , target , " which is before start " , type )
106
- ));
107
- }
108
- return true ;
109
- }
110
-
111
- private boolean isValidEnd (JDateTime target , JDateTime end , String codeDate , String codeTime ) {
112
- if (target .getDateTime ().compare (end .getDateTime ()) > 0 ) {
113
- var type = target .getDateTime ().getType ();
114
- var code = type == DATE_TYPE ? codeDate : codeTime ;
115
- return failWith (new JsonSchemaException (
116
- new ErrorDetail (code , type , " is later than end " , type ),
117
- new ExpectedDetail (end , "a " , type , " until or before " , end ),
118
- new ActualDetail (target , "found " , target , " which is after end " , type )
119
- ));
120
- }
121
- return true ;
120
+ if (target .getDateTime ().compare (rStart .getDateTime ()) >= 0 ) return true ;
121
+ return failOnStartDate (target , rStart , getErrorCode (target , DRNG07 , DRNG08 ));
122
122
}
123
123
124
124
public boolean start (JDateTime target , JString reference ) {
@@ -156,7 +156,7 @@ private JDateTime getDateTime(DateTimeParser parser, JString dateTime) {
156
156
&& result .getDateTime ().getType () == parser .getType ()) return result ;
157
157
var jDateTime = new DateTimeAgent (parser ).parse (function , dateTime );
158
158
if (jDateTime == null ) return null ;
159
- dateTime .setDerived (jDateTime .create (dateTime ));
159
+ dateTime .setDerived (jDateTime .createNode (dateTime ));
160
160
return (JDateTime ) dateTime .getDerived ();
161
161
}
162
162
}
0 commit comments