View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
11   */
12  
13  package org.eclipse.jgit.treewalk;
14  
15  import java.io.IOException;
16  
17  import org.eclipse.jgit.errors.CorruptObjectException;
18  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.eclipse.jgit.lib.ObjectReader;
21  
22  /**
23   * Iterator over an empty tree (a directory with no files).
24   */
25  public class EmptyTreeIterator extends AbstractTreeIterator {
26  	/**
27  	 * Create a new iterator with no parent.
28  	 */
29  	public EmptyTreeIterator() {
30  		// Create a root empty tree.
31  	}
32  
33  	EmptyTreeIterator(AbstractTreeIterator p) {
34  		super(p);
35  		pathLen = pathOffset;
36  	}
37  
38  	/**
39  	 * Create an iterator for a subtree of an existing iterator.
40  	 * <p>
41  	 * The caller is responsible for setting up the path of the child iterator.
42  	 *
43  	 * @param p
44  	 *            parent tree iterator.
45  	 * @param childPath
46  	 *            path array to be used by the child iterator. This path must
47  	 *            contain the path from the top of the walk to the first child
48  	 *            and must end with a '/'.
49  	 * @param childPathOffset
50  	 *            position within <code>childPath</code> where the child can
51  	 *            insert its data. The value at
52  	 *            <code>childPath[childPathOffset-1]</code> must be '/'.
53  	 */
54  	public EmptyTreeIterator(final AbstractTreeIterator p,
55  			final byte[] childPath, final int childPathOffset) {
56  		super(p, childPath, childPathOffset);
57  		pathLen = childPathOffset - 1;
58  	}
59  
60  	/** {@inheritDoc} */
61  	@Override
62  	public AbstractTreeIterator createSubtreeIterator(ObjectReader reader)
63  			throws IncorrectObjectTypeException, IOException {
64  		return new EmptyTreeIterator(this);
65  	}
66  
67  	/** {@inheritDoc} */
68  	@Override
69  	public boolean hasId() {
70  		return false;
71  	}
72  
73  	/** {@inheritDoc} */
74  	@Override
75  	public ObjectId getEntryObjectId() {
76  		return ObjectId.zeroId();
77  	}
78  
79  	/** {@inheritDoc} */
80  	@Override
81  	public byte[] idBuffer() {
82  		return zeroid;
83  	}
84  
85  	/** {@inheritDoc} */
86  	@Override
87  	public int idOffset() {
88  		return 0;
89  	}
90  
91  	/** {@inheritDoc} */
92  	@Override
93  	public void reset() {
94  		// Do nothing.
95  	}
96  
97  	/** {@inheritDoc} */
98  	@Override
99  	public boolean first() {
100 		return true;
101 	}
102 
103 	/** {@inheritDoc} */
104 	@Override
105 	public boolean eof() {
106 		return true;
107 	}
108 
109 	/** {@inheritDoc} */
110 	@Override
111 	public void next(int delta) throws CorruptObjectException {
112 		// Do nothing.
113 	}
114 
115 	/** {@inheritDoc} */
116 	@Override
117 	public void back(int delta) throws CorruptObjectException {
118 		// Do nothing.
119 	}
120 
121 	/** {@inheritDoc} */
122 	@Override
123 	public void stopWalk() {
124 		if (parent != null)
125 			parent.stopWalk();
126 	}
127 
128 	/** {@inheritDoc} */
129 	@Override
130 	protected boolean needsStopWalk() {
131 		return parent != null && parent.needsStopWalk();
132 	}
133 }