EmptyTreeIterator.java

  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. package org.eclipse.jgit.treewalk;

  13. import java.io.IOException;

  14. import org.eclipse.jgit.errors.CorruptObjectException;
  15. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  16. import org.eclipse.jgit.lib.ObjectId;
  17. import org.eclipse.jgit.lib.ObjectReader;

  18. /**
  19.  * Iterator over an empty tree (a directory with no files).
  20.  */
  21. public class EmptyTreeIterator extends AbstractTreeIterator {
  22.     /**
  23.      * Create a new iterator with no parent.
  24.      */
  25.     public EmptyTreeIterator() {
  26.         // Create a root empty tree.
  27.     }

  28.     EmptyTreeIterator(AbstractTreeIterator p) {
  29.         super(p);
  30.         pathLen = pathOffset;
  31.     }

  32.     /**
  33.      * Create an iterator for a subtree of an existing iterator.
  34.      * <p>
  35.      * The caller is responsible for setting up the path of the child iterator.
  36.      *
  37.      * @param p
  38.      *            parent tree iterator.
  39.      * @param childPath
  40.      *            path array to be used by the child iterator. This path must
  41.      *            contain the path from the top of the walk to the first child
  42.      *            and must end with a '/'.
  43.      * @param childPathOffset
  44.      *            position within <code>childPath</code> where the child can
  45.      *            insert its data. The value at
  46.      *            <code>childPath[childPathOffset-1]</code> must be '/'.
  47.      */
  48.     public EmptyTreeIterator(final AbstractTreeIterator p,
  49.             final byte[] childPath, final int childPathOffset) {
  50.         super(p, childPath, childPathOffset);
  51.         pathLen = childPathOffset - 1;
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public AbstractTreeIterator createSubtreeIterator(ObjectReader reader)
  56.             throws IncorrectObjectTypeException, IOException {
  57.         return new EmptyTreeIterator(this);
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public boolean hasId() {
  62.         return false;
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     public ObjectId getEntryObjectId() {
  67.         return ObjectId.zeroId();
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public byte[] idBuffer() {
  72.         return zeroid;
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public int idOffset() {
  77.         return 0;
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public void reset() {
  82.         // Do nothing.
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public boolean first() {
  87.         return true;
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public boolean eof() {
  92.         return true;
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     public void next(int delta) throws CorruptObjectException {
  97.         // Do nothing.
  98.     }

  99.     /** {@inheritDoc} */
  100.     @Override
  101.     public void back(int delta) throws CorruptObjectException {
  102.         // Do nothing.
  103.     }

  104.     /** {@inheritDoc} */
  105.     @Override
  106.     public void stopWalk() {
  107.         if (parent != null)
  108.             parent.stopWalk();
  109.     }

  110.     /** {@inheritDoc} */
  111.     @Override
  112.     protected boolean needsStopWalk() {
  113.         return parent != null && parent.needsStopWalk();
  114.     }
  115. }