diff options
author | aclement <aclement> | 2011-06-15 16:14:41 +0000 |
---|---|---|
committer | aclement <aclement> | 2011-06-15 16:14:41 +0000 |
commit | 77da838587dd23523cba21f2e8b77f391f54601e (patch) | |
tree | f3e78e7da81fd2c56c2e4485ed2530d6656bdf34 /tests/bugs1612 | |
parent | 1939b950303f6e5cf417be3cdde605812fd95343 (diff) | |
download | aspectj-77da838587dd23523cba21f2e8b77f391f54601e.tar.gz aspectj-77da838587dd23523cba21f2e8b77f391f54601e.zip |
349398
Diffstat (limited to 'tests/bugs1612')
-rw-r--r-- | tests/bugs1612/pr349398/CacheAspect.java | 35 | ||||
-rw-r--r-- | tests/bugs1612/pr349398/DataGenerator.java | 14 | ||||
-rw-r--r-- | tests/bugs1612/pr349398/DataGeneratorCacheAspect.java | 13 | ||||
-rw-r--r-- | tests/bugs1612/pr349398/DataGeneratorTest.java | 41 |
4 files changed, 103 insertions, 0 deletions
diff --git a/tests/bugs1612/pr349398/CacheAspect.java b/tests/bugs1612/pr349398/CacheAspect.java new file mode 100644 index 000000000..3240327b6 --- /dev/null +++ b/tests/bugs1612/pr349398/CacheAspect.java @@ -0,0 +1,35 @@ +//package info.unterstein.hagen.moderne.ea6.a3; + +import java.util.HashMap; + +/** + * Enables a more complex and generic caching aspect which can be extended to +be + * used in several use cases. + * + * @author <a href="mailto:unterstein@me.com">Johannes Unterstein</a> + * @param <k> + * the class of the keys + * @param <V> + * the class of the cached values + */ +public abstract aspect CacheAspect<V> { + private HashMap<Object, V> cache; + + public abstract pointcut cachePoint(Object key); + + V around(Object key) : cachePoint(key) { + if (this.cache == null) { + this.cache = new HashMap<Object, V>(); + } + V result; + if (this.cache.containsKey(key)) { + result = this.cache.get(key); + } else { + result = proceed(key); + this.cache.put(key, result); + } +Object o = this.cache; + return result; + } +} diff --git a/tests/bugs1612/pr349398/DataGenerator.java b/tests/bugs1612/pr349398/DataGenerator.java new file mode 100644 index 000000000..65619545a --- /dev/null +++ b/tests/bugs1612/pr349398/DataGenerator.java @@ -0,0 +1,14 @@ +//package info.unterstein.hagen.moderne.ea6.a3; + +public class DataGenerator { + + private static final int MAGIC_NUMBER = 23; + + public Integer getData(Integer i) { + try { + Thread.sleep(100); + } catch (InterruptedException ex) { + } + return new Integer(i * MAGIC_NUMBER); + } +} diff --git a/tests/bugs1612/pr349398/DataGeneratorCacheAspect.java b/tests/bugs1612/pr349398/DataGeneratorCacheAspect.java new file mode 100644 index 000000000..53c941bf3 --- /dev/null +++ b/tests/bugs1612/pr349398/DataGeneratorCacheAspect.java @@ -0,0 +1,13 @@ +//package info.unterstein.hagen.moderne.ea6.a3; + +/** + * An extension of the generic cache for the concrete use case of caching the + * {@link DataGenerator}. + * + * @author <a href="mailto:unterstein@me.com">Johannes Unterstein</a> + */ +public aspect DataGeneratorCacheAspect extends CacheAspect<Integer> { + + public pointcut cachePoint(Object key) : call(Integer +DataGenerator.getData(Integer)) && args(key); +} diff --git a/tests/bugs1612/pr349398/DataGeneratorTest.java b/tests/bugs1612/pr349398/DataGeneratorTest.java new file mode 100644 index 000000000..9a2c0ec0a --- /dev/null +++ b/tests/bugs1612/pr349398/DataGeneratorTest.java @@ -0,0 +1,41 @@ +//package info.unterstein.hagen.moderne.ea6.a3; + +public class DataGeneratorTest { + public static void main(String []argv) { + new DataGeneratorTest().testGetData(); + new DataGeneratorTest().testGetDataSpeedUp(); + } + + public void testGetData() { + DataGenerator generator = new DataGenerator(); + assertEquals(new Integer(0), generator.getData(0)); + assertEquals(new Integer(23), generator.getData(1)); + assertEquals(new Integer(2 * 23), generator.getData(2)); + } + + public void assertEquals(Object o, Object p) { + if (!o.equals(p)) { + throw new IllegalStateException(); + } + } + + public void assertTrue(boolean b) { + if (!b) { + throw new IllegalStateException(); + } + } + + public void testGetDataSpeedUp() { + DataGenerator generator = new DataGenerator(); + long before = System.currentTimeMillis(); + for (int i = 0; i < 5; i++) { + generator.getData(i); + } + for (int i = 0; i < 5; i++) { + generator.getData(0); + } + long after = System.currentTimeMillis(); + assertTrue((after - before) < 600); + } +} + |