@@ -22,14 +22,41 @@ public class Struct {
2222 private StructTemplate template ;
2323 private final Map <Field <?>, Object []> fieldValues ;
2424
25+ public static Struct newInstance (@ NotNull StructTemplate template , @ Nullable Event event ) {
26+ Class <? extends Struct > structClass = template .getCustomClass ();
27+ try {
28+ return structClass .getDeclaredConstructor (StructTemplate .class , Event .class ).newInstance (template , event );
29+ } catch (Exception e ) {
30+ throw new RuntimeException ("Failed to create new instance of struct class " + structClass .getName (), e );
31+ }
32+ }
33+
34+ public static Struct newInstance (Struct source ) {
35+ Class <? extends Struct > structClass = source .getTemplate ().getCustomClass ();
36+ try {
37+ return structClass .getDeclaredConstructor (Struct .class ).newInstance (source );
38+ } catch (Exception e ) {
39+ throw new RuntimeException ("Failed to create new instance of struct class " + structClass .getName (), e );
40+ }
41+ }
42+
43+ public static Struct newInstance (@ NotNull StructTemplate template , @ Nullable Event event , @ Nullable Map <String , Expression <?>> initialValues ) {
44+ Class <? extends Struct > structClass = template .getCustomClass ();
45+ try {
46+ return structClass .getDeclaredConstructor (StructTemplate .class , Event .class , Map .class ).newInstance (template , event , initialValues );
47+ } catch (Exception e ) {
48+ throw new RuntimeException ("Failed to create new instance of struct class " + structClass .getName (), e );
49+ }
50+ }
51+
2552 /**
2653 * Creates a new struct with the given template and event.
2754 *
2855 * @param template The template to create the struct from.
2956 * @param event The event to evaluate the default values in.
3057 * @see StructManager#createStruct(StructTemplate, Event)
3158 */
32- public Struct (@ NotNull StructTemplate template , @ Nullable Event event ) {
59+ protected Struct (@ NotNull StructTemplate template , @ Nullable Event event ) {
3360 this .template = template ;
3461 fieldValues = new HashMap <>();
3562 for (Field <?> field : template .getFields ()) {
@@ -44,7 +71,7 @@ public Struct(@NotNull StructTemplate template, @Nullable Event event) {
4471 * @param source The struct to copy from.
4572 * @see StructManager#createStruct(StructTemplate, Event)
4673 */
47- public Struct (Struct source ) {
74+ protected Struct (Struct source ) {
4875 this .template = source .template ;
4976 fieldValues = new HashMap <>();
5077 for (Map .Entry <Field <?>, Object []> entry : source .fieldValues .entrySet ()) {
@@ -64,7 +91,7 @@ public Struct(Struct source) {
6491 * @param initialValues The initial values to set in the struct. This is a map of field names to expressions.
6592 * @see StructManager#createStruct(StructTemplate, Event)
6693 */
67- Struct (@ NotNull StructTemplate template , @ Nullable Event event , @ Nullable Map <String , Expression <?>> initialValues ) {
94+ protected Struct (@ NotNull StructTemplate template , @ Nullable Event event , @ Nullable Map <String , Expression <?>> initialValues ) {
6895 this .template = template ;
6996 fieldValues = new HashMap <>();
7097 for (Field <?> field : template .getFields ()) {
0 commit comments