31
31
import java .util .EnumMap ;
32
32
import java .util .Map ;
33
33
import java .util .Optional ;
34
+ import java .util .function .Function ;
34
35
import java .util .function .Predicate ;
35
36
36
37
import javax .lang .model .type .*;
@@ -182,8 +183,7 @@ public boolean isPartial() {
182
183
* @return the constant value attribute of this type
183
184
*/
184
185
public Object constValue () {
185
- return getMetadata (TypeMetadata .ConstantValue .class )
186
- .map (ConstantValue ::value ).orElse (null );
186
+ return getMetadata (TypeMetadata .ConstantValue .class , ConstantValue ::value , null );
187
187
}
188
188
189
189
/** Is this a constant type whose value is false?
@@ -361,11 +361,23 @@ public List<TypeMetadata> getMetadata() {
361
361
/**
362
362
* Get the type metadata of the given kind associated with this type (if any).
363
363
*/
364
- public <M extends TypeMetadata > Optional <M > getMetadata (Class <M > metadataClass ) {
365
- return metadata .stream ()
366
- .filter (m -> metadataClass .isAssignableFrom (m .getClass ()))
367
- .map (metadataClass ::cast )
368
- .findFirst ();
364
+ @ SuppressWarnings ("unchecked" )
365
+ public <M extends TypeMetadata > M getMetadata (Class <M > metadataClass ) {
366
+ return getMetadata (metadataClass , Function .identity (), null );
367
+ }
368
+
369
+ /**
370
+ * Get the type metadata of the given kind associated with this type (if any),
371
+ * and apply the provided mapping function.
372
+ */
373
+ @ SuppressWarnings ("unchecked" )
374
+ public <M extends TypeMetadata , Z > Z getMetadata (Class <M > metadataClass , Function <M , Z > metadataFunc , Z defaultValue ) {
375
+ for (TypeMetadata m : metadata ) {
376
+ if (m .getClass () == metadataClass ) {
377
+ return metadataFunc .apply ((M )m );
378
+ }
379
+ }
380
+ return defaultValue ;
369
381
}
370
382
371
383
/**
@@ -374,17 +386,20 @@ public <M extends TypeMetadata> Optional<M> getMetadata(Class<M> metadataClass)
374
386
* an exception is thrown.
375
387
*/
376
388
public Type addMetadata (TypeMetadata md ) {
377
- Assert .check (getMetadata (md .getClass ()). isEmpty () );
378
- return cloneWithMetadata (metadata .append (md ));
389
+ Assert .check (getMetadata (md .getClass ()) == null );
390
+ return cloneWithMetadata (metadata .prepend (md ));
379
391
}
380
392
381
393
/**
382
394
* Create a new copy of this type but without the specified type metadata.
383
395
*/
384
396
public Type dropMetadata (Class <? extends TypeMetadata > metadataClass ) {
385
- List <TypeMetadata > newMetadata = metadata .stream ()
386
- .filter (m -> !metadataClass .isAssignableFrom (m .getClass ()))
387
- .collect (List .collector ());
397
+ List <TypeMetadata > newMetadata = List .nil ();
398
+ for (TypeMetadata m : metadata ) {
399
+ if (m .getClass () != metadataClass ) {
400
+ newMetadata = newMetadata .prepend (m );
401
+ }
402
+ }
388
403
return cloneWithMetadata (newMetadata );
389
404
}
390
405
@@ -442,13 +457,12 @@ public Type annotatedType(final List<Attribute.TypeCompound> annos) {
442
457
}
443
458
444
459
public boolean isAnnotated () {
445
- return getMetadata (TypeMetadata .Annotations .class ). isPresent () ;
460
+ return getMetadata (TypeMetadata .Annotations .class ) != null ;
446
461
}
447
462
448
463
@ Override @ DefinedBy (Api .LANGUAGE_MODEL )
449
464
public List <Attribute .TypeCompound > getAnnotationMirrors () {
450
- return getMetadata (TypeMetadata .Annotations .class )
451
- .map (Annotations ::annotations ).orElse (List .nil ());
465
+ return getMetadata (TypeMetadata .Annotations .class , Annotations ::annotations , List .nil ());
452
466
}
453
467
454
468
0 commit comments