All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anwiba.tools.graphml.utilities.Node Maven / Gradle / Ivy

There is a newer version: 1.2.50
Show newest version
/*
 * #%L
 * *
 * %%
 * Copyright (C) 2007 - 2017 Andreas W. Bartels ([email protected])
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
package net.anwiba.tools.graphml.utilities;

import java.util.HashSet;
import java.util.Set;

public class Node {

  Set> subchildren = new HashSet<>();
  Set> notContainingBySubchildren = new HashSet<>();
  Set> children = new HashSet<>();
  private final T object;

  public Node(final T object) {
    this.object = object;
  }

  public void add(final Node childe) {
    this.children.add(childe);
  }

  public void remove(final Node childe) {
    this.children.remove(childe);
  }

  public Iterable> children() {
    return this.children;
  }

  public T getObject() {
    return this.object;
  }

  public boolean childrenContains(final Node node) {
    if (this.subchildren.contains(node)) {
      return true;
    }
    if (this.notContainingBySubchildren.contains(node)) {
      return false;
    }
    for (final Node child : this.children) {
      if (child.contains(node)) {
        this.subchildren.add(node);
        return true;
      }
    }
    this.notContainingBySubchildren.add(node);
    return false;
  }

  public boolean contains(final Node node) {
    if (this.children.contains(node) || this.subchildren.contains(node)) {
      return true;
    }
    if (this.notContainingBySubchildren.contains(node)) {
      return false;
    }
    for (final Node child : this.children) {
      if (child.contains(node)) {
        this.subchildren.add(node);
        return true;
      }
    }
    this.notContainingBySubchildren.add(node);
    return false;
  }

  public void reset() {
    this.subchildren.clear();;
    this.notContainingBySubchildren.clear();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy