Skip to content

Commit 2bbbbbd

Browse files
committed
Refactor and optimize source code
1 parent 2e434b4 commit 2bbbbbd

33 files changed

+309
-429
lines changed

src/main/java/com/relogiclabs/jschema/exception/CommonException.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ public synchronized Throwable fillInStackTrace() {
5151
private void formatStackTrace() {
5252
StackTraceElement[] stackTrace = getStackTrace();
5353
int offset = 0;
54-
for(var e : stackTrace)
54+
for(var e : stackTrace) {
5555
if(e.getMethodName().startsWith(FAIL_METHOD_PREFIX)) offset++;
5656
else break;
57+
}
5758
setStackTrace(copyOfRange(stackTrace, offset, stackTrace.length));
5859
}
5960
}

src/main/java/com/relogiclabs/jschema/exception/InvalidDateTimeException.java

+4
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ public class InvalidDateTimeException extends CommonException {
44
public InvalidDateTimeException(String code, String message) {
55
super(code, message);
66
}
7+
8+
public InvalidDateTimeException(String code, String message, Throwable cause) {
9+
super(code, message, cause);
10+
}
711
}

src/main/java/com/relogiclabs/jschema/exception/JsonSchemaException.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.relogiclabs.jschema.tree.Context;
77
import lombok.Getter;
88

9-
import static com.relogiclabs.jschema.internal.util.MiscellaneousHelper.nonNullFrom;
9+
import static com.relogiclabs.jschema.internal.util.CommonHelper.nonNullFrom;
1010

1111
@Getter
1212
public class JsonSchemaException extends ScriptRuntimeException {
@@ -20,13 +20,14 @@ public JsonSchemaException(ErrorDetail error, ExpectedDetail expected, ActualDet
2020

2121
public JsonSchemaException(ErrorDetail error, ExpectedDetail expected,
2222
ActualDetail actual, Throwable cause) {
23-
super(error.getCode(), format(error, expected, actual), cause);
23+
super(error.getCode(), formatMessage(error, expected, actual), cause);
2424
this.error = error;
2525
this.expected = expected;
2626
this.actual = actual;
2727
}
2828

29-
private static String format(ErrorDetail error, ExpectedDetail expected, ActualDetail actual) {
29+
private static String formatMessage(ErrorDetail error, ExpectedDetail expected,
30+
ActualDetail actual) {
3031
Context context = nonNullFrom(expected.getContext(), actual.getContext());
3132
return context.getRuntime().getMessageFormatter().format(error, expected, actual);
3233
}

src/main/java/com/relogiclabs/jschema/function/CoreFunctions1.java

+45-50
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.relogiclabs.jschema.node.JObject;
1010
import com.relogiclabs.jschema.node.JString;
1111
import com.relogiclabs.jschema.node.JUndefined;
12-
import com.relogiclabs.jschema.tree.RuntimeContext;
1312

1413
import static com.relogiclabs.jschema.message.ErrorCode.ALEN01;
1514
import static com.relogiclabs.jschema.message.ErrorCode.ALEN02;
@@ -28,139 +27,135 @@
2827
import static com.relogiclabs.jschema.message.ErrorCode.SLEN05;
2928

3029
public abstract class CoreFunctions1 extends FunctionProvider {
31-
public CoreFunctions1(RuntimeContext runtime) {
32-
super(runtime);
33-
}
34-
3530
public boolean length(JString target, JInteger length) {
3631
var rLength = target.length();
3732
if(rLength != length.getValue()) return fail(new JsonSchemaException(
38-
new ErrorDetail(SLEN01, "Invalid length of string ", target),
39-
new ExpectedDetail(caller, "a string of length ", length),
40-
new ActualDetail(target, "found ", rLength, " which does not match")));
33+
new ErrorDetail(SLEN01, "Invalid length of string " + target),
34+
new ExpectedDetail(caller, "a string of length " + length),
35+
new ActualDetail(target, "found " + rLength + " which does not match")));
4136
return true;
4237
}
4338

4439
public boolean length(JArray target, JInteger length) {
4540
var rLength = target.getElements().size();
4641
if(rLength != length.getValue()) return fail(new JsonSchemaException(
47-
new ErrorDetail(ALEN01, "Invalid length of array ", target.getOutline()),
48-
new ExpectedDetail(caller, "an array of length ", length),
49-
new ActualDetail(target, "found ", rLength, " which does not match")));
42+
new ErrorDetail(ALEN01, "Invalid length of array " + target.getOutline()),
43+
new ExpectedDetail(caller, "an array of length " + length),
44+
new ActualDetail(target, "found " + rLength + " which does not match")));
5045
return true;
5146
}
5247

5348
public boolean length(JObject target, JInteger length) {
5449
var rLength = target.getProperties().size();
5550
if(rLength != length.getValue()) return fail(new JsonSchemaException(
56-
new ErrorDetail(OLEN01, "Invalid size or length of object ", target.getOutline()),
57-
new ExpectedDetail(caller, "an object of length ", length),
58-
new ActualDetail(target, "found ", rLength, " which does not match")));
51+
new ErrorDetail(OLEN01, "Invalid size or length of object " + target.getOutline()),
52+
new ExpectedDetail(caller, "an object of length " + length),
53+
new ActualDetail(target, "found " + rLength + " which does not match")));
5954
return true;
6055
}
6156

6257
public boolean length(JString target, JInteger minimum, JInteger maximum) {
6358
var length = target.length();
6459
if(length < minimum.getValue())
6560
return fail(new JsonSchemaException(new ErrorDetail(SLEN02,
66-
"String ", target.getOutline(), " length is outside of range"),
67-
new ExpectedDetail(caller, "length in range [", minimum, ", ", maximum, "]"),
68-
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
61+
"String " + target.getOutline() + " length is outside of range"),
62+
new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"),
63+
new ActualDetail(target, "found " + length + " that is less than " + minimum)));
6964
if(length > maximum.getValue())
7065
return fail(new JsonSchemaException(new ErrorDetail(SLEN03,
71-
"String ", target.getOutline(), " length is outside of range"),
72-
new ExpectedDetail(caller, "length in range [", minimum, ", ", maximum, "]"),
73-
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
66+
"String " + target.getOutline() + " length is outside of range"),
67+
new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"),
68+
new ActualDetail(target, "found " + length + " that is greater than " + maximum)));
7469
return true;
7570
}
7671

7772
public boolean length(JString target, JInteger minimum, JUndefined undefined) {
7873
var length = target.length();
7974
if(length < minimum.getValue())
8075
return fail(new JsonSchemaException(new ErrorDetail(SLEN04,
81-
"String ", target.getOutline(), " length is outside of range"),
82-
new ExpectedDetail(caller, "length in range [", minimum, ", ", undefined, "]"),
83-
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
76+
"String " + target.getOutline() + " length is outside of range"),
77+
new ExpectedDetail(caller, "length in range [" + minimum + ", " + undefined + "]"),
78+
new ActualDetail(target, "found " + length + " that is less than " + minimum)));
8479
return true;
8580
}
8681

8782
public boolean length(JString target, JUndefined undefined, JInteger maximum) {
8883
var length = target.length();
8984
if(length > maximum.getValue())
9085
return fail(new JsonSchemaException(new ErrorDetail(SLEN05,
91-
"String ", target.getOutline(), " length is outside of range"),
92-
new ExpectedDetail(caller, "length in range [", undefined, ", ", maximum, "]"),
93-
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
86+
"String " + target.getOutline() + " length is outside of range"),
87+
new ExpectedDetail(caller, "length in range [" + undefined + ", " + maximum + "]"),
88+
new ActualDetail(target, "found " + length + " that is greater than " + maximum)));
9489
return true;
9590
}
9691

9792
public boolean length(JArray target, JInteger minimum, JInteger maximum) {
9893
var length = target.getElements().size();
9994
if(length < minimum.getValue())
10095
return fail(new JsonSchemaException(new ErrorDetail(ALEN02,
101-
"Array ", target.getOutline(), " length is outside of range"),
102-
new ExpectedDetail(caller, "length in range [", minimum, ", ", maximum, "]"),
103-
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
96+
"Array " + target.getOutline() + " length is outside of range"),
97+
new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"),
98+
new ActualDetail(target, "found " + length + " that is less than " + minimum)));
10499
if(length > maximum.getValue())
105100
return fail(new JsonSchemaException(new ErrorDetail(ALEN03,
106-
"Array ", target.getOutline(), " length is outside of range"),
107-
new ExpectedDetail(caller, "length in range [", minimum, ", ", maximum, "]"),
108-
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
101+
"Array " + target.getOutline() + " length is outside of range"),
102+
new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"),
103+
new ActualDetail(target, "found " + length + " that is greater than " + maximum)));
109104
return true;
110105
}
111106

112107
public boolean length(JArray target, JInteger minimum, JUndefined undefined) {
113108
var length = target.getElements().size();
114109
if(length < minimum.getValue())
115110
return fail(new JsonSchemaException(new ErrorDetail(ALEN04,
116-
"Array ", target.getOutline(), " length is outside of range"),
117-
new ExpectedDetail(caller, "length in range [", minimum, ", ", undefined, "]"),
118-
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
111+
"Array " + target.getOutline() + " length is outside of range"),
112+
new ExpectedDetail(caller, "length in range [" + minimum + ", " + undefined + "]"),
113+
new ActualDetail(target, "found " + length + " that is less than " + minimum)));
119114
return true;
120115
}
121116

122117
public boolean length(JArray target, JUndefined undefined, JInteger maximum) {
123118
var length = target.getElements().size();
124119
if(length > maximum.getValue())
125120
return fail(new JsonSchemaException(new ErrorDetail(ALEN05,
126-
"Array ", target.getOutline(), " length is outside of range"),
127-
new ExpectedDetail(caller, "length in range [", undefined, ", ", maximum, "]"),
128-
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
121+
"Array " + target.getOutline() + " length is outside of range"),
122+
new ExpectedDetail(caller, "length in range [" + undefined + ", " + maximum + "]"),
123+
new ActualDetail(target, "found " + length + " that is greater than " + maximum)));
129124
return true;
130125
}
131126

132127
public boolean length(JObject target, JInteger minimum, JInteger maximum) {
133128
var length = target.getProperties().size();
134129
if(length < minimum.getValue())
135130
return fail(new JsonSchemaException(new ErrorDetail(OLEN02,
136-
"Object ", target.getOutline(), " size or length is outside of range"),
137-
new ExpectedDetail(caller, "length in range [", minimum, ", ", maximum, "]"),
138-
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
131+
"Object " + target.getOutline() + " size or length is outside of range"),
132+
new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"),
133+
new ActualDetail(target, "found " + length + " that is less than " + minimum)));
139134
if(length > maximum.getValue())
140135
return fail(new JsonSchemaException(new ErrorDetail(OLEN03,
141-
"Object ", target.getOutline(), " size or length is outside of range"),
142-
new ExpectedDetail(caller, "length in range [", minimum, ", ", maximum, "]"),
143-
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
136+
"Object " + target.getOutline() + " size or length is outside of range"),
137+
new ExpectedDetail(caller, "length in range [" + minimum + ", " + maximum + "]"),
138+
new ActualDetail(target, "found " + length + " that is greater than " + maximum)));
144139
return true;
145140
}
146141

147142
public boolean length(JObject target, JInteger minimum, JUndefined undefined) {
148143
var length = target.getProperties().size();
149144
if(length < minimum.getValue())
150145
return fail(new JsonSchemaException(new ErrorDetail(OLEN04,
151-
"Object ", target.getOutline(), " size or length is outside of range"),
152-
new ExpectedDetail(caller, "length in range [", minimum, ", ", undefined, "]"),
153-
new ActualDetail(target, "found ", length, " that is less than ", minimum)));
146+
"Object " + target.getOutline() + " size or length is outside of range"),
147+
new ExpectedDetail(caller, "length in range [" + minimum + ", " + undefined + "]"),
148+
new ActualDetail(target, "found " + length + " that is less than " + minimum)));
154149
return true;
155150
}
156151

157152
public boolean length(JObject target, JUndefined undefined, JInteger maximum) {
158153
var length = target.getProperties().size();
159154
if(length > maximum.getValue())
160155
return fail(new JsonSchemaException(new ErrorDetail(OLEN05,
161-
"Object ", target.getOutline(), " size or length is outside of range"),
162-
new ExpectedDetail(caller, "length in range [", undefined, ", ", maximum, "]"),
163-
new ActualDetail(target, "found ", length, " that is greater than ", maximum)));
156+
"Object " + target.getOutline() + " size or length is outside of range"),
157+
new ExpectedDetail(caller, "length in range [" + undefined + ", " + maximum + "]"),
158+
new ActualDetail(target, "found " + length + " that is greater than " + maximum)));
164159
return true;
165160
}
166161
}

0 commit comments

Comments
 (0)