| /* |
| * |
| * Copyright (c) 2011 - 2018 - Loetz GmbH & Co KG, 69115 Heidelberg, Germany |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Initial contribution: |
| * Loetz GmbH & Co. KG |
| * |
| */ |
| package org.eclipse.osbp.xtext.datainterchange.jvmmodel; |
| |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.Map; |
| import java.util.NoSuchElementException; |
| |
| /** |
| * The Class JoinGraph |
| * |
| * @param <J> the generic type |
| */ |
| public class JoinGraph<J> implements Iterable<J> { |
| |
| /** The graph that maps nodes and their edges. */ |
| private final Map<J, Map<J, String>> graph = new HashMap<>(); |
| |
| /** |
| * Adds a node. |
| * |
| * @param node the node |
| * @return true, if successful |
| */ |
| public boolean addNode(J node) { |
| if (node == null) { |
| throw new NullPointerException("node must not be null."); |
| } |
| if (graph.containsKey(node)) { |
| return false; |
| } |
| graph.put(node, new HashMap<J, String>()); |
| return true; |
| } |
| |
| /** |
| * Adds a join. |
| * |
| * @param source the source |
| * @param destination the destination |
| * @param joinName the join name |
| */ |
| public void addJoin (J source, J destination, String joinName) { |
| if (source == null || destination == null) { |
| throw new NullPointerException("source or destination must not be null."); |
| } |
| if (!graph.containsKey(source) || !graph.containsKey(destination)) { |
| throw new NoSuchElementException("source and destination must be part of graph."); |
| } |
| graph.get(source).put(destination, joinName); |
| } |
| |
| /** |
| * Removes a join. |
| * |
| * @param source the source |
| * @param destination the destination |
| */ |
| public void removeJoin (J source, J destination) { |
| if (source == null || destination == null) { |
| throw new NullPointerException("source or destination must not be null."); |
| } |
| if (!graph.containsKey(source) || !graph.containsKey(destination)) { |
| throw new NoSuchElementException("source and destination must be part of graph."); |
| } |
| graph.get(source).remove(destination); |
| } |
| |
| /** |
| * get all joins outgoing from a node. |
| * |
| * @param node the node |
| * @return the list |
| */ |
| public Map<J, String> joinsFrom(J node) { |
| if (node == null) { |
| throw new NullPointerException("node must not be null."); |
| } |
| Map<J, String> joins = graph.get(node); |
| if (joins == null) { |
| throw new NoSuchElementException("source node doesn't exist."); |
| } |
| return Collections.unmodifiableMap(joins); |
| } |
| |
| /* (non-Javadoc) |
| * @see java.lang.Iterable#iterator() |
| */ |
| @Override public Iterator<J> iterator() { |
| return graph.keySet().iterator(); |
| } |
| } |