Author: tommaso
Date: Wed Feb  6 14:07:35 2013
New Revision: 1442968

URL: http://svn.apache.org/viewvc?rev=1442968&view=rev
Log:
DIRECTMEMORY-124 - added Iterable over Cache values

Added:
    
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/utils/
    
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/utils/CacheValuesIterable.java
   (with props)
    
directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/utils/
    
directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/utils/CacheValuesIterableTest.java
   (with props)
Modified:
    
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java

Modified: 
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java
URL: 
http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java?rev=1442968&r1=1442967&r2=1442968&view=diff
==============================================================================
--- 
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java
 (original)
+++ 
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java
 Wed Feb  6 14:07:35 2013
@@ -124,7 +124,7 @@ public interface CacheService<K, V>
     /**
      * Removes the pointer and frees the underlying memory area.
      * 
-     * @param key The pointer to remove
+     * @param pointer The pointer to remove
      */
     void free( Pointer<V> pointer );
 

Added: 
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/utils/CacheValuesIterable.java
URL: 
http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/utils/CacheValuesIterable.java?rev=1442968&view=auto
==============================================================================
--- 
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/utils/CacheValuesIterable.java
 (added)
+++ 
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/utils/CacheValuesIterable.java
 Wed Feb  6 14:07:35 2013
@@ -0,0 +1,67 @@
+package org.apache.directmemory.utils;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Iterator;
+
+import org.apache.directmemory.cache.CacheService;
+
+/**
+ * A simple {@link Iterable} over {@link CacheService}'s values
+ */
+public class CacheValuesIterable<K, V>
+    implements Iterable<V>
+{
+
+    private final CacheService<K, V> cacheService;
+
+    private final Iterator<K> keysIterator;
+
+    public CacheValuesIterable( CacheService<K, V> cacheService )
+    {
+        this.cacheService = cacheService;
+        this.keysIterator = cacheService.getMap().keySet().iterator();
+    }
+
+    @Override
+    public Iterator<V> iterator()
+    {
+        return new Iterator<V>()
+        {
+            @Override
+            public boolean hasNext()
+            {
+                return keysIterator.hasNext();
+            }
+
+            @Override
+            public V next()
+            {
+                return cacheService.retrieve( keysIterator.next() );
+            }
+
+            @Override
+            public void remove()
+            {
+                keysIterator.remove();
+            }
+        };
+    }
+}

Propchange: 
directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/utils/CacheValuesIterable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/utils/CacheValuesIterableTest.java
URL: 
http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/utils/CacheValuesIterableTest.java?rev=1442968&view=auto
==============================================================================
--- 
directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/utils/CacheValuesIterableTest.java
 (added)
+++ 
directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/utils/CacheValuesIterableTest.java
 Wed Feb  6 14:07:35 2013
@@ -0,0 +1,60 @@
+package org.apache.directmemory.utils;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.directmemory.DirectMemory;
+import org.apache.directmemory.cache.CacheService;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Testcase for {@link CacheValuesIterable}
+ */
+public class CacheValuesIterableTest
+{
+
+    @Test
+    public void simpleTest()
+        throws Exception
+    {
+        CacheService<String, Long> cache =
+            new DirectMemory<String, Long>().setNumberOfBuffers( 10 ).setSize( 
1000 ).setInitialCapacity( 10000 ).setConcurrencyLevel( 4 ).newCacheService();
+
+        assertNull( cache.retrieve( "a" ) );
+        assertNotNull( cache.put( "a", 1L ) );
+        assertNotNull( cache.put( "b", 2L ) );
+        assertNotNull( cache.put( "c", 3L ) );
+        assertNotNull( cache.retrieve( "a" ) );
+        assertNotNull( cache.retrieve( "b" ) );
+        assertNotNull( cache.retrieve( "c" ) );
+        assertEquals( 1L, cache.retrieve( "a" ).longValue() );
+
+        CacheValuesIterable<String, Long> cacheValuesIterable = new 
CacheValuesIterable<String, Long>( cache );
+        for ( Long longVal : cacheValuesIterable )
+        {
+            assertNotNull( longVal );
+            assertTrue( longVal > 0 );
+        }
+    }
+}

Propchange: 
directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/utils/CacheValuesIterableTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to