@@ -60,14 +60,14 @@ final class ComponentFlattenerImpl implements ComponentFlattener {
6060    .mapper (TextComponent .class , TextComponent ::content )
6161    .build ();
6262
63-   private  static  final  int  MAX_DEPTH  = 512 ;
64- 
6563  private  final  InheritanceAwareMap <Component , Handler > flatteners ;
6664  private  final  Function <Component , String > unknownHandler ;
65+   private  final  int  maximumDepth ;
6766
68-   ComponentFlattenerImpl (final  InheritanceAwareMap <Component , Handler > flatteners , final  @ Nullable  Function <Component , String > unknownHandler ) {
67+   ComponentFlattenerImpl (final  InheritanceAwareMap <Component , Handler > flatteners , final  @ Nullable  Function <Component , String > unknownHandler ,  final   int   maximumDepth ) {
6968    this .flatteners  = flatteners ;
7069    this .unknownHandler  = unknownHandler ;
70+     this .maximumDepth  = maximumDepth ;
7171  }
7272
7373  @ Override 
@@ -79,8 +79,8 @@ private void flatten0(final @NotNull Component input, final @NotNull FlattenerLi
7979    requireNonNull (input , "input" );
8080    requireNonNull (listener , "listener" );
8181    if  (input  == Component .empty ()) return ;
82-     if  (depth  > MAX_DEPTH ) {
83-       throw  new  IllegalStateException ("Exceeded maximum depth of "  + MAX_DEPTH  + " while attempting to flatten components!" );
82+     if  (depth  > this . maximumDepth ) {
83+       throw  new  IllegalStateException ("Exceeded maximum depth of "  + this . maximumDepth  + " while attempting to flatten components!" );
8484    }
8585
8686    final  @ Nullable  Handler  flattener  = this .flattener (input );
@@ -114,7 +114,7 @@ private void flatten0(final @NotNull Component input, final @NotNull FlattenerLi
114114
115115  @ Override 
116116  public  ComponentFlattener .@ NotNull  Builder  toBuilder () {
117-     return  new  BuilderImpl (this .flatteners , this .unknownHandler );
117+     return  new  BuilderImpl (this .flatteners , this .unknownHandler ,  this . maximumDepth );
118118  }
119119
120120  // A function that allows nesting other flatten operations 
@@ -124,21 +124,26 @@ interface Handler {
124124  }
125125
126126  static  final  class  BuilderImpl  implements  Builder  {
127+     private  static  final  int  DEFAULT_MAX_DEPTH  = 32 ;
128+ 
127129    private  final  InheritanceAwareMap .Builder <Component , Handler > flatteners ;
128130    private  @ Nullable  Function <Component , String > unknownHandler ;
131+     private  int  maximumDepth ;
129132
130133    BuilderImpl () {
131134      this .flatteners  = InheritanceAwareMap .<Component , Handler >builder ().strict (true );
135+       this .maximumDepth  = DEFAULT_MAX_DEPTH ;
132136    }
133137
134-     BuilderImpl (final  InheritanceAwareMap <Component , Handler > flatteners , final  @ Nullable  Function <Component , String > unknownHandler ) {
138+     BuilderImpl (final  InheritanceAwareMap <Component , Handler > flatteners , final  @ Nullable  Function <Component , String > unknownHandler ,  final   int   maximumDepth ) {
135139      this .flatteners  = InheritanceAwareMap .builder (flatteners ).strict (true );
136140      this .unknownHandler  = unknownHandler ;
141+       this .maximumDepth  = maximumDepth ;
137142    }
138143
139144    @ Override 
140145    public  @ NotNull  ComponentFlattener  build () {
141-       return  new  ComponentFlattenerImpl (this .flatteners .build (), this .unknownHandler );
146+       return  new  ComponentFlattenerImpl (this .flatteners .build (), this .unknownHandler ,  this . maximumDepth );
142147    }
143148
144149    @ Override 
@@ -160,5 +165,12 @@ static final class BuilderImpl implements Builder {
160165      this .unknownHandler  = converter ;
161166      return  this ;
162167    }
168+ 
169+     @ Override 
170+     public  @ NotNull  Builder  maximumDepth (int  maximumDepth ) {
171+       if  (maximumDepth  <= 0 ) throw  new  IllegalArgumentException ("maxDepth must be greater than 0, was "  + maximumDepth );
172+       this .maximumDepth  = maximumDepth ;
173+       return  this ;
174+     }
163175  }
164176}
0 commit comments