1 56 package org.objectstyle.cayenne.graph; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.ListIterator ; 64 65 71 public class CompoundDiff implements GraphDiff { 72 73 protected List diffs; 74 75 78 public CompoundDiff() { 79 } 80 81 85 public CompoundDiff(List diffs) { 86 this.diffs = diffs; 87 } 88 89 92 public boolean isNoop() { 93 if (diffs == null) { 94 return true; 95 } 96 97 Iterator it = diffs.iterator(); 98 while (it.hasNext()) { 99 if (!((GraphDiff) it.next()).isNoop()) { 100 return false; 101 } 102 } 103 104 return true; 105 } 106 107 public List getDiffs() { 108 return (diffs != null) 109 ? Collections.unmodifiableList(diffs) 110 : Collections.EMPTY_LIST; 111 } 112 113 public void add(GraphDiff diff) { 114 nonNullDiffs().add(diff); 115 } 116 117 public void addAll(Collection diffs) { 118 nonNullDiffs().addAll(diffs); 119 } 120 121 124 public void apply(GraphChangeHandler tracker) { 125 if (diffs == null) { 126 return; 127 } 128 129 Iterator it = diffs.iterator(); 131 while (it.hasNext()) { 132 GraphDiff change = (GraphDiff) it.next(); 133 change.apply(tracker); 134 } 135 } 136 137 140 public void undo(GraphChangeHandler tracker) { 141 if (diffs == null) { 142 return; 143 } 144 145 ListIterator it = diffs.listIterator(diffs.size()); 146 while (it.hasPrevious()) { 147 GraphDiff change = (GraphDiff) it.previous(); 148 change.undo(tracker); 149 } 150 } 151 152 List nonNullDiffs() { 153 if (diffs == null) { 154 synchronized (this) { 155 if (diffs == null) { 156 diffs = new ArrayList (); 157 } 158 } 159 } 160 161 return diffs; 162 } 163 } 164 | Popular Tags |