11package com .relogiclabs .json .schema .tree ;
22
33import com .relogiclabs .json .schema .exception .DuplicateDefinitionException ;
4- import com .relogiclabs .json .schema .internal .tree .FunctionManager ;
5- import com .relogiclabs .json .schema .internal .tree .PragmaManager ;
4+ import com .relogiclabs .json .schema .function .FutureValidator ;
5+ import com .relogiclabs .json .schema .internal .tree .ExceptionRegistry ;
6+ import com .relogiclabs .json .schema .internal .tree .FunctionRegistry ;
7+ import com .relogiclabs .json .schema .internal .tree .PragmaRegistry ;
68import com .relogiclabs .json .schema .message .MessageFormatter ;
79import com .relogiclabs .json .schema .types .JAlias ;
810import com .relogiclabs .json .schema .types .JDefinition ;
911import com .relogiclabs .json .schema .types .JFunction ;
1012import com .relogiclabs .json .schema .types .JInclude ;
1113import com .relogiclabs .json .schema .types .JNode ;
1214import com .relogiclabs .json .schema .types .JPragma ;
15+ import com .relogiclabs .json .schema .types .JReceiver ;
1316import com .relogiclabs .json .schema .types .JValidator ;
1417import lombok .Getter ;
1518
19+ import java .util .ArrayList ;
1620import java .util .HashMap ;
17- import java .util .LinkedList ;
21+ import java .util .List ;
1822import java .util .Map ;
19- import java .util .Queue ;
23+ import java .util .UUID ;
2024import java .util .function .Supplier ;
2125
2226import static com .relogiclabs .json .schema .internal .util .StringHelper .concat ;
2529
2630
2731public final class RuntimeContext {
28- private final FunctionManager functionManager ;
29- private final PragmaManager pragmaManager ;
30- private int disableException = 0 ;
31-
32+ @ Getter private final FunctionRegistry functions ;
33+ @ Getter private final PragmaRegistry pragmas ;
3234 @ Getter private final Map <JAlias , JValidator > definitions ;
33- @ Getter private final boolean throwException ;
34- @ Getter private final Queue <Exception > exceptions ;
35+ @ Getter private final ExceptionRegistry exceptions ;
36+ @ Getter private final Map <String , FutureValidator > validators ;
37+ @ Getter private final Map <JReceiver , List <JNode >> receivers ;
38+ @ Getter private final Map <String , Object > storage ;
3539 @ Getter private final MessageFormatter messageFormatter ;
3640
37-
3841 public RuntimeContext (MessageFormatter messageFormatter , boolean throwException ) {
3942 this .messageFormatter = messageFormatter ;
40- this .throwException = throwException ;
4143 this .definitions = new HashMap <>();
42- this .functionManager = new FunctionManager (this );
43- this .pragmaManager = new PragmaManager ();
44- this .exceptions = new LinkedList <>();
44+ this .functions = new FunctionRegistry (this );
45+ this .pragmas = new PragmaRegistry ();
46+ this .exceptions = new ExceptionRegistry (throwException );
47+ this .receivers = new HashMap <>();
48+ this .storage = new HashMap <>();
49+ this .validators = new HashMap <>();
4550 }
4651
4752 public JPragma addPragma (JPragma pragma ) {
48- return pragmaManager .addPragma (pragma );
53+ return pragmas .addPragma (pragma );
4954 }
5055
5156 public JInclude addClass (JInclude include ) {
@@ -54,7 +59,7 @@ public JInclude addClass(JInclude include) {
5459 }
5560
5661 public void addClass (String className , Context context ) {
57- functionManager .addClass (className , context );
62+ functions .addClass (className , context );
5863 }
5964
6065 public JDefinition addDefinition (JDefinition definition ) {
@@ -68,38 +73,49 @@ DEFI01, concat("Duplicate definition of ", quote(definition.getAlias()),
6873 return definition ;
6974 }
7075
71- public boolean getIgnoreUndefinedProperties ( ) {
72- return pragmaManager . getIgnoreUndefinedProperties ( );
76+ public boolean invokeFunction ( JFunction function , JNode target ) {
77+ return functions . invokeFunction ( function , target );
7378 }
7479
75- public double getFloatingPointTolerance ( ) {
76- return pragmaManager .getFloatingPointTolerance ();
80+ public boolean areEqual ( double value1 , double value2 ) {
81+ return Math . abs ( value1 - value2 ) < pragmas .getFloatingPointTolerance ();
7782 }
7883
79- public boolean getIgnoreObjectPropertyOrder ( ) {
80- return pragmaManager . getIgnoreObjectPropertyOrder ( );
84+ public < T > T tryExecute ( Supplier < T > function ) {
85+ return exceptions . tryExecute ( function );
8186 }
8287
83- public boolean invokeFunction ( JFunction function , JNode target ) {
84- return functionManager . invokeFunction ( function , target );
88+ public void register ( List < JReceiver > list ) {
89+ for ( var r : list ) receivers . put ( r , new ArrayList <>() );
8590 }
8691
87- public boolean areEqual ( double value1 , double value2 ) {
88- return Math . abs ( value1 - value2 ) < getFloatingPointTolerance ( );
92+ public void receive ( List < JReceiver > list , JNode node ) {
93+ for ( var r : list ) receivers . get ( r ). add ( node );
8994 }
9095
91- public <T > T tryMatch (Supplier <T > function ) {
92- try {
93- disableException += 1 ;
94- return function .get ();
95- } finally {
96- disableException -= 1 ;
97- }
96+ public List <JNode > fetch (JReceiver receiver ) {
97+ return receivers .get (receiver );
98+ }
99+
100+ public boolean addValidator (FutureValidator validator ) {
101+ return validators .put (UUID .randomUUID ().toString (), validator ) == null ;
102+ }
103+
104+ public boolean invokeValidators () {
105+ var result = true ;
106+ for (var v : validators .values ()) result &= v .validate ();
107+ return result ;
98108 }
99109
100110 public boolean failWith (RuntimeException exception ) {
101- if ( throwException && disableException == 0 ) throw exception ;
102- if ( disableException == 0 ) exceptions .add (exception );
111+ exceptions . tryThrow ( exception ) ;
112+ exceptions .tryAdd (exception );
103113 return false ;
104114 }
115+
116+ public void clear () {
117+ exceptions .clear ();
118+ storage .clear ();
119+ for (var v : receivers .values ()) v .clear ();
120+ }
105121}
0 commit comments