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.List;
012import java.util.StringTokenizer;
013
014import javax.jmi.reflect.RefObject;
015
016import net.mdatools.modelant.core.api.match.MatchingCriteria;
017
018/**
019 * A simple matching criteria for model elements, just by providing the same list of
020 * attributes and associations to all objects.
021 * @author Rusi Popov (popovr@mdatools.net)
022 */
023public class SimpleCriteria implements MatchingCriteria {
024
025  /**
026   * A non-null list of attributes/paths to use
027   */
028  private final List<String> attributes;
029
030  /**
031   * A non-null list of associations/paths to use
032   */
033  private final List<String> associations;
034
035  /**
036   * @param attributes is a non-null comma-separated list of attributes/paths to use
037   * @param associations is a non-null comma-separated list of associations/paths to use
038   */
039  public SimpleCriteria(String attributes, String associations) {
040    this.attributes = parse(attributes);
041    this.associations = parse(associations);
042  }
043
044  /**
045   * Use explicitly provided lists of attribute names and association names.
046   * Take a snapshot of the lists, so they could be reused and changed later, not affecting the criteria
047   * @param attributes not null
048   * @param associations not null
049   */
050  public SimpleCriteria(List<String> attributes, List<String> associations) {
051    this.attributes = new ArrayList<>(attributes);
052    this.associations = new ArrayList<>(associations);
053  }
054
055
056  /**
057   * Parses a comma-separated list of words
058   * @param list is a non-null, possibly empty list of words
059   * @return a non-null, but possibly empty, list of separate words out of the list provided
060   */
061  private List<String> parse(String list) {
062    List<String> result;
063    StringTokenizer tokenizer;
064
065    result = new ArrayList<String>();
066    tokenizer = new StringTokenizer( list, ", " );
067    while ( tokenizer.hasMoreElements() ) {
068      result.add( (String) tokenizer.nextElement() );
069    }
070    return result;
071  }
072
073
074  /**
075   * @see net.mdatools.modelant.core.api.match.MatchingCriteria#getAttributes(javax.jmi.reflect.RefObject)
076   */
077  public List<String> getAttributes(RefObject forObject) {
078    return attributes;
079  }
080
081
082  /**
083   * @see net.mdatools.modelant.core.api.match.MatchingCriteria#getAssociations(javax.jmi.reflect.RefObject)
084   */
085  public List<String> getAssociations(RefObject forObject) {
086    return associations;
087  }
088
089  /**
090   * @see java.lang.Object#toString()
091   */
092  public String toString() {
093    StringBuilder builder;
094
095    builder = new StringBuilder();
096    builder.append( "SimpleCriteria [")
097           .append( "[attributes=" ).append( attributes )
098           .append( ", associations=" ).append( associations )
099           .append( "]" );
100
101    return builder.toString();
102  }
103}