001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.core.model;
028
029import org.apache.logging.log4j.LogManager;
030import org.apache.logging.log4j.Logger;
031
032import java.util.Collection;
033import java.util.LinkedHashSet;
034import java.util.Objects;
035import java.util.Set;
036import java.util.function.Function;
037
038import edu.umd.cs.findbugs.annotations.NonNull;
039import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
040
041/**
042 * Supports walking a portion of a metaschema model collecting a set of
043 * definitions that match the provided filter. For a definition to be collected,
044 * the filter must return {@code true}.
045 */
046public abstract class DefinitionCollectingModelWalker
047    extends ModelWalker<Void> {
048  private static final Logger LOGGER = LogManager.getLogger(DefinitionCollectingModelWalker.class);
049
050  private final Function<IDefinition, Boolean> filter;
051  @NonNull
052  private final Set<IDefinition> definitions = new LinkedHashSet<>();
053
054  @Override
055  protected Void getDefaultData() { // NOPMD - intentional
056    return null;
057  }
058
059  /**
060   * Construct a new walker using the provided filter.
061   *
062   * @param filter
063   *          the filter to match definitions against
064   */
065  protected DefinitionCollectingModelWalker(Function<IDefinition, Boolean> filter) {
066    Objects.requireNonNull(filter, "filter");
067    this.filter = filter;
068  }
069
070  /**
071   * Retrieves the filter used for matching.
072   *
073   * @return the filter
074   */
075  protected Function<IDefinition, Boolean> getFilter() {
076    return filter;
077  }
078
079  /**
080   * Return the collection of definitions matching the configured filter.
081   *
082   * @return the collection of definitions
083   */
084  @NonNull
085  @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "interface doesn't allow modification")
086  public Collection<? extends IDefinition> getDefinitions() {
087    return definitions;
088  }
089
090  @Override
091  protected void visit(IFlagDefinition def, Void data) {
092    if (LOGGER.isTraceEnabled()) {
093      LOGGER.trace("visiting flag definition '{}'", def.toCoordinates());
094    }
095    if (getFilter().apply(def)) {
096      definitions.add(def);
097    }
098  }
099
100  @Override
101  protected boolean visit(IFieldDefinition def, Void data) {
102    if (LOGGER.isTraceEnabled()) {
103      LOGGER.trace("visiting field definition '{}'", def.toCoordinates());
104    }
105    boolean retval;
106    if (definitions.contains(def)) {
107      // no need to visit, since this has already been seen
108      retval = false;
109    } else {
110      if (getFilter().apply(def)) {
111        definitions.add(def);
112      }
113      retval = true;
114    }
115    return retval;
116  }
117
118  @Override
119  protected boolean visit(IAssemblyDefinition def, Void data) {
120    if (LOGGER.isTraceEnabled()) {
121      LOGGER.trace("visiting assembly definition '{}'", def.toCoordinates());
122    }
123    boolean retval;
124    if (definitions.contains(def)) {
125      // no need to visit, since this has already been seen
126      retval = false;
127    } else {
128      if (getFilter().apply(def)) {
129        definitions.add(def);
130      }
131      retval = true;
132    }
133    return retval;
134  }
135}