1 19 20 package org.apache.cayenne.graph; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.ListIterator ; 28 29 35 public class CompoundDiff implements GraphDiff { 36 37 protected List diffs; 38 39 42 public CompoundDiff() { 43 } 44 45 49 public CompoundDiff(List diffs) { 50 this.diffs = diffs; 51 } 52 53 56 public boolean isNoop() { 57 if (diffs == null || diffs.isEmpty()) { 58 return true; 59 } 60 61 Iterator it = diffs.iterator(); 62 while (it.hasNext()) { 63 if (!((GraphDiff) it.next()).isNoop()) { 64 return false; 65 } 66 } 67 68 return true; 69 } 70 71 public List getDiffs() { 72 return (diffs != null) 73 ? Collections.unmodifiableList(diffs) 74 : Collections.EMPTY_LIST; 75 } 76 77 public void add(GraphDiff diff) { 78 nonNullDiffs().add(diff); 79 } 80 81 public void addAll(Collection diffs) { 82 nonNullDiffs().addAll(diffs); 83 } 84 85 88 public void apply(GraphChangeHandler tracker) { 89 if (diffs == null) { 90 return; 91 } 92 93 Iterator it = diffs.iterator(); 95 while (it.hasNext()) { 96 GraphDiff change = (GraphDiff) it.next(); 97 change.apply(tracker); 98 } 99 } 100 101 104 public void undo(GraphChangeHandler tracker) { 105 if (diffs == null) { 106 return; 107 } 108 109 ListIterator it = diffs.listIterator(diffs.size()); 110 while (it.hasPrevious()) { 111 GraphDiff change = (GraphDiff) it.previous(); 112 change.undo(tracker); 113 } 114 } 115 116 List nonNullDiffs() { 117 if (diffs == null) { 118 synchronized (this) { 119 if (diffs == null) { 120 diffs = new ArrayList (); 121 } 122 } 123 } 124 125 return diffs; 126 } 127 } 128 | Popular Tags |