1515import java .util .Optional ;
1616import java .util .concurrent .CompletableFuture ;
1717import java .util .concurrent .CompletionStage ;
18- import java .util .function .Function ;
1918
2019public class MetadataCachingProviderDecorator implements CloudProvider {
2120
@@ -35,122 +34,89 @@ public MetadataCachingProviderDecorator(CloudProvider delegate, Duration cacheEn
3534 public CompletionStage <CloudItemMetadata > itemMetadata (CloudPath node ) {
3635 var cachedMetadata = metadataCache .getIfPresent (node );
3736 if (cachedMetadata != null ) {
38- return cachedMetadata
39- .map (CompletableFuture ::completedFuture )
37+ return cachedMetadata //
38+ .map (CompletableFuture ::completedFuture ) //
4039 .orElseGet (() -> CompletableFuture .failedFuture (new NotFoundException ()));
4140 } else {
42- return delegate .itemMetadata (node )
43- .handle ((metadata , exception ) -> {
41+ return delegate .itemMetadata (node ) //
42+ .whenComplete ((metadata , exception ) -> {
4443 if (exception == null ) {
4544 assert metadata != null ;
4645 metadataCache .put (node , Optional .of (metadata ));
47- return CompletableFuture .completedFuture (metadata );
4846 } else if (exception instanceof NotFoundException ) {
4947 metadataCache .put (node , Optional .empty ());
50- return CompletableFuture .<CloudItemMetadata >failedFuture (exception );
5148 } else {
5249 metadataCache .invalidate (node );
53- return CompletableFuture .<CloudItemMetadata >failedFuture (exception );
5450 }
55- }). thenCompose ( Function . identity ()) ;
51+ });
5652 }
5753 }
5854
5955 @ Override
6056 public CompletionStage <CloudItemList > list (CloudPath folder , Optional <String > pageToken ) {
61- return delegate .list (folder , pageToken )
62- .handle ((cloudItemList , exception ) -> {
57+ return delegate .list (folder , pageToken ) //
58+ .whenComplete ((cloudItemList , exception ) -> {
6359 evictIncludingDescendants (folder );
6460 if (exception == null ) {
6561 assert cloudItemList != null ;
6662 cloudItemList .getItems ().forEach (metadata -> metadataCache .put (metadata .getPath (), Optional .of (metadata )));
67- return CompletableFuture .completedFuture (cloudItemList );
68- } else {
69- return CompletableFuture .<CloudItemList >failedFuture (exception );
7063 }
71- }). thenCompose ( Function . identity ()) ;
64+ });
7265 }
7366
7467 @ Override
7568 public CompletionStage <InputStream > read (CloudPath file , ProgressListener progressListener ) {
76- return delegate .read (file , progressListener )
77- .handle ((metadata , exception ) -> {
78- if (exception == null ) {
79- assert metadata != null ;
80- return CompletableFuture .completedFuture (metadata );
81- } else {
69+ return delegate .read (file , progressListener ) //
70+ .whenComplete ((metadata , exception ) -> {
71+ if (exception != null ) {
8272 metadataCache .invalidate (file );
83- return CompletableFuture .<InputStream >failedFuture (exception );
8473 }
85- }). thenCompose ( Function . identity ()) ;
74+ });
8675 }
8776
8877 @ Override
8978 public CompletionStage <InputStream > read (CloudPath file , long offset , long count , ProgressListener progressListener ) {
90- return delegate .read (file , offset , count , progressListener )
91- .handle ((inputStream , exception ) -> {
92- if (exception == null ) {
93- assert inputStream != null ;
94- return CompletableFuture .completedFuture (inputStream );
95- } else {
79+ return delegate .read (file , offset , count , progressListener ) //
80+ .whenComplete ((inputStream , exception ) -> {
81+ if (exception != null ) {
9682 metadataCache .invalidate (file );
97- return CompletableFuture .<InputStream >failedFuture (exception );
9883 }
99- }). thenCompose ( Function . identity ()) ;
84+ });
10085 }
10186
10287 @ Override
10388 public CompletionStage <Void > write (CloudPath file , boolean replace , InputStream data , long size , Optional <Instant > lastModified , ProgressListener progressListener ) {
104- return delegate .write (file , replace , data , size , lastModified , progressListener )
105- .handle ((nullReturn , exception ) -> {
106- if (exception == null ) {
107- return CompletableFuture .completedFuture (nullReturn );
108- } else {
89+ return delegate .write (file , replace , data , size , lastModified , progressListener ) //
90+ .whenComplete ((nullReturn , exception ) -> {
91+ if (exception != null ) {
10992 metadataCache .invalidate (file );
110- return CompletableFuture .<Void >failedFuture (exception );
11193 }
112- }). thenCompose ( Function . identity ()) ;
94+ });
11395 }
11496
11597 @ Override
11698 public CompletionStage <CloudPath > createFolder (CloudPath folder ) {
117- return delegate .createFolder (folder )
118- .handle ((metadata , exception ) -> {
99+ return delegate .createFolder (folder ) //
100+ .whenComplete ((metadata , exception ) -> {
119101 metadataCache .invalidate (folder );
120- if (exception == null ) {
121- assert metadata != null ;
122- return CompletableFuture .completedFuture (metadata );
123- } else {
124- return CompletableFuture .<CloudPath >failedFuture (exception );
125- }
126- }).thenCompose (Function .identity ());
102+ });
127103 }
128104
129105 @ Override
130106 public CompletionStage <Void > delete (CloudPath node ) {
131- return delegate .delete (node )
132- .handle ((nullReturn , exception ) -> {
107+ return delegate .delete (node ) //
108+ .whenComplete ((nullReturn , exception ) -> {
133109 evictIncludingDescendants (node );
134- if (exception == null ) {
135- return CompletableFuture .completedFuture (nullReturn );
136- } else {
137- return CompletableFuture .<Void >failedFuture (exception );
138- }
139- }).thenCompose (Function .identity ());
110+ });
140111 }
141112
142113 @ Override
143114 public CompletionStage <CloudPath > move (CloudPath source , CloudPath target , boolean replace ) {
144- return delegate .move (source , target , replace )
145- .handle ((path , exception ) -> {
115+ return delegate .move (source , target , replace ) //
116+ .whenComplete ((path , exception ) -> {
146117 metadataCache .invalidate (source );
147118 metadataCache .invalidate (target );
148- if (exception == null ) {
149- return CompletableFuture .completedFuture (path );
150- } else {
151- return CompletableFuture .<CloudPath >failedFuture (exception );
152- }
153- }).thenCompose (Function .identity ());
119+ });
154120 }
155121
156122 private void evictIncludingDescendants (CloudPath cleartextPath ) {
0 commit comments