forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-722 Intro to JSONassert (eugenp#1437)
* [email protected] Evaluation article on Different Types of Bean Injection in Spring * Revert "[email protected]" This reverts commit 963cc51. * Fixing compilation error and removing unused import * Introduction to Java9 StackWalking API - [email protected] Code examples for the article "Introduction to Java9 StackWalking API" * BAEL-608 Introduction to Java9 StackWalking API * BAEL-608 Introduction to Java9 StackWalking API changing the test names to BDD style * BAEL-608 Introduction to Java9 StackWalking API correcting the typo * BAEL-608 Introduction to Java9 StackWalking API improving method names * BAEL-608 Introduction to Java9 StackWalking API test method names improvements * BAEL-718 Quick intro to javatuples * merging pom from master * BAEL-722 Intro to JSONassert
- Loading branch information
Showing
27 changed files
with
753 additions
and
632 deletions.
There are no files selected for viewing
114 changes: 57 additions & 57 deletions
114
algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Dijkstra.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
package com.baeldung.algorithms.ga.dijkstra; | ||
|
||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
|
||
public class Dijkstra { | ||
|
||
public static Graph calculateShortestPathFromSource(Graph graph, Node source) { | ||
|
||
source.setDistance(0); | ||
|
||
Set<Node> settledNodes = new HashSet<>(); | ||
Set<Node> unsettledNodes = new HashSet<>(); | ||
unsettledNodes.add(source); | ||
|
||
while (unsettledNodes.size() != 0) { | ||
Node currentNode = getLowestDistanceNode(unsettledNodes); | ||
unsettledNodes.remove(currentNode); | ||
for (Entry<Node, Integer> adjacencyPair : currentNode.getAdjacentNodes().entrySet()) { | ||
Node adjacentNode = adjacencyPair.getKey(); | ||
Integer edgeWeigh = adjacencyPair.getValue(); | ||
|
||
if (!settledNodes.contains(adjacentNode)) { | ||
CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode); | ||
unsettledNodes.add(adjacentNode); | ||
} | ||
} | ||
settledNodes.add(currentNode); | ||
} | ||
return graph; | ||
} | ||
|
||
private static void CalculateMinimumDistance(Node evaluationNode, Integer edgeWeigh, Node sourceNode) { | ||
Integer sourceDistance = sourceNode.getDistance(); | ||
if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) { | ||
evaluationNode.setDistance(sourceDistance + edgeWeigh); | ||
LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath()); | ||
shortestPath.add(sourceNode); | ||
evaluationNode.setShortestPath(shortestPath); | ||
} | ||
} | ||
|
||
private static Node getLowestDistanceNode(Set<Node> unsettledNodes) { | ||
Node lowestDistanceNode = null; | ||
int lowestDistance = Integer.MAX_VALUE; | ||
for (Node node : unsettledNodes) { | ||
int nodeDistance = node.getDistance(); | ||
if (nodeDistance < lowestDistance) { | ||
lowestDistance = nodeDistance; | ||
lowestDistanceNode = node; | ||
} | ||
} | ||
return lowestDistanceNode; | ||
} | ||
} | ||
package com.baeldung.algorithms.ga.dijkstra; | ||
|
||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
|
||
public class Dijkstra { | ||
|
||
public static Graph calculateShortestPathFromSource(Graph graph, Node source) { | ||
|
||
source.setDistance(0); | ||
|
||
Set<Node> settledNodes = new HashSet<>(); | ||
Set<Node> unsettledNodes = new HashSet<>(); | ||
unsettledNodes.add(source); | ||
|
||
while (unsettledNodes.size() != 0) { | ||
Node currentNode = getLowestDistanceNode(unsettledNodes); | ||
unsettledNodes.remove(currentNode); | ||
for (Entry<Node, Integer> adjacencyPair : currentNode.getAdjacentNodes().entrySet()) { | ||
Node adjacentNode = adjacencyPair.getKey(); | ||
Integer edgeWeigh = adjacencyPair.getValue(); | ||
|
||
if (!settledNodes.contains(adjacentNode)) { | ||
CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode); | ||
unsettledNodes.add(adjacentNode); | ||
} | ||
} | ||
settledNodes.add(currentNode); | ||
} | ||
return graph; | ||
} | ||
|
||
private static void CalculateMinimumDistance(Node evaluationNode, Integer edgeWeigh, Node sourceNode) { | ||
Integer sourceDistance = sourceNode.getDistance(); | ||
if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) { | ||
evaluationNode.setDistance(sourceDistance + edgeWeigh); | ||
LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath()); | ||
shortestPath.add(sourceNode); | ||
evaluationNode.setShortestPath(shortestPath); | ||
} | ||
} | ||
|
||
private static Node getLowestDistanceNode(Set<Node> unsettledNodes) { | ||
Node lowestDistanceNode = null; | ||
int lowestDistance = Integer.MAX_VALUE; | ||
for (Node node : unsettledNodes) { | ||
int nodeDistance = node.getDistance(); | ||
if (nodeDistance < lowestDistance) { | ||
lowestDistance = nodeDistance; | ||
lowestDistanceNode = node; | ||
} | ||
} | ||
return lowestDistanceNode; | ||
} | ||
} |
42 changes: 21 additions & 21 deletions
42
algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Graph.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
package com.baeldung.algorithms.ga.dijkstra; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class Graph { | ||
|
||
private Set<Node> nodes = new HashSet<>(); | ||
|
||
public void addNode(Node nodeA) { | ||
nodes.add(nodeA); | ||
} | ||
|
||
public Set<Node> getNodes() { | ||
return nodes; | ||
} | ||
|
||
public void setNodes(Set<Node> nodes) { | ||
this.nodes = nodes; | ||
} | ||
} | ||
package com.baeldung.algorithms.ga.dijkstra; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class Graph { | ||
|
||
private Set<Node> nodes = new HashSet<>(); | ||
|
||
public void addNode(Node nodeA) { | ||
nodes.add(nodeA); | ||
} | ||
|
||
public Set<Node> getNodes() { | ||
return nodes; | ||
} | ||
|
||
public void setNodes(Set<Node> nodes) { | ||
this.nodes = nodes; | ||
} | ||
} |
116 changes: 58 additions & 58 deletions
116
algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Node.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,58 @@ | ||
package com.baeldung.algorithms.ga.dijkstra; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Node { | ||
|
||
private String name; | ||
|
||
private LinkedList<Node> shortestPath = new LinkedList<>(); | ||
|
||
private Integer distance = Integer.MAX_VALUE; | ||
|
||
private Map<Node, Integer> adjacentNodes = new HashMap<>(); | ||
|
||
public Node(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void addDestination(Node destination, int distance) { | ||
adjacentNodes.put(destination, distance); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Map<Node, Integer> getAdjacentNodes() { | ||
return adjacentNodes; | ||
} | ||
|
||
public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) { | ||
this.adjacentNodes = adjacentNodes; | ||
} | ||
|
||
public Integer getDistance() { | ||
return distance; | ||
} | ||
|
||
public void setDistance(Integer distance) { | ||
this.distance = distance; | ||
} | ||
|
||
public List<Node> getShortestPath() { | ||
return shortestPath; | ||
} | ||
|
||
public void setShortestPath(LinkedList<Node> shortestPath) { | ||
this.shortestPath = shortestPath; | ||
} | ||
|
||
} | ||
package com.baeldung.algorithms.ga.dijkstra; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Node { | ||
|
||
private String name; | ||
|
||
private LinkedList<Node> shortestPath = new LinkedList<>(); | ||
|
||
private Integer distance = Integer.MAX_VALUE; | ||
|
||
private Map<Node, Integer> adjacentNodes = new HashMap<>(); | ||
|
||
public Node(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void addDestination(Node destination, int distance) { | ||
adjacentNodes.put(destination, distance); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Map<Node, Integer> getAdjacentNodes() { | ||
return adjacentNodes; | ||
} | ||
|
||
public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) { | ||
this.adjacentNodes = adjacentNodes; | ||
} | ||
|
||
public Integer getDistance() { | ||
return distance; | ||
} | ||
|
||
public void setDistance(Integer distance) { | ||
this.distance = distance; | ||
} | ||
|
||
public List<Node> getShortestPath() { | ||
return shortestPath; | ||
} | ||
|
||
public void setShortestPath(LinkedList<Node> shortestPath) { | ||
this.shortestPath = shortestPath; | ||
} | ||
|
||
} |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
|
||
package com.baeldung.examples; | ||
|
||
import com.baeldung.examples.guice.Communication; | ||
import com.baeldung.examples.guice.binding.AOPModule; | ||
import com.baeldung.examples.guice.modules.BasicModule; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* | ||
* @author baeldung | ||
*/ | ||
public class RunGuice { | ||
|
||
public static void main(String[] args) { | ||
Injector injector = Guice.createInjector(new BasicModule(), new AOPModule()); | ||
Communication comms = injector.getInstance(Communication.class); | ||
Scanner scanner = new Scanner(System.in); | ||
while (true) { | ||
String input = scanner.nextLine(); | ||
if (input.equalsIgnoreCase("q")) { | ||
System.exit(0); | ||
} else { | ||
comms.sendMessage(input); | ||
} | ||
|
||
} | ||
|
||
} | ||
} | ||
|
||
package com.baeldung.examples; | ||
|
||
import com.baeldung.examples.guice.Communication; | ||
import com.baeldung.examples.guice.binding.AOPModule; | ||
import com.baeldung.examples.guice.modules.BasicModule; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* | ||
* @author baeldung | ||
*/ | ||
public class RunGuice { | ||
|
||
public static void main(String[] args) { | ||
Injector injector = Guice.createInjector(new BasicModule(), new AOPModule()); | ||
Communication comms = injector.getInstance(Communication.class); | ||
Scanner scanner = new Scanner(System.in); | ||
while (true) { | ||
String input = scanner.nextLine(); | ||
if (input.equalsIgnoreCase("q")) { | ||
System.exit(0); | ||
} else { | ||
comms.sendMessage(input); | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
Oops, something went wrong.