alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (ImmutableNetwork.java)

This example Java source code file (ImmutableNetwork.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractconfigurablenetwork, deprecated, function, immutablenetwork, map, nodeconnections, object, override, set, util

The ImmutableNetwork.java Java example source code

/*
 * Copyright (C) 2014 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.google.common.graph;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;

import java.util.Map;
import java.util.Set;

/**
 * A {@link Network} whose relationships are constant. Instances of this class may be obtained
 * with {@link #copyOf(Network)}.
 *
 * @author James Sexton
 * @author Joshua O'Madadhain
 * @author Omar Darwish
 * @param <N> Node parameter type
 * @param <E> Edge parameter type
 */
public final class ImmutableNetwork<N, E> extends AbstractConfigurableNetwork {

  private ImmutableNetwork(Network<N, E> graph) {
    super(NetworkBuilder.from(graph), getNodeConnections(graph), getEdgeToReferenceNode(graph));
  }

  /**
   * Returns an immutable copy of {@code graph}.
   */
  public static <N, E> ImmutableNetwork copyOf(Network graph) {
    return (graph instanceof ImmutableNetwork)
        ? (ImmutableNetwork<N, E>) graph
        : new ImmutableNetwork<N, E>(graph);
  }

  /**
   * Simply returns its argument.
   *
   * @deprecated no need to use this
   */
  @Deprecated
  public static <N, E> ImmutableNetwork copyOf(ImmutableNetwork graph) {
    return checkNotNull(graph);
  }

  @Override
  public Set<E> edgesConnecting(Object nodeA, Object nodeB) {
    // This set is calculated as the intersection of two sets, and is likely to be small.
    // As an optimization, copy it to an ImmutableSet so re-iterating is fast.
    return ImmutableSet.copyOf(super.edgesConnecting(nodeA, nodeB));
  }

  private static <N, E> Map> getNodeConnections(Network 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<N, NodeConnections nodeConnections = ImmutableMap.builder();
    for (N node : graph.nodes()) {
      nodeConnections.put(node, nodeConnectionsOf(graph, node));
    }
    return nodeConnections.build();
  }

  private static <N, E> Map getEdgeToReferenceNode(Network graph) {
    // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will
    // have whatever ordering the graph's edges do, so ImmutableSortedMap is unnecessary even if the
    // input edges are sorted.
    ImmutableMap.Builder<E, N> edgeToReferenceNode = ImmutableMap.builder();
    for (E edge : graph.edges()) {
      edgeToReferenceNode.put(edge, graph.incidentNodes(edge).nodeA());
    }
    return edgeToReferenceNode.build();
  }

  private static <N, E> NodeConnections nodeConnectionsOf(Network graph, N node) {
    if (graph.isDirected()) {
      Map<E, N> inEdgeMap = Maps.asMap(graph.inEdges(node), sourceNodeFn(graph));
      Map<E, N> outEdgeMap = Maps.asMap(graph.outEdges(node), targetNodeFn(graph));
      return graph.allowsParallelEdges()
           ? DirectedMultiNodeConnections.ofImmutable(inEdgeMap, outEdgeMap)
           : DirectedNodeConnections.ofImmutable(inEdgeMap, outEdgeMap);
    } else {
      Map<E, N> incidentEdgeMap =
          Maps.asMap(graph.incidentEdges(node), oppositeNodeFn(graph, node));
      return graph.allowsParallelEdges()
          ? UndirectedMultiNodeConnections.ofImmutable(incidentEdgeMap)
          : UndirectedNodeConnections.ofImmutable(incidentEdgeMap);
    }
  }

  private static <N, E> Function sourceNodeFn(final Network graph) {
    return new Function<E, N>() {
      @Override
      public N apply(E edge) {
        return graph.incidentNodes(edge).source();
      }
    };
  }

  private static <N, E> Function targetNodeFn(final Network graph) {
    return new Function<E, N>() {
      @Override
      public N apply(E edge) {
        return graph.incidentNodes(edge).target();
      }
    };
  }

  private static <N, E> Function oppositeNodeFn(final Network graph, final N node) {
    return new Function<E, N>() {
      @Override
      public N apply(E edge) {
        return Graphs.oppositeNode(graph, edge, node);
      }
    };
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java ImmutableNetwork.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.