View Javadoc

1   /**
2    * Copyright (c) 2011, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  
28  package org.treetank.service.xml.diff;
29  
30  import java.util.HashSet;
31  import java.util.Set;
32  
33  import org.treetank.api.INodeReadTrx;
34  import org.treetank.exception.TTException;
35  import org.treetank.node.interfaces.IStructNode;
36  import org.treetank.service.xml.diff.DiffFactory.EDiff;
37  
38  /**
39   * Implements {@link IDiffObservable}, which can be used for all classes, which
40   * implement the {@link IDiff} interface.
41   * 
42   * @author Johannes Lichtenberger, University of Konstanz
43   * 
44   */
45  abstract class AbsDiffObservable implements IDiffObservable {
46  
47      /** {@link IReadTransaction} on new revision. */
48      transient INodeReadTrx mNewRtx;
49  
50      /** {@link IReadTransaction} on old revision. */
51      transient INodeReadTrx mOldRtx;
52  
53      /**
54       * {@link Set} of observers, which want to be notified of the encountered
55       * differences.
56       */
57      private final Set<IDiffObserver> mDiffObservers;
58  
59      /**
60       * Default constructor.
61       */
62      AbsDiffObservable() {
63          mDiffObservers = new HashSet<IDiffObserver>();
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public final void fireDiff(final EDiff paramDiff, final IStructNode paramNewNode,
69          final IStructNode paramOldNode, final DiffDepth paramDepth) {
70          for (final IDiffObserver observer : mDiffObservers) {
71              observer.diffListener(paramDiff, paramNewNode, paramOldNode, paramDepth);
72          }
73      }
74  
75      /**
76       * {@inheritDoc}
77       * 
78       */
79      @Override
80      public final void done() throws TTException {
81          mNewRtx.close();
82          mOldRtx.close();
83  
84          for (final IDiffObserver observer : mDiffObservers) {
85              observer.diffDone();
86          }
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public final void addObserver(final IDiffObserver paramObserver) {
92          mDiffObservers.add(paramObserver);
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public final void removeObserver(final IDiffObserver paramObserver) {
98          mDiffObservers.remove(paramObserver);
99      }
100 }