public Optional applyGraphFilter()

in gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java [538:600]


        public Optional<StarVertex> applyGraphFilter(final GraphFilter graphFilter) {
            if (!graphFilter.hasFilter())
                return Optional.of(this);
            else if (graphFilter.legalVertex(this)) {
                if (graphFilter.hasEdgeFilter()) {
                    if (graphFilter.checkEdgeLegality(Direction.OUT).negative())
                        this.dropEdges(Direction.OUT);
                    if (graphFilter.checkEdgeLegality(Direction.IN).negative())
                        this.dropEdges(Direction.IN);
                    if (null != this.outEdges)
                        for (final String key : new HashSet<>(this.outEdges.keySet())) {
                            if (graphFilter.checkEdgeLegality(Direction.OUT, key).negative())
                                this.dropEdges(Direction.OUT, key);
                        }
                    if (null != this.inEdges)
                        for (final String key : new HashSet<>(this.inEdges.keySet())) {
                            if (graphFilter.checkEdgeLegality(Direction.IN, key).negative())
                                this.dropEdges(Direction.IN, key);
                        }
                    if (null != this.inEdges || null != this.outEdges) {
                        final Map<String, List<Edge>> outEdges = new HashMap<>();
                        final Map<String, List<Edge>> inEdges = new HashMap<>();
                        graphFilter.legalEdges(this).forEachRemaining(edge -> {
                            if (edge instanceof StarGraph.StarOutEdge) {
                                List<Edge> edges = outEdges.get(edge.label());
                                if (null == edges) {
                                    edges = new ArrayList<>();
                                    outEdges.put(edge.label(), edges);
                                }
                                edges.add(edge);
                            } else {
                                List<Edge> edges = inEdges.get(edge.label());
                                if (null == edges) {
                                    edges = new ArrayList<>();
                                    inEdges.put(edge.label(), edges);
                                }
                                edges.add(edge);
                            }
                        });

                        if (outEdges.isEmpty())
                            this.dropEdges(Direction.OUT);
                        else
                            this.outEdges = outEdges;

                        if (inEdges.isEmpty())
                            this.dropEdges(Direction.IN);
                        else
                            this.inEdges = inEdges;
                    }
                }
                if (graphFilter.hasVertexPropertyFilter()) {
                    Set<String> retainSet = new HashSet<>();
                    graphFilter.legalVertexProperties(this).forEachRemaining(property -> {
                        retainSet.add(property.key());
                    });
                    this.vertexProperties.keySet().removeIf(key -> !retainSet.contains(key));
                }
                return Optional.of(this);
            } else {
                return Optional.empty();
            }
        }