Author: tv
Date: Mon Dec 10 20:58:01 2018
New Revision: 1848636

URL: http://svn.apache.org/viewvc?rev=1848636&view=rev
Log:
Use streaming where appropriate

Removed:
    
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/struct/LRUMapEntry.java
Modified:
    
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/match/KeyMatcherPatternImpl.java
    
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
    
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/struct/AbstractLRUMap.java

Modified: 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/match/KeyMatcherPatternImpl.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/match/KeyMatcherPatternImpl.java?rev=1848636&r1=1848635&r2=1848636&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/match/KeyMatcherPatternImpl.java
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/match/KeyMatcherPatternImpl.java
 Mon Dec 10 20:58:01 2018
@@ -1,5 +1,9 @@
 package org.apache.commons.jcs.engine.match;
 
+import java.util.Set;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -21,11 +25,6 @@ package org.apache.commons.jcs.engine.ma
 
 import org.apache.commons.jcs.engine.match.behavior.IKeyMatcher;
 
-import java.util.HashSet;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
 /** This implementation of the KeyMatcher uses standard Java Pattern matching. 
*/
 public class KeyMatcherPatternImpl<K>
     implements IKeyMatcher<K>
@@ -45,22 +44,8 @@ public class KeyMatcherPatternImpl<K>
     {
         Pattern compiledPattern = Pattern.compile( pattern );
 
-        Set<K> matchingKeys = new HashSet<K>();
-
-        // Look for matches
-        for (K key : keyArray)
-        {
-            // TODO we might want to match on the toString.
-            if ( key instanceof String )
-            {
-                Matcher matcher = compiledPattern.matcher( (String) key );
-                if ( matcher.matches() )
-                {
-                    matchingKeys.add( key );
-                }
-            }
-        }
-
-        return matchingKeys;
+        return keyArray.stream()
+                .filter(key -> 
compiledPattern.matcher(key.toString()).matches())
+                .collect(Collectors.toSet());
     }
 }

Modified: 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java?rev=1848636&r1=1848635&r2=1848636&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
 Mon Dec 10 20:58:01 2018
@@ -280,11 +280,12 @@ public abstract class AbstractMemoryCach
      */
     public void dumpMap()
     {
-        log.debug( "dumpingMap" );
-        for (Map.Entry<K, MemoryElementDescriptor<K, V>> e : map.entrySet())
+        if (log.isDebugEnabled())
         {
-            MemoryElementDescriptor<K, V> me = e.getValue();
-            log.debug( "dumpMap> key=" + e.getKey() + ", val=" + 
me.getCacheElement().getVal() );
+            log.debug("dumpingMap");
+            map.entrySet().forEach(e ->
+                log.debug("dumpMap> key=" + e.getKey() + ", val=" +
+                        e.getValue().getCacheElement().getVal()));
         }
     }
 

Modified: 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/struct/AbstractLRUMap.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/struct/AbstractLRUMap.java?rev=1848636&r1=1848635&r2=1848636&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/struct/AbstractLRUMap.java
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/struct/AbstractLRUMap.java
 Mon Dec 10 20:58:01 2018
@@ -20,17 +20,17 @@ package org.apache.commons.jcs.utils.str
  */
 
 import java.io.Serializable;
+import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
 
 import org.apache.commons.jcs.engine.control.group.GroupAttrName;
 import org.apache.commons.jcs.engine.stats.StatElement;
@@ -45,7 +45,7 @@ import org.apache.commons.logging.LogFac
  * use any but put, get, remove, and clear.
  * <p>
  * Children can implement the processRemovedLRU method if they want to handle 
the removal of the
- * lest recently used item.
+ * least recently used item.
  * <p>
  * This class was abstracted out of the LRU Memory cache. Put, remove, and get 
should be thread
  * safe. It uses a hashtable and our own double linked list.
@@ -162,14 +162,9 @@ public abstract class AbstractLRUMap<K,
     @Override
     public Collection<V> values()
     {
-        List<V> valueList = new ArrayList<V>(map.size());
-
-        for (LRUElementDescriptor<K, V> value : map.values())
-        {
-            valueList.add(value.getPayload());
-        }
-
-        return valueList;
+        return map.values().stream()
+                .map(value -> value.getPayload())
+                .collect(Collectors.toList());
     }
 
     /**
@@ -180,10 +175,8 @@ public abstract class AbstractLRUMap<K,
     {
         if ( source != null )
         {
-            for (Map.Entry<? extends K, ? extends V> entry : source.entrySet())
-            {
-                this.put( entry.getKey(), entry.getValue() );
-            }
+            source.entrySet()
+                .forEach(entry -> put(entry.getKey(), entry.getValue()));
         }
     }
 
@@ -337,7 +330,7 @@ public abstract class AbstractLRUMap<K,
             // The spool will put them in a disk event queue, so there is no
             // need to pre-queue the queuing. This would be a bit wasteful
             // and wouldn't save much time in this synchronous call.
-            while ( shouldRemove() )
+            while (shouldRemove())
             {
                 lock.lock();
                 try
@@ -392,12 +385,12 @@ public abstract class AbstractLRUMap<K,
     @SuppressWarnings("unchecked") // No generics for public fields
     public void dumpCacheEntries()
     {
-        log.debug( "dumpingCacheEntries" );
-        for ( LRUElementDescriptor<K, V> me = list.getFirst(); me != null; me 
= (LRUElementDescriptor<K, V>) me.next )
+        if (log.isDebugEnabled())
         {
-            if ( log.isDebugEnabled() )
+            log.debug("dumpingCacheEntries");
+            for (LRUElementDescriptor<K, V> me = list.getFirst(); me != null; 
me = (LRUElementDescriptor<K, V>) me.next)
             {
-                log.debug( "dumpCacheEntries> key=" + me.getKey() + ", val=" + 
me.getPayload() );
+                log.debug("dumpCacheEntries> key=" + me.getKey() + ", val=" + 
me.getPayload());
             }
         }
     }
@@ -407,14 +400,11 @@ public abstract class AbstractLRUMap<K,
      */
     public void dumpMap()
     {
-        log.debug( "dumpingMap" );
-        for (Map.Entry<K, LRUElementDescriptor<K, V>> e : map.entrySet())
+        if (log.isDebugEnabled())
         {
-            LRUElementDescriptor<K, V> me = e.getValue();
-            if ( log.isDebugEnabled() )
-            {
-                log.debug( "dumpMap> key=" + e.getKey() + ", val=" + 
me.getPayload() );
-            }
+            log.debug("dumpingMap");
+            map.entrySet().forEach(e ->
+                log.debug("dumpMap> key=" + e.getKey() + ", val=" + 
e.getValue().getPayload()));
         }
     }
 
@@ -562,16 +552,10 @@ public abstract class AbstractLRUMap<K,
         lock.lock();
         try
         {
-            // TODO we should return a defensive copy
-            Set<Map.Entry<K, LRUElementDescriptor<K, V>>> entries = 
map.entrySet();
-            Set<Map.Entry<K, V>> unWrapped = new HashSet<Map.Entry<K, V>>();
-
-            for (Map.Entry<K, LRUElementDescriptor<K, V>> pre : entries) {
-                Map.Entry<K, V> post = new LRUMapEntry<K, V>(pre.getKey(), 
pre.getValue().getPayload());
-                unWrapped.add(post);
-            }
-
-            return unWrapped;
+            return map.entrySet().stream()
+                    .map(entry -> new AbstractMap.SimpleEntry<K, V>(
+                            entry.getKey(), entry.getValue().getPayload()))
+                    .collect(Collectors.toSet());
         }
         finally
         {
@@ -585,8 +569,9 @@ public abstract class AbstractLRUMap<K,
     @Override
     public Set<K> keySet()
     {
-        // TODO fix this, it needs to return the keys inside the wrappers.
-        return map.keySet();
+        return map.values().stream()
+                .map(value -> value.getKey())
+                .collect(Collectors.toSet());
     }
 
 }


Reply via email to