You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShapeCommandMap.java 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Created on Jan 7, 2005
  3. *
  4. * @author Mohan Radhakrishnan
  5. */
  6. //package com.blueprint.ui.util;
  7. import java.lang.ref.ReferenceQueue;
  8. import java.lang.ref.SoftReference;
  9. import java.util.AbstractMap;
  10. import java.util.HashMap;
  11. import java.util.LinkedHashSet;
  12. import java.util.Map;
  13. import java.util.Set;
  14. /*
  15. * Undo/redo for each shape. This can be used to maintain
  16. * a list of changes to rollback. Since the calls to the
  17. * model tier are direct and the reverse calls to update the
  18. * UI are Commands, this list is for the latter.
  19. */
  20. public class ShapeCommandMap<K,V> extends AbstractMap<K,V> {
  21. private final Map<K, SoftReference<V>> internalMap = new HashMap<K, SoftReference<V>>();
  22. private final ReferenceQueue<V> queue = new ReferenceQueue<V>();
  23. public V put( K key, V value ){
  24. //remove stale entries
  25. SoftReference<V> ref = new SoftReference<V>( value, queue );
  26. SoftReference<V> s = internalMap.put( key, ref );
  27. return ( s != null ? s.get() : null );
  28. }
  29. /*public V get( K key ){
  30. //remove stale entries
  31. SoftReference<V> value = internalMap.get( key );
  32. return ( value != null ? value.get() : null );
  33. }*/
  34. public Set<Entry<K,V>> entrySet(){
  35. Set<Entry<K,V>> commands = new LinkedHashSet<Entry<K,V>>();
  36. for( final Entry<K,SoftReference<V>> entry : internalMap.entrySet() ){
  37. final V value = entry.getValue().get();
  38. commands.add( new Entry<K,V>(){
  39. public K getKey(){
  40. return entry.getKey();
  41. }
  42. public V getValue(){
  43. return value;
  44. }
  45. public V setValue( V v ){
  46. entry.setValue(
  47. new SoftReference<V>( v, queue ) );
  48. return value;
  49. }
  50. });
  51. }
  52. return commands;
  53. }
  54. }
  55. aspect TriggerBug {
  56. public void foo() {
  57. ShapeCommandMap<String,String> map = new ShapeCommandMap<String,String>();
  58. map.put("hi","there");
  59. }
  60. before() : execution(* getValue(..)) {
  61. System.out.println("a matching call");
  62. }
  63. }