
automately.core.data.predicates.KeyStartsWithPredicate Maven / Gradle / Ivy
package automately.core.data.predicates;
import com.hazelcast.query.Predicate;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Map;
/**
* A simple predicate that allows you to filter out via keys
* that start with a string.
*/
public class KeyStartsWithPredicate implements Serializable, Predicate {
private String key;
private String startsWith;
public KeyStartsWithPredicate(String startsWith) {
this(null, startsWith);
}
public KeyStartsWithPredicate(String key, String startsWith){
this.key = key;
this.startsWith = startsWith;
}
@Override
public boolean apply(Map.Entry entry) {
if(key == null){
return entry.getKey() instanceof String && ((String) entry.getKey()).startsWith(startsWith);
}
Object o = entry.getValue();
Class> clazz = o.getClass();
for(Field field : clazz.getDeclaredFields()) {
//you can also use .toGenericString() instead of .getName(). This will
//give you the type information as well.
if (field.getName().equals(key)) {
try {
Object c = field.get(o);
if(c != null && c instanceof String){
if(((String) c).startsWith(startsWith)){
return true;
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy