KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > graph > CompoundDiff


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.graph;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ListIterator JavaDoc;
28
29 /**
30  * A GraphDiff that is a list of other GraphDiffs.
31  *
32  * @since 1.2
33  * @author Andrus Adamchik
34  */

35 public class CompoundDiff implements GraphDiff {
36
37     protected List JavaDoc diffs;
38
39     /**
40      * Creates an empty CompoundDiff instance.
41      */

42     public CompoundDiff() {
43     }
44
45     /**
46      * Creates CompoundDiff instance. Note that a List is not cloned in this constructor,
47      * so subsequent calls to add and addAll would modify the original list.
48      */

49     public CompoundDiff(List JavaDoc diffs) {
50         this.diffs = diffs;
51     }
52
53     /**
54      * Returns true if this diff has no other diffs or if all of its diffs are noops.
55      */

56     public boolean isNoop() {
57         if (diffs == null || diffs.isEmpty()) {
58             return true;
59         }
60
61         Iterator JavaDoc 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 JavaDoc 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 JavaDoc diffs) {
82         nonNullDiffs().addAll(diffs);
83     }
84
85     /**
86      * Iterates over diffs list, calling "apply" on each individual diff.
87      */

88     public void apply(GraphChangeHandler tracker) {
89         if (diffs == null) {
90             return;
91         }
92
93         // implements a naive linear commit - simply replay stored operations
94
Iterator JavaDoc it = diffs.iterator();
95         while (it.hasNext()) {
96             GraphDiff change = (GraphDiff) it.next();
97             change.apply(tracker);
98         }
99     }
100
101     /**
102      * Iterates over diffs list in reverse order, calling "apply" on each individual diff.
103      */

104     public void undo(GraphChangeHandler tracker) {
105         if (diffs == null) {
106             return;
107         }
108
109         ListIterator JavaDoc it = diffs.listIterator(diffs.size());
110         while (it.hasPrevious()) {
111             GraphDiff change = (GraphDiff) it.previous();
112             change.undo(tracker);
113         }
114     }
115
116     List JavaDoc nonNullDiffs() {
117         if (diffs == null) {
118             synchronized (this) {
119                 if (diffs == null) {
120                     diffs = new ArrayList JavaDoc();
121                 }
122             }
123         }
124
125         return diffs;
126     }
127 }
128
Popular Tags