KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jrcs > diff > myers > PathNode


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowledgement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgement may appear in the software itself,
26  * if and wherever such third-party acknowledgements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  */

57
58 package org.apache.commons.jrcs.diff.myers;
59
60 /**
61  * A node in a diffpath.
62  *
63  * @version $Revision: 1.1 $ $Date: 2004/10/06 09:47:23 $
64  * @author <a HREF="mailto:juanco@suigeneris.org">Juanco Anez</a>
65  *
66  * @see DiffNode
67  * @see Snake
68  *
69  */

70 public abstract class PathNode
71 {
72     /** Position in the original sequence. */
73     public final int i;
74     /** Position in the revised sequence. */
75     public final int j;
76     /** The previous node in the path. */
77     public final PathNode prev;
78
79     /**
80      * Concatenates a new path node with an existing diffpath.
81      * @param i The position in the original sequence for the new node.
82      * @param j The position in the revised sequence for the new node.
83      * @param prev The previous node in the path.
84      */

85     public PathNode(int i, int j, PathNode prev)
86     {
87         this.i = i;
88         this.j = j;
89         this.prev = prev;
90     }
91
92     /**
93      * Is this node a {@link Snake Snake node}?
94      * @return true if this is a {@link Snake Snake node}
95      */

96     public abstract boolean isSnake();
97
98     /**
99      * Is this a bootstrap node?
100      * <p>
101      * In bottstrap nodes one of the two corrdinates is
102      * less than zero.
103      * @return tru if this is a bootstrap node.
104      */

105     public boolean isBootstrap()
106     {
107         return i < 0 || j < 0;
108     }
109
110     /**
111      * Skips sequences of {@link DiffNode DiffNodes} until a
112      * {@link Snake} or bootstrap node is found, or the end
113      * of the path is reached.
114      * @return The next first {@link Snake} or bootstrap node in the path, or
115      * <code>null</code>
116      * if none found.
117      */

118     public final PathNode previousSnake()
119     {
120         if (isBootstrap())
121             return null;
122         if (!isSnake() && prev != null)
123             return prev.previousSnake();
124         return this;
125     }
126
127     /**
128      * {@inheritDoc}
129      */

130     public String JavaDoc toString()
131     {
132         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("[");
133         PathNode node = this;
134         while (node != null)
135         {
136             buf.append("(");
137             buf.append(Integer.toString(node.i));
138             buf.append(",");
139             buf.append(Integer.toString(node.j));
140             buf.append(")");
141             node = node.prev;
142         }
143         buf.append("]");
144         return buf.toString();
145     }
146 }
Popular Tags