2424import java .util .Set ;
2525import java .util .function .Predicate ;
2626import java .util .stream .Collectors ;
27+ import java .util .stream .Stream ;
2728
2829import org .jspecify .annotations .Nullable ;
2930
@@ -103,8 +104,8 @@ public void afterPropertiesSet() {
103104 Assert .notNull (this .beanClassLoader , "BeanClassLoader not initialized" );
104105
105106 // Create the groups from the metadata
106- Set <ProxyHttpServiceGroup > groups = this .groupsMetadata .groups (this .beanClassLoader ).stream ()
107- .map (ProxyHttpServiceGroup ::new )
107+ Set <ConfigurableGroup > groups = this .groupsMetadata .groups (this .beanClassLoader ).stream ()
108+ .map (ConfigurableGroup ::new )
108109 .collect (Collectors .toSet ());
109110
110111 // Apply group configurers
@@ -115,7 +116,7 @@ public void afterPropertiesSet() {
115116
116117 // Create proxies
117118 Map <String , Map <Class <?>, Object >> proxies = groups .stream ()
118- .collect (Collectors .toMap (ProxyHttpServiceGroup ::name , ProxyHttpServiceGroup ::createProxies ));
119+ .collect (Collectors .toMap (ConfigurableGroup ::name , ConfigurableGroup ::createProxies ));
119120
120121 this .proxyRegistry = new DefaultHttpServiceProxyRegistry (proxies );
121122 }
@@ -158,20 +159,20 @@ private static void addGroupAdapter(
158159
159160
160161 /**
161- * {@link HttpServiceGroup} that creates client proxies .
162+ * Wraps the declared HttpServiceGroup, and helps to configure its client and proxy factory .
162163 */
163- private static final class ProxyHttpServiceGroup implements HttpServiceGroup {
164+ private static final class ConfigurableGroup {
164165
165- private final HttpServiceGroup declaredGroup ;
166+ private final HttpServiceGroup group ;
166167
167168 private final HttpServiceGroupAdapter <?> groupAdapter ;
168169
169170 private final Object clientBuilder ;
170171
171172 private final HttpServiceProxyFactory .Builder proxyFactoryBuilder = HttpServiceProxyFactory .builder ();
172173
173- ProxyHttpServiceGroup (HttpServiceGroup group ) {
174- this .declaredGroup = group ;
174+ ConfigurableGroup (HttpServiceGroup group ) {
175+ this .group = group ;
175176 this .groupAdapter = getGroupAdapter (group .clientType ());
176177 this .clientBuilder = this .groupAdapter .createClientBuilder ();
177178 }
@@ -182,30 +183,32 @@ private static HttpServiceGroupAdapter<?> getGroupAdapter(HttpServiceGroup.Clien
182183 return adapter ;
183184 }
184185
185- @ Override
186186 public String name () {
187- return this .declaredGroup .name ();
187+ return this .group .name ();
188188 }
189189
190- @ Override
191- public Set <Class <?>> httpServiceTypes () {
192- return this .declaredGroup .httpServiceTypes ();
190+ HttpServiceGroup httpServiceGroup () {
191+ return this .group ;
193192 }
194193
195- @ Override
196- public ClientType clientType () {
197- return this .declaredGroup .clientType ();
194+ @ SuppressWarnings ("unchecked" )
195+ public <CB > void applyClientCallback (HttpServiceGroupConfigurer .ClientCallback <CB > callback ) {
196+ callback .withClient (this .group , (CB ) this .clientBuilder );
197+ }
198+
199+ public void applyProxyFactoryCallback (HttpServiceGroupConfigurer .ProxyFactoryCallback callback ) {
200+ callback .withProxyFactory (this .group , this .proxyFactoryBuilder );
198201 }
199202
200203 @ SuppressWarnings ("unchecked" )
201- public <CB > void applyConfigurer (HttpServiceGroupConfigurer .GroupCallback <CB > callback ) {
202- callback .withGroup (this , (CB ) this .clientBuilder , this .proxyFactoryBuilder );
204+ public <CB > void applyGroupCallback (HttpServiceGroupConfigurer .GroupCallback <CB > callback ) {
205+ callback .withGroup (this . group , (CB ) this .clientBuilder , this .proxyFactoryBuilder );
203206 }
204207
205208 public Map <Class <?>, Object > createProxies () {
206- Map <Class <?>, Object > map = new LinkedHashMap <>(httpServiceTypes ().size ());
209+ Map <Class <?>, Object > map = new LinkedHashMap <>(this . group . httpServiceTypes ().size ());
207210 HttpServiceProxyFactory factory = this .proxyFactoryBuilder .exchangeAdapter (initExchangeAdapter ()).build ();
208- httpServiceTypes ().forEach (type -> map .put (type , factory .createClient (type )));
211+ this . group . httpServiceTypes ().forEach (type -> map .put (type , factory .createClient (type )));
209212 return map ;
210213 }
211214
@@ -227,16 +230,16 @@ public String toString() {
227230 */
228231 private static final class DefaultGroups <CB > implements HttpServiceGroupConfigurer .Groups <CB > {
229232
230- private final Set <ProxyHttpServiceGroup > groups ;
233+ private final Set <ConfigurableGroup > groups ;
231234
232- private final Predicate <HttpServiceGroup > defaultFilter ;
235+ private final Predicate <ConfigurableGroup > clientTypeFilter ;
233236
234- private Predicate <HttpServiceGroup > filter ;
237+ private Predicate <ConfigurableGroup > filter ;
235238
236- DefaultGroups (Set <ProxyHttpServiceGroup > groups , HttpServiceGroup .ClientType clientType ) {
239+ DefaultGroups (Set <ConfigurableGroup > groups , HttpServiceGroup .ClientType clientType ) {
237240 this .groups = groups ;
238- this .defaultFilter = (group -> group .clientType ().equals (clientType ));
239- this .filter = this .defaultFilter ;
241+ this .clientTypeFilter = (group -> group . httpServiceGroup () .clientType ().equals (clientType ));
242+ this .filter = this .clientTypeFilter ;
240243 }
241244
242245 @ Override
@@ -246,24 +249,29 @@ public HttpServiceGroupConfigurer.Groups<CB> filterByName(String... groupNames)
246249
247250 @ Override
248251 public HttpServiceGroupConfigurer .Groups <CB > filter (Predicate <HttpServiceGroup > predicate ) {
249- this .filter = this .filter .and (predicate );
252+ this .filter = this .filter .and (group -> predicate . test ( group . httpServiceGroup ()) );
250253 return this ;
251254 }
252255
253256 @ Override
254257 public void forEachClient (HttpServiceGroupConfigurer .ClientCallback <CB > callback ) {
255- forEachGroup ( (group , clientBuilder , factoryBuilder ) -> callback . withClient ( group , clientBuilder ));
258+ filterAndReset (). forEach (group -> group . applyClientCallback ( callback ));
256259 }
257260
258261 @ Override
259262 public void forEachProxyFactory (HttpServiceGroupConfigurer .ProxyFactoryCallback callback ) {
260- forEachGroup ( (group , clientBuilder , factoryBuilder ) -> callback . withProxyFactory ( group , factoryBuilder ));
263+ filterAndReset (). forEach (group -> group . applyProxyFactoryCallback ( callback ));
261264 }
262265
263266 @ Override
264267 public void forEachGroup (HttpServiceGroupConfigurer .GroupCallback <CB > callback ) {
265- this .groups .stream ().filter (this .filter ).forEach (group -> group .applyConfigurer (callback ));
266- this .filter = this .defaultFilter ; // reset the filter (terminal method)
268+ filterAndReset ().forEach (group -> group .applyGroupCallback (callback ));
269+ }
270+
271+ private Stream <ConfigurableGroup > filterAndReset () {
272+ Stream <ConfigurableGroup > stream = this .groups .stream ().filter (this .filter );
273+ this .filter = this .clientTypeFilter ;
274+ return stream ;
267275 }
268276 }
269277
0 commit comments