summaryrefslogtreecommitdiffstats
path: root/tests/bugs150/ShapeCommandMap.java
blob: 38d820841ade73d54d78eeb86fc497089e03fc84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Created on Jan 7, 2005
 *
 * @author Mohan Radhakrishnan
 */
//package com.blueprint.ui.util;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/*
 * Undo/redo for each shape. This can be used to maintain
 * a list of changes to rollback. Since the calls to the
 * model tier are direct and the reverse calls to update the
 * UI are Commands, this list is for the latter.
 */

public class ShapeCommandMap<K,V> extends AbstractMap<K,V> {
	
	private final Map<K, SoftReference<V>> internalMap = new HashMap<K, SoftReference<V>>();
	
	private final ReferenceQueue<V> queue = new ReferenceQueue<V>();
	
	public V put( K key, V value ){
		//remove stale entries
		SoftReference<V> ref = new SoftReference<V>( value, queue );
		SoftReference<V> s = internalMap.put( key, ref );
		return ( s != null ? s.get() : null );
	}
	
	/*public V get( K key ){
		//remove stale entries
		SoftReference<V> value = internalMap.get( key );
		return ( value != null ? value.get() : null );
	}*/
	
	public Set<Entry<K,V>> entrySet(){
		Set<Entry<K,V>> commands = new LinkedHashSet<Entry<K,V>>();
		for( final Entry<K,SoftReference<V>> entry : internalMap.entrySet() ){
			final V value = entry.getValue().get();
			commands.add( new Entry<K,V>(){
							public K getKey(){
								return entry.getKey();
							}
							public V getValue(){
								return value;
							}
							public V setValue( V v ){
								entry.setValue( 
										new SoftReference<V>( v, queue ) );
								return value;
							}
						});
		}
		return commands;
	}
}

aspect TriggerBug {
	
  public void foo() {
	ShapeCommandMap<String,String> map = new ShapeCommandMap<String,String>();  
	map.put("hi","there");
  }
  
  before() : execution(* getValue(..)) {
	  System.out.println("a matching call");
  }
}