001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.core.operation.model.topology;
009
010import java.util.ArrayList;
011import java.util.Iterator;
012import java.util.List;
013
014import javax.jmi.reflect.RefObject;
015
016import net.mdatools.modelant.core.api.match.MatchingCriteria;
017
018/**
019 * A list of criteria (like class or other list criteria) to compare or print model elements.
020 * Collect the results of all nested matching criteria, that are applicable.
021 * @author Rusi Popov (popovr@mdatools.net)
022 */
023public class ListCriteria implements MatchingCriteria {
024
025  /**
026   * Holds the nested matching criteria
027   */
028  private final List<MatchingCriteria> nested = new ArrayList<MatchingCriteria>();
029
030  /**
031   * @see net.mdatools.modelant.core.api.match.MatchingCriteria#getAttributes(javax.jmi.reflect.RefObject)
032   */
033  public List<String> getAttributes(RefObject forObject) {
034    List<String> result;
035    Iterator<MatchingCriteria> nestedIterator;
036
037    result = new ArrayList<String>(nested.size());
038
039    nestedIterator = nested.iterator();
040    while ( nestedIterator.hasNext() ) {
041      result.addAll( nestedIterator.next().getAttributes( forObject ));
042    }
043    return result;
044  }
045
046
047  /**
048   * @see net.mdatools.modelant.core.api.match.MatchingCriteria#getAssociations(javax.jmi.reflect.RefObject)
049   */
050  public List<String> getAssociations(RefObject forObject) {
051    List<String> result;
052    Iterator<MatchingCriteria> nestedIterator;
053
054    result = new ArrayList<String>();
055
056    nestedIterator = nested.iterator();
057    while ( nestedIterator.hasNext() ) {
058      result.addAll( nestedIterator.next().getAssociations( forObject ));
059    }
060    return result;
061  }
062
063
064  /**
065   * Just registers criteria more general than the previously registered
066   * @param criteria is not null criteria to add
067   */
068  public final void add(MatchingCriteria criteria) {
069    assert criteria != null : "Expected non-null criteria";
070
071    nested.add(criteria);
072  }
073
074
075  /**
076   * @see java.lang.Object#toString()
077   */
078  public String toString() {
079    StringBuilder builder;
080
081    builder = new StringBuilder();
082    builder.append( "ListCriteria [" );
083    builder.append( "nested=" ).append( nested );
084    builder.append( "]" );
085
086    return builder.toString();
087  }
088}