com.hazelcast.org.apache.calcite.util.graph.AttributedDirectedGraph Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.org.apache.calcite.util.graph;
import com.hazelcast.org.apache.calcite.util.Util;
import com.hazelcast.org.checkerframework.checker.initialization.qual.UnknownInitialization;
import com.hazelcast.org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
/**
* Directed graph where edges have attributes and allows multiple edges between
* any two vertices provided that their attributes are different.
*
* @param Vertex type
* @param Edge type
*/
public class AttributedDirectedGraph
extends DefaultDirectedGraph {
/** Creates an attributed graph. */
public AttributedDirectedGraph(@UnknownInitialization AttributedEdgeFactory edgeFactory) {
super(edgeFactory);
}
public static AttributedDirectedGraph create(
AttributedEdgeFactory edgeFactory) {
return new AttributedDirectedGraph<>(edgeFactory);
}
/** Returns the first edge between one vertex to another. */
@Override public @Nullable E getEdge(V source, V target) {
final VertexInfo info = getVertex(source);
for (E outEdge : info.outEdges) {
if (outEdge.target.equals(target)) {
return outEdge;
}
}
return null;
}
// CHECKSTYLE: IGNORE 1
/** @deprecated Use {@link #addEdge(Object, Object, Object...)}. */
@Deprecated
@Override public @Nullable E addEdge(V vertex, V targetVertex) {
return super.addEdge(vertex, targetVertex);
}
public @Nullable E addEdge(V vertex, V targetVertex, Object... attributes) {
final VertexInfo info = getVertex(vertex);
final VertexInfo targetInfo = getVertex(targetVertex);
@SuppressWarnings("unchecked")
final AttributedEdgeFactory f =
(AttributedEdgeFactory) this.edgeFactory;
final E edge = f.createEdge(vertex, targetVertex, attributes);
if (edges.add(edge)) {
info.outEdges.add(edge);
targetInfo.inEdges.add(edge);
return edge;
} else {
return null;
}
}
/** Returns all edges between one vertex to another. */
public Iterable getEdges(V source, final V target) {
final VertexInfo info = getVertex(source);
return Util.filter(info.outEdges, outEdge -> outEdge.target.equals(target));
}
/** Removes all edges from a given vertex to another.
* Returns whether any were removed. */
@Override public boolean removeEdge(V source, V target) {
// remove out edges
final List outEdges = getVertex(source).outEdges;
int removeOutCount = 0;
for (int i = 0, size = outEdges.size(); i < size; i++) {
E edge = outEdges.get(i);
if (edge.target.equals(target)) {
outEdges.remove(i);
edges.remove(edge);
++removeOutCount;
}
}
// remove in edges
final List inEdges = getVertex(target).inEdges;
int removeInCount = 0;
for (int i = 0, size = inEdges.size(); i < size; i++) {
E edge = inEdges.get(i);
if (edge.source.equals(source)) {
inEdges.remove(i);
++removeInCount;
}
}
assert removeOutCount == removeInCount;
return removeOutCount > 0;
}
/** Factory for edges that have attributes.
*
* @param Vertex type
* @param Edge type
*/
public interface AttributedEdgeFactory extends EdgeFactory {
E createEdge(V v0, V v1, Object... attributes);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy