RevFlagSet.java

  1. /*
  2.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.revwalk;

  11. import java.util.AbstractSet;
  12. import java.util.ArrayList;
  13. import java.util.Collection;
  14. import java.util.Iterator;
  15. import java.util.List;

  16. /**
  17.  * Multiple application level mark bits for
  18.  * {@link org.eclipse.jgit.revwalk.RevObject}s.
  19.  *
  20.  * @see RevFlag
  21.  */
  22. public class RevFlagSet extends AbstractSet<RevFlag> {
  23.     int mask;

  24.     private final List<RevFlag> active;

  25.     /**
  26.      * Create an empty set of flags.
  27.      */
  28.     public RevFlagSet() {
  29.         active = new ArrayList<>();
  30.     }

  31.     /**
  32.      * Create a set of flags, copied from an existing set.
  33.      *
  34.      * @param s
  35.      *            the set to copy flags from.
  36.      */
  37.     public RevFlagSet(RevFlagSet s) {
  38.         mask = s.mask;
  39.         active = new ArrayList<>(s.active);
  40.     }

  41.     /**
  42.      * Create a set of flags, copied from an existing collection.
  43.      *
  44.      * @param s
  45.      *            the collection to copy flags from.
  46.      */
  47.     public RevFlagSet(Collection<RevFlag> s) {
  48.         this();
  49.         addAll(s);
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public boolean contains(Object o) {
  54.         if (o instanceof RevFlag)
  55.             return (mask & ((RevFlag) o).mask) != 0;
  56.         return false;
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public boolean containsAll(Collection<?> c) {
  61.         if (c instanceof RevFlagSet) {
  62.             final int cMask = ((RevFlagSet) c).mask;
  63.             return (mask & cMask) == cMask;
  64.         }
  65.         return super.containsAll(c);
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public boolean add(RevFlag flag) {
  70.         if ((mask & flag.mask) != 0)
  71.             return false;
  72.         mask |= flag.mask;
  73.         int p = 0;
  74.         while (p < active.size() && active.get(p).mask < flag.mask)
  75.             p++;
  76.         active.add(p, flag);
  77.         return true;
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public boolean remove(Object o) {
  82.         final RevFlag flag = (RevFlag) o;
  83.         if ((mask & flag.mask) == 0)
  84.             return false;
  85.         mask &= ~flag.mask;
  86.         for (int i = 0; i < active.size(); i++)
  87.             if (active.get(i).mask == flag.mask)
  88.                 active.remove(i);
  89.         return true;
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public Iterator<RevFlag> iterator() {
  94.         final Iterator<RevFlag> i = active.iterator();
  95.         return new Iterator<RevFlag>() {
  96.             private RevFlag current;

  97.             @Override
  98.             public boolean hasNext() {
  99.                 return i.hasNext();
  100.             }

  101.             @Override
  102.             public RevFlag next() {
  103.                 return current = i.next();
  104.             }

  105.             @Override
  106.             public void remove() {
  107.                 mask &= ~current.mask;
  108.                 i.remove();
  109.             }
  110.         };
  111.     }

  112.     /** {@inheritDoc} */
  113.     @Override
  114.     public int size() {
  115.         return active.size();
  116.     }
  117. }