public void clean ( ) { Instant minAge = Instant . now ( ) . minus ( storageTimeInSeconds , ChronoUnit . SECONDS ) ; synchronized ( messages ) { messages . removeIf ( messageRef -> Optional . ofNullable ( messageRef . get ( ) ) . map ( message -> ! message . isCachedForever ( ) && message . getCreationTimestamp ( ) . isBefore ( minAge ) ) . orElse ( true ) ) ; long foreverCachedAmount = messages . stream ( ) . map ( Reference :: get ) . filter ( Objects :: nonNull ) . filter ( Message :: isCachedForever ) . count ( ) ; messages . removeAll ( messages . stream ( ) . filter ( messageRef -> Optional . ofNullable ( messageRef . get ( ) ) . map ( message -> ! message . isCachedForever ( ) ) . orElse ( true ) ) . limit ( Math . max ( 0 , messages . size ( ) - capacity - foreverCachedAmount ) ) . collect ( Collectors . toList ( ) ) ) ; } }
Cleans the cache .