001    package org.expasy.jpl.commons.collection.graph;
002    
003    
004    import java.util.regex.Matcher;
005    import java.util.regex.Pattern;
006    import org.apache.commons.collections15.Predicate;
007    import org.apache.commons.collections15.Transformer;
008    import edu.uci.ics.jung.graph.Graph;
009    
010    
011    /**
012     * A simple Graph node predicate (needed for collapsable graph).
013     * 
014     * @author nikitin
015     * 
016     * @version 1.0
017     * 
018     */
019    public final class GraphPredicate {
020            
021            public static class GraphInstancePredicate<T> implements Predicate<T> {
022                    
023                    @SuppressWarnings("unchecked")
024                    @Override
025                    public boolean evaluate(T o) {
026                            
027                            return o instanceof Graph;
028                    }
029                    
030                    @Override
031                    public String toString() {
032                            return "GraphInstancePredicate";
033                    }
034            }
035            
036            public static class VertexPatternPredicate<T> implements Predicate<T> {
037                    
038                    private Pattern pattern;
039                    private Transformer<T, String> transformer;
040                    
041                    public VertexPatternPredicate(Pattern pattern) {
042                            this.pattern = pattern;
043                            this.transformer = new Transformer<T, String>() {
044                                    
045                                    @Override
046                                    public String transform(T o) {
047                                            return o.toString();
048                                    }
049                                    
050                            };
051                    }
052                    
053                    /**
054                     * @param transformer the node to string transformer.
055                     */
056                    public void setTransformer(Transformer<T, String> transformer) {
057                            this.transformer = transformer;
058                    }
059                    
060                    @Override
061                    public boolean evaluate(T node) {
062                            Matcher matcher = pattern.matcher(transformer.transform(node));
063                            return matcher.find();
064                    }
065            }
066            
067            public static class ValuePredicate<T extends Number> implements
068                Predicate<T> {
069                    
070                    private T threshold;
071                    
072                    public ValuePredicate(T threshold) {
073                            this.threshold = threshold;
074                    }
075                    
076                    @Override
077                    public boolean evaluate(T value) {
078                            return value.doubleValue() >= threshold.doubleValue();
079                    }
080            }
081    }