All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.topicus.jdbc.shaded.com.google.common.graph.ImmutableGraph Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
/*
 * Copyright (C) 2014 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in nl.topicus.jdbc.shaded.com.liance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.nl.topicus.jdbc.shaded.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 nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.graph;

import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Preconditions.checkNotNull;

import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.annotations.Beta;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Function;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Functions;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.collect.ImmutableMap;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.collect.Maps;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.graph.GraphConstants.Presence;
import nl.topicus.jdbc.shaded.com.google.errorprone.annotations.Immutable;

/**
 * A {@link Graph} whose elements and structural relationships will never change. Instances of this
 * class may be obtained with {@link #copyOf(Graph)}.
 *
 * 

See the Guava User's Guide's discussion * of the {@code Immutable*} types for more information on the properties and guarantees * provided by this class. * * @author James Sexton * @author Joshua O'Madadhain * @author Omar Darwish * @param Node parameter type * @since 20.0 */ @Beta public abstract class ImmutableGraph extends ForwardingGraph { /** To ensure the immutability contract is maintained, there must be no public constructors. */ ImmutableGraph() {} /** Returns an immutable copy of {@code graph}. */ public static ImmutableGraph copyOf(Graph graph) { return (graph instanceof ImmutableGraph) ? (ImmutableGraph) graph : new ValueBackedImpl( GraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size()); } /** * Simply returns its argument. * * @deprecated no need to use this */ @Deprecated public static ImmutableGraph copyOf(ImmutableGraph graph) { return checkNotNull(graph); } private static ImmutableMap> getNodeConnections( Graph graph) { // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have // whatever ordering the graph's nodes do, so ImmutableSortedMap is unnecessary even if the // input nodes are sorted. ImmutableMap.Builder> nodeConnections = ImmutableMap.builder(); for (N node : graph.nodes()) { nodeConnections.put(node, connectionsOf(graph, node)); } return nodeConnections.build(); } private static GraphConnections connectionsOf(Graph graph, N node) { Function edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); } static class ValueBackedImpl extends ImmutableGraph { protected final ValueGraph backingValueGraph; ValueBackedImpl( AbstractGraphBuilder builder, ImmutableMap> nodeConnections, long edgeCount) { this.backingValueGraph = new ConfigurableValueGraph(builder, nodeConnections, edgeCount); } @Override protected Graph delegate() { return backingValueGraph; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy