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.filter;
009
010import java.util.ArrayList;
011import java.util.Collection;
012
013import javax.jmi.reflect.RefObject;
014
015import net.mdatools.modelant.core.api.Filter;
016
017/**
018 * Filter the collection based on the contents of "name" field, if any
019 * @author Rusi Popov (popovr@mdatools.net)
020 */
021public class FilterByName<T extends RefObject> implements Filter<T> {
022
023  private final String target;
024
025  /**
026   * @param target not null name value to search for
027   */
028  public FilterByName(String target) {
029    this.target = target;
030  }
031
032  public Collection<T> execute(Collection<T> collection) throws RuntimeException, IllegalArgumentException {
033    Collection<T> result;
034    Object value;
035
036    result = new ArrayList<>();
037    for (T element:collection) {
038      value = element.refGetValue( "name" );
039
040      if ( target.equals(value) ) {
041        result.add(element);
042      }
043    }
044    return result;
045  }
046
047}