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

com.facebook.presto.jdbc.internal.guava.graph.ImmutableValueGraph Maven / Gradle / Ivy

There is a newer version: 0.289
Show newest version
/*
 * Copyright (C) 2016 The Guava Authors
 *
 * Licensed 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.facebook.presto.jdbc.internal.guava.graph;

import static com.facebook.presto.jdbc.internal.guava.base.Preconditions.checkNotNull;

import com.facebook.presto.jdbc.internal.guava.annotations.Beta;
import com.facebook.presto.jdbc.internal.guava.base.Function;
import com.facebook.presto.jdbc.internal.guava.collect.ImmutableMap;
import com.facebook.presto.jdbc.internal.guava.collect.Maps;
import com.google.errorprone.annotations.Immutable;

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

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 * @param Node parameter type * @param Value parameter type * @since 20.0 */ @Beta @Immutable(containerOf = {"N", "V"}) @SuppressWarnings("Immutable") // Extends ConfigurableValueGraph but uses ImmutableMaps. public final class ImmutableValueGraph extends ConfigurableValueGraph { private ImmutableValueGraph(ValueGraph graph) { super(ValueGraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size()); } /** Returns an immutable copy of {@code graph}. */ public static ImmutableValueGraph copyOf(ValueGraph graph) { return (graph instanceof ImmutableValueGraph) ? (ImmutableValueGraph) graph : new ImmutableValueGraph(graph); } /** * Simply returns its argument. * * @deprecated no need to use this */ @Deprecated public static ImmutableValueGraph copyOf(ImmutableValueGraph graph) { return checkNotNull(graph); } @Override public ImmutableGraph asGraph() { return new ImmutableGraph(this); // safe because the view is effectively immutable } private static ImmutableMap> getNodeConnections( ValueGraph 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( final ValueGraph graph, final N node) { Function successorNodeToValueFn = new Function() { @Override public V apply(N successorNode) { return graph.edgeValueOrDefault(node, successorNode, null); } }; return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), successorNodeToValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), successorNodeToValueFn)); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy