11package com .salesforce .multicloudj .iam .model ;
22
33import com .salesforce .multicloudj .common .exceptions .InvalidArgumentException ;
4- import lombok .Builder ;
5- import lombok .Getter ;
6- import lombok .Singular ;
7-
84import java .util .List ;
95import java .util .Map ;
106import java .util .Objects ;
7+ import lombok .Builder ;
8+ import lombok .Getter ;
9+ import lombok .Singular ;
1110
1211/**
1312 * Represents a single statement within a policy document.
3029 */
3130@ Getter
3231public class Statement {
33- private final String sid ;
34- private final String effect ;
35- private final List <String > principals ;
36- private final List <String > actions ;
37- private final List <String > resources ;
38- private final Map <String , Map <String , Object >> conditions ;
39-
40- @ Builder
41- private Statement (String sid , String effect ,
42- @ Singular List <String > principals ,
43- @ Singular List <String > actions ,
44- @ Singular List <String > resources ,
45- Map <String , Map <String , Object >> conditions ) {
46- // Validate effect
47- if (effect == null || effect .trim ().isEmpty ()) {
48- throw new InvalidArgumentException ("Effect is required and cannot be empty" );
49- }
32+ private final String sid ;
33+ private final String effect ;
34+ private final List <String > principals ;
35+ private final List <String > actions ;
36+ private final List <String > resources ;
37+ private final Map <String , Map <String , Object >> conditions ;
5038
51- // Filter out null/empty/whitespace values and validate actions
52- this .sid = sid ;
53- this .effect = effect ;
54- this .principals = filterValidStrings (principals );
55- this .actions = filterValidStrings (actions );
56- this .resources = filterValidStrings (resources );
39+ @ Builder
40+ private Statement (String sid , String effect ,
41+ @ Singular List <String > principals ,
42+ @ Singular List <String > actions ,
43+ @ Singular List <String > resources ,
44+ Map <String , Map <String , Object >> conditions ) {
45+ // Validate effect
46+ if (effect == null || effect .trim ().isEmpty ()) {
47+ throw new InvalidArgumentException ("Effect is required and cannot be empty" );
48+ }
5749
58- // Validate that at least one action exists after filtering
59- if (this .actions .isEmpty ()) {
60- throw new InvalidArgumentException ("At least one action is required" );
61- }
50+ // Filter out null/empty/whitespace values and validate actions
51+ this .sid = sid ;
52+ this .effect = effect ;
53+ this .principals = filterValidStrings (principals );
54+ this .actions = filterValidStrings (actions );
55+ this .resources = filterValidStrings (resources );
6256
63- this .conditions = conditions != null ? conditions : new java .util .HashMap <>();
57+ // Validate that at least one action exists after filtering
58+ if (this .actions .isEmpty ()) {
59+ throw new InvalidArgumentException ("At least one action is required" );
6460 }
6561
66- private static List <String > filterValidStrings (List <String > input ) {
67- if (input == null ) {
68- return new java .util .ArrayList <>();
69- }
70- return input .stream ()
71- .filter (s -> s != null && !s .trim ().isEmpty ())
72- .collect (java .util .stream .Collectors .toList ());
62+ this .conditions = conditions != null ? conditions : new java .util .HashMap <>();
63+ }
64+
65+ private static List <String > filterValidStrings (List <String > input ) {
66+ if (input == null ) {
67+ return new java .util .ArrayList <>();
7368 }
69+ return input .stream ()
70+ .filter (s -> s != null && !s .trim ().isEmpty ())
71+ .collect (java .util .stream .Collectors .toList ());
72+ }
7473
75- public static class StatementBuilder {
76- public StatementBuilder condition (String operator , String key , Object value ) {
77- if (operator != null && key != null && value != null ) {
78- if (this .conditions == null ) {
79- this .conditions = new java .util .HashMap <>();
80- }
81- this .conditions .computeIfAbsent (operator , k -> new java .util .HashMap <>()).put (key , value );
82- }
83- return this ;
74+ /**
75+ * Custom builder for Statement to handle conditions.
76+ */
77+ public static class StatementBuilder {
78+ /**
79+ * Adds a condition to the statement.
80+ *
81+ * @param operator the condition operator
82+ * @param key the condition key
83+ * @param value the condition value
84+ * @return this builder
85+ */
86+ public StatementBuilder condition (String operator , String key , Object value ) {
87+ if (operator != null && key != null && value != null ) {
88+ if (this .conditions == null ) {
89+ this .conditions = new java .util .HashMap <>();
8490 }
91+ this .conditions .computeIfAbsent (operator , k -> new java .util .HashMap <>())
92+ .put (key , value );
93+ }
94+ return this ;
8595 }
96+ }
8697
8798
8899
89- @ Override
90- public boolean equals (Object o ) {
91- if (this == o ) return true ;
92- if (o == null || getClass () != o .getClass ()) return false ;
93- Statement statement = (Statement ) o ;
94- return Objects .equals (sid , statement .sid ) &&
95- Objects .equals (effect , statement .effect ) &&
96- Objects .equals (principals , statement .principals ) &&
97- Objects .equals (actions , statement .actions ) &&
98- Objects .equals (resources , statement .resources ) &&
99- Objects .equals (conditions , statement .conditions );
100+ @ Override
101+ public boolean equals (Object o ) {
102+ if (this == o ) {
103+ return true ;
100104 }
101-
102- @ Override
103- public int hashCode () {
104- return Objects .hash (sid , effect , principals , actions , resources , conditions );
105+ if (o == null || getClass () != o .getClass ()) {
106+ return false ;
105107 }
108+ Statement statement = (Statement ) o ;
109+ return Objects .equals (sid , statement .sid )
110+ && Objects .equals (effect , statement .effect )
111+ && Objects .equals (principals , statement .principals )
112+ && Objects .equals (actions , statement .actions )
113+ && Objects .equals (resources , statement .resources )
114+ && Objects .equals (conditions , statement .conditions );
115+ }
106116
107- @ Override
108- public String toString () {
109- return "Statement{" +
110- "sid='" + sid + '\'' +
111- ", effect='" + effect + '\'' +
112- ", principals=" + principals +
113- ", actions=" + actions +
114- ", resources=" + resources +
115- ", conditions=" + conditions +
116- '}' ;
117- }
117+ @ Override
118+ public int hashCode () {
119+ return Objects .hash (sid , effect , principals , actions , resources , conditions );
120+ }
121+
122+ @ Override
123+ public String toString () {
124+ return "Statement{"
125+ + "sid='" + sid + '\''
126+ + ", effect='" + effect + '\''
127+ + ", principals=" + principals
128+ + ", actions=" + actions
129+ + ", resources=" + resources
130+ + ", conditions=" + conditions
131+ + '}' ;
132+ }
118133}
0 commit comments