Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Migrating CachingCalculatorDecorator from Scala to Java

  • Loading branch information...
commit 10b895c0debd42e1b684fda6301c2b96e49a0f63 1 parent 58d4061
@nurkiewicz authored
View
31 src/main/java/com/blogspot/nurkiewicz/cacheable/CachingCalculatorDecorator.java
@@ -0,0 +1,31 @@
+package com.blogspot.nurkiewicz.cacheable;
+
+import com.blogspot.nurkiewicz.cacheable.calculator.Calculator;
+
+import java.util.Map;
+
+/**
+ * @author Tomasz Nurkiewicz
+ * @since 1/12/13, 4:59 PM
+ */
+public class CachingCalculatorDecorator implements Calculator {
+
+ private final Map<Integer, Integer> cache = new java.util.concurrent.ConcurrentHashMap<Integer, Integer>();
+
+ private final Calculator target;
+
+ public CachingCalculatorDecorator(Calculator target) {
+ this.target = target;
+ }
+
+ @Override
+ public int identity(int x) {
+ final Integer existing = cache.get(x);
+ if (existing != null) {
+ return existing;
+ }
+ final int newValue = target.identity(x);
+ cache.put(x, newValue);
+ return newValue;
+ }
+}
View
20 src/main/scala/com/blogspot/nurkiewicz/cacheable/CachingCalculatorDecorator.scala
@@ -1,20 +0,0 @@
-package com.blogspot.nurkiewicz.cacheable
-
-import calculator.Calculator
-import org.springframework.cache.annotation.Cacheable
-import org.springframework.stereotype.Service
-import org.springframework.beans.factory.annotation.Configurable
-import collection.JavaConversions._
-
-
-class CachingCalculatorDecorator(target: Calculator) extends Calculator {
- private val cache = new java.util.concurrent.ConcurrentHashMap[java.lang.Integer, java.lang.Integer]
-
- override def identity(x: Int) = cache.get(x) match {
- case null =>
- val v = target.identity(x)
- cache.put(x, v)
- v
- case v => v
- }
-}
Please sign in to comment.
Something went wrong with that request. Please try again.