1/*
2 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24package com.sun.hotspot.igv.filter;
25
26import com.sun.hotspot.igv.graph.Diagram;
27import com.sun.hotspot.igv.graph.Figure;
28import com.sun.hotspot.igv.graph.Selector;
29import java.util.ArrayList;
30import java.util.HashSet;
31import java.util.List;
32import java.util.Set;
33
34/**
35 *
36 * @author Thomas Wuerthinger
37 */
38public class RemoveFilter extends AbstractFilter {
39
40    private List<RemoveRule> rules;
41    private String name;
42
43    public RemoveFilter(String name) {
44        this.name = name;
45        rules = new ArrayList<>();
46    }
47
48    @Override
49    public String getName() {
50        return name;
51    }
52
53    @Override
54    public void apply(Diagram diagram) {
55        for (RemoveRule r : rules) {
56            List<Figure> selected = r.getSelector().selected(diagram);
57            Set<Figure> toRemove = new HashSet<>(selected);
58
59            if (r.getRemoveOrphans()) {
60                boolean changed;
61                do {
62                    changed = false;
63                    for (Figure f : diagram.getFigures()) {
64                        if (!toRemove.contains(f)) {
65                            if (toRemove.containsAll(f.getPredecessors()) && toRemove.containsAll(f.getSuccessors())) {
66                                toRemove.add(f);
67                                changed = true;
68                            }
69                        }
70                    }
71                } while (changed);
72            }
73
74            diagram.removeAllFigures(toRemove);
75        }
76    }
77
78    public void addRule(RemoveRule rule) {
79        rules.add(rule);
80    }
81
82    public static class RemoveRule {
83
84        private Selector selector;
85        private boolean removeOrphans;
86
87        public RemoveRule(Selector selector) {
88            this(selector, false);
89        }
90
91        public RemoveRule(Selector selector, boolean removeOrphans) {
92            this.selector = selector;
93            this.removeOrphans = removeOrphans;
94        }
95
96        public Selector getSelector() {
97            return selector;
98        }
99
100        public boolean getRemoveOrphans() {
101            return removeOrphans;
102        }
103    }
104}
105