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 FilterByField<T extends RefObject> implements Filter<T> { 022 023 private final String fieldName; 024 private final String target; 025 026 /** 027 * @param fieldName non-null field name to evaluate value if it has the value provided 028 * @param target not null name value to search for 029 */ 030 public FilterByField(String fieldName, String target) { 031 this.fieldName = fieldName; 032 this.target = target; 033 } 034 035 public Collection<T> execute(Collection<T> collection) throws RuntimeException, IllegalArgumentException { 036 Collection<T> result; 037 Object value; 038 039 result = new ArrayList<>(); 040 for (T element:collection) { 041 value = element.refGetValue( fieldName ); 042 043 if ( target.equals(value) ) { 044 result.add(element); 045 } 046 } 047 return result; 048 } 049 050}