DIRECTMEMORY-124 - forgot to svn add Jaromir's classes

git-svn-id: https://svn.apache.org/repos/asf/directmemory/trunk@1443592 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/utils/NonStrictCacheValuesIterator.java b/directmemory-cache/src/main/java/org/apache/directmemory/utils/NonStrictCacheValuesIterator.java
new file mode 100644
index 0000000..91edbe2
--- /dev/null
+++ b/directmemory-cache/src/main/java/org/apache/directmemory/utils/NonStrictCacheValuesIterator.java
@@ -0,0 +1,90 @@
+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;
+
+/**
+ * The relaxed iterator, it guarantees that if hasNext() returns true, the subsequent calling next() always returns a value.
+ * This value might be stalled or expired.
+ * 
+ * @param <K>
+ * @param <V>
+ */
+public class NonStrictCacheValuesIterator<K, V>
+    implements Iterator<V>
+{
+    private Iterator<K> keysIterator;
+
+    private CacheService<K, V> cacheService;
+
+    private K currentKey;
+
+    private K nextKey;
+
+    private V nextValue;
+
+    public NonStrictCacheValuesIterator( Iterator<K> keysIterator, CacheService<K, V> cacheService )
+    {
+        this.keysIterator = keysIterator;
+        this.cacheService = cacheService;
+        findNext();
+    }
+
+    @Override
+    public boolean hasNext()
+    {
+        return nextValue != null;
+    }
+
+    @Override
+    public V next()
+    {
+        V currentValue = nextValue;
+        currentKey = nextKey;
+        findNext();
+        return currentValue;
+    }
+
+    @Override
+    public void remove()
+    {
+        cacheService.free( currentKey );
+    }
+
+    private void findNext()
+    {
+        while ( keysIterator.hasNext() )
+        {
+            K key = keysIterator.next();
+            V value = cacheService.retrieve( key );
+            if ( value != null )
+            {
+                nextKey = key;
+                nextValue = value;
+                return;
+            }
+        }
+        nextKey = null;
+        nextValue = null;
+    }
+}
diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/utils/StrictCacheValuesIterator.java b/directmemory-cache/src/main/java/org/apache/directmemory/utils/StrictCacheValuesIterator.java
new file mode 100644
index 0000000..d9a2a89
--- /dev/null
+++ b/directmemory-cache/src/main/java/org/apache/directmemory/utils/StrictCacheValuesIterator.java
@@ -0,0 +1,66 @@
+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;
+import org.apache.directmemory.memory.Pointer;
+
+public class StrictCacheValuesIterator<K, V>
+    implements Iterator<V>
+{
+    private Iterator<K> keysIterator;
+
+    private CacheService<K, V> cacheService;
+
+    private K currentKey;
+
+    public StrictCacheValuesIterator( Iterator<K> keysIterator, CacheService<K, V> cacheService )
+    {
+        this.keysIterator = keysIterator;
+        this.cacheService = cacheService;
+    }
+
+    @Override
+    public boolean hasNext()
+    {
+        return keysIterator.hasNext();
+    }
+
+    @Override
+    public V next()
+    {
+        currentKey = keysIterator.next();
+        Pointer<V> pointer = cacheService.getPointer( currentKey );
+        if ( pointer != null && pointer.isExpired() )
+        {
+            throw new RuntimeException( "Value pointer has expired" );
+        }
+        return cacheService.retrieve( currentKey );
+    }
+
+    @Override
+    public void remove()
+    {
+        cacheService.free( currentKey );
+        keysIterator.remove();
+    }
+};
\ No newline at end of file