diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Dijkstra.java b/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Dijkstra.java index 046f13983d..0b01e9b48b 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Dijkstra.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Dijkstra.java @@ -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 settledNodes = new HashSet<>(); - Set unsettledNodes = new HashSet<>(); - unsettledNodes.add(source); - - while (unsettledNodes.size() != 0) { - Node currentNode = getLowestDistanceNode(unsettledNodes); - unsettledNodes.remove(currentNode); - for (Entry 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 shortestPath = new LinkedList<>(sourceNode.getShortestPath()); - shortestPath.add(sourceNode); - evaluationNode.setShortestPath(shortestPath); - } - } - - private static Node getLowestDistanceNode(Set 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 settledNodes = new HashSet<>(); + Set unsettledNodes = new HashSet<>(); + unsettledNodes.add(source); + + while (unsettledNodes.size() != 0) { + Node currentNode = getLowestDistanceNode(unsettledNodes); + unsettledNodes.remove(currentNode); + for (Entry 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 shortestPath = new LinkedList<>(sourceNode.getShortestPath()); + shortestPath.add(sourceNode); + evaluationNode.setShortestPath(shortestPath); + } + } + + private static Node getLowestDistanceNode(Set 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; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Graph.java b/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Graph.java index 00db4b01e4..76694ed76e 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Graph.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Graph.java @@ -1,21 +1,21 @@ -package com.baeldung.algorithms.ga.dijkstra; - -import java.util.HashSet; -import java.util.Set; - -public class Graph { - - private Set nodes = new HashSet<>(); - - public void addNode(Node nodeA) { - nodes.add(nodeA); - } - - public Set getNodes() { - return nodes; - } - - public void setNodes(Set nodes) { - this.nodes = nodes; - } -} +package com.baeldung.algorithms.ga.dijkstra; + +import java.util.HashSet; +import java.util.Set; + +public class Graph { + + private Set nodes = new HashSet<>(); + + public void addNode(Node nodeA) { + nodes.add(nodeA); + } + + public Set getNodes() { + return nodes; + } + + public void setNodes(Set nodes) { + this.nodes = nodes; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Node.java b/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Node.java index 256f12e204..ac34bfadd1 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Node.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Node.java @@ -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 shortestPath = new LinkedList<>(); - - private Integer distance = Integer.MAX_VALUE; - - private Map 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 getAdjacentNodes() { - return adjacentNodes; - } - - public void setAdjacentNodes(Map adjacentNodes) { - this.adjacentNodes = adjacentNodes; - } - - public Integer getDistance() { - return distance; - } - - public void setDistance(Integer distance) { - this.distance = distance; - } - - public List getShortestPath() { - return shortestPath; - } - - public void setShortestPath(LinkedList 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 shortestPath = new LinkedList<>(); + + private Integer distance = Integer.MAX_VALUE; + + private Map 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 getAdjacentNodes() { + return adjacentNodes; + } + + public void setAdjacentNodes(Map adjacentNodes) { + this.adjacentNodes = adjacentNodes; + } + + public Integer getDistance() { + return distance; + } + + public void setDistance(Integer distance) { + this.distance = distance; + } + + public List getShortestPath() { + return shortestPath; + } + + public void setShortestPath(LinkedList shortestPath) { + this.shortestPath = shortestPath; + } + +} diff --git a/core-java/0.004102810554955205 b/core-java/0.004102810554955205 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.04832801936270381 b/core-java/0.04832801936270381 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.5633433244738808 b/core-java/0.5633433244738808 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.6256429734439612 b/core-java/0.6256429734439612 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.8260098203820962 b/core-java/0.8260098203820962 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.9799201796740292 b/core-java/0.9799201796740292 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/guice/src/main/java/com/baeldung/examples/RunGuice.java b/guice/src/main/java/com/baeldung/examples/RunGuice.java index a07447cde8..660952f325 100644 --- a/guice/src/main/java/com/baeldung/examples/RunGuice.java +++ b/guice/src/main/java/com/baeldung/examples/RunGuice.java @@ -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); + } + + } + + } +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/Communication.java b/guice/src/main/java/com/baeldung/examples/guice/Communication.java index 7f7cb822d8..464e0c641d 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/Communication.java +++ b/guice/src/main/java/com/baeldung/examples/guice/Communication.java @@ -1,40 +1,40 @@ - -package com.baeldung.examples.guice; - -import com.google.inject.Inject; -import com.google.inject.name.Named; -import java.util.Date; -import java.util.LinkedList; -import java.util.Queue; -import java.util.logging.Logger; - -/** - * - * @author baeldung - */ -public class Communication { - - final Date start = new Date(); - - @Inject - private Logger logger; - - @Inject - private DefaultCommunicator communicator; - - public Communication(Boolean keepRecords) { - if (keepRecords) { - System.out.println("keeping records"); - } - } - - public boolean sendMessage(String message) { - - return communicator.sendMessage(message); - } - - public DefaultCommunicator getCommunicator() { - return this.communicator; - } - -} + +package com.baeldung.examples.guice; + +import com.google.inject.Inject; +import com.google.inject.name.Named; +import java.util.Date; +import java.util.LinkedList; +import java.util.Queue; +import java.util.logging.Logger; + +/** + * + * @author baeldung + */ +public class Communication { + + final Date start = new Date(); + + @Inject + private Logger logger; + + @Inject + private DefaultCommunicator communicator; + + public Communication(Boolean keepRecords) { + if (keepRecords) { + System.out.println("keeping records"); + } + } + + public boolean sendMessage(String message) { + + return communicator.sendMessage(message); + } + + public DefaultCommunicator getCommunicator() { + return this.communicator; + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/CommunicationMode.java b/guice/src/main/java/com/baeldung/examples/guice/CommunicationMode.java index 444b775478..7a36f0c276 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/CommunicationMode.java +++ b/guice/src/main/java/com/baeldung/examples/guice/CommunicationMode.java @@ -1,12 +1,12 @@ - -package com.baeldung.examples.guice; - -import com.baeldung.examples.guice.constant.CommunicationModel; - -public interface CommunicationMode { - - public CommunicationModel getMode(); - - public boolean sendMessage(String message); - -} + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.constant.CommunicationModel; + +public interface CommunicationMode { + + public CommunicationModel getMode(); + + public boolean sendMessage(String message); + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java b/guice/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java index c65644646a..24e0c28dd1 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java +++ b/guice/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java @@ -1,51 +1,51 @@ - -package com.baeldung.examples.guice; - -import com.baeldung.examples.guice.marker.Communicator; -import com.google.inject.Inject; -import com.google.inject.name.Named; - -/** - * - * @author baeldung - */ -public class DefaultCommunicator implements Communicator { - - private CommunicationMode defaultCommsMode; - @Inject - @Named("SMSComms") - CommunicationMode smsCommsMode; - @Inject - @Named("EmailComms") - CommunicationMode emailCommsMode; - @Inject - @Named("IMComms") - CommunicationMode imCommsMode; - - protected DefaultCommunicator(CommunicationMode defaultComms) { - this.defaultCommsMode = defaultComms; - } - - public DefaultCommunicator() { - - } - - public boolean sendMessage(String message) { - boolean sent = false; - if (defaultCommsMode != null) { - sent = sendMessageByDefault(message); - } else { - sent = smsCommsMode.sendMessage(message); - } - return sent; - } - - private boolean sendMessageByDefault(String message) { - boolean sent = false; - if (message != null && !message.trim().equals("")) { - return defaultCommsMode.sendMessage(message); - } - return sent; - } - -} + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.marker.Communicator; +import com.google.inject.Inject; +import com.google.inject.name.Named; + +/** + * + * @author baeldung + */ +public class DefaultCommunicator implements Communicator { + + private CommunicationMode defaultCommsMode; + @Inject + @Named("SMSComms") + CommunicationMode smsCommsMode; + @Inject + @Named("EmailComms") + CommunicationMode emailCommsMode; + @Inject + @Named("IMComms") + CommunicationMode imCommsMode; + + protected DefaultCommunicator(CommunicationMode defaultComms) { + this.defaultCommsMode = defaultComms; + } + + public DefaultCommunicator() { + + } + + public boolean sendMessage(String message) { + boolean sent = false; + if (defaultCommsMode != null) { + sent = sendMessageByDefault(message); + } else { + sent = smsCommsMode.sendMessage(message); + } + return sent; + } + + private boolean sendMessageByDefault(String message) { + boolean sent = false; + if (message != null && !message.trim().equals("")) { + return defaultCommsMode.sendMessage(message); + } + return sent; + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java b/guice/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java index 3caca0edcc..06e77a58e2 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java +++ b/guice/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java @@ -1,24 +1,24 @@ - -package com.baeldung.examples.guice; - -import com.baeldung.examples.guice.aop.MessageSentLoggable; -import com.baeldung.examples.guice.constant.CommunicationModel; - -/** - * - * @author baeldung - */ -public class EmailCommunicationMode implements CommunicationMode { - - @Override - public CommunicationModel getMode() { - return CommunicationModel.EMAIL; - } - - @Override - @MessageSentLoggable - public boolean sendMessage(String Message) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - -} + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.baeldung.examples.guice.constant.CommunicationModel; + +/** + * + * @author baeldung + */ +public class EmailCommunicationMode implements CommunicationMode { + + @Override + public CommunicationModel getMode() { + return CommunicationModel.EMAIL; + } + + @Override + @MessageSentLoggable + public boolean sendMessage(String Message) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java b/guice/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java index bc9bd61449..42b0c82b90 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java +++ b/guice/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java @@ -1,30 +1,30 @@ - -package com.baeldung.examples.guice; - -import com.baeldung.examples.guice.aop.MessageSentLoggable; -import com.baeldung.examples.guice.constant.CommunicationModel; -import com.google.inject.Inject; -import java.util.logging.Logger; - -/** - * - * @author baeldung - */ -public class IMCommunicationMode implements CommunicationMode { - - @Inject - private Logger logger; - - @Override - public CommunicationModel getMode() { - return CommunicationModel.IM; - } - - @Override - @MessageSentLoggable - public boolean sendMessage(String message) { - logger.info("IM Message Sent"); - return true; - } - -} + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.baeldung.examples.guice.constant.CommunicationModel; +import com.google.inject.Inject; +import java.util.logging.Logger; + +/** + * + * @author baeldung + */ +public class IMCommunicationMode implements CommunicationMode { + + @Inject + private Logger logger; + + @Override + public CommunicationModel getMode() { + return CommunicationModel.IM; + } + + @Override + @MessageSentLoggable + public boolean sendMessage(String message) { + logger.info("IM Message Sent"); + return true; + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java b/guice/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java index 28475839dd..7a30e51f10 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java +++ b/guice/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java @@ -1,30 +1,30 @@ - -package com.baeldung.examples.guice; - -import com.baeldung.examples.guice.aop.MessageSentLoggable; -import com.baeldung.examples.guice.constant.CommunicationModel; -import com.google.inject.Inject; -import java.util.logging.Logger; - -/** - * - * @author baeldung - */ -public class SMSCommunicationMode implements CommunicationMode { - - @Inject - private Logger logger; - - @Override - public CommunicationModel getMode() { - return CommunicationModel.SMS; - } - - @Override - @MessageSentLoggable - public boolean sendMessage(String message) { - logger.info("SMS message sent"); - return true; - } - -} + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.baeldung.examples.guice.constant.CommunicationModel; +import com.google.inject.Inject; +import java.util.logging.Logger; + +/** + * + * @author baeldung + */ +public class SMSCommunicationMode implements CommunicationMode { + + @Inject + private Logger logger; + + @Override + public CommunicationModel getMode() { + return CommunicationModel.SMS; + } + + @Override + @MessageSentLoggable + public boolean sendMessage(String message) { + logger.info("SMS message sent"); + return true; + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java b/guice/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java index 2ad5f8b92e..379cd5f18b 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java +++ b/guice/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java @@ -1,24 +1,24 @@ - -package com.baeldung.examples.guice.aop; - -import com.google.inject.Inject; -import java.util.logging.Logger; -import org.aopalliance.intercept.MethodInterceptor; -import org.aopalliance.intercept.MethodInvocation; - -/** - * - * @author baeldung - */ -public class MessageLogger implements MethodInterceptor { - - @Override - public Object invoke(MethodInvocation invocation) throws Throwable { - Object[] objectArray = invocation.getArguments(); - int i = 0; - for (Object object : objectArray) { - Logger.getAnonymousLogger().info("Sending message: " + object.toString()); - } - return invocation.proceed(); - } -} + +package com.baeldung.examples.guice.aop; + +import com.google.inject.Inject; +import java.util.logging.Logger; +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +/** + * + * @author baeldung + */ +public class MessageLogger implements MethodInterceptor { + + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + Object[] objectArray = invocation.getArguments(); + int i = 0; + for (Object object : objectArray) { + Logger.getAnonymousLogger().info("Sending message: " + object.toString()); + } + return invocation.proceed(); + } +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java b/guice/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java index 5e5a411d0e..431c4bc0ce 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java +++ b/guice/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java @@ -1,17 +1,17 @@ - -package com.baeldung.examples.guice.aop; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * - * @author baeldung - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface MessageSentLoggable { - -} + +package com.baeldung.examples.guice.aop; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author baeldung + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface MessageSentLoggable { + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java b/guice/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java index 109d9a6389..b41dcf16e5 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java +++ b/guice/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java @@ -1,23 +1,23 @@ - -package com.baeldung.examples.guice.binding; - -import com.baeldung.examples.guice.aop.MessageLogger; -import com.baeldung.examples.guice.aop.MessageSentLoggable; -import com.google.inject.AbstractModule; -import com.google.inject.matcher.Matchers; - -/** - * - * @author baeldung - */ -public class AOPModule extends AbstractModule { - - @Override - protected void configure() { - bindInterceptor(Matchers.any(), - Matchers.annotatedWith(MessageSentLoggable.class), - new MessageLogger() - ); - } - -} + +package com.baeldung.examples.guice.binding; + +import com.baeldung.examples.guice.aop.MessageLogger; +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.google.inject.AbstractModule; +import com.google.inject.matcher.Matchers; + +/** + * + * @author baeldung + */ +public class AOPModule extends AbstractModule { + + @Override + protected void configure() { + bindInterceptor(Matchers.any(), + Matchers.annotatedWith(MessageSentLoggable.class), + new MessageLogger() + ); + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java b/guice/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java index 93f0fe54ba..1cd9d624ab 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java +++ b/guice/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java @@ -1,37 +1,37 @@ - -package com.baeldung.examples.guice.binding; - -import com.baeldung.examples.guice.Communication; -import com.baeldung.examples.guice.CommunicationMode; -import com.baeldung.examples.guice.DefaultCommunicator; -import com.baeldung.examples.guice.EmailCommunicationMode; -import com.baeldung.examples.guice.IMCommunicationMode; -import com.baeldung.examples.guice.SMSCommunicationMode; -import com.google.inject.AbstractModule; -import com.google.inject.name.Names; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * - * @author baeldung - */ -public class BasicModule extends AbstractModule { - - @Override - protected void configure() { - try { - bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.TYPE)); - } catch (NoSuchMethodException ex) { - Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); - } catch (SecurityException ex) { - Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); - } - bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); - - bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); - bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); - bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); - } - -} + +package com.baeldung.examples.guice.binding; + +import com.baeldung.examples.guice.Communication; +import com.baeldung.examples.guice.CommunicationMode; +import com.baeldung.examples.guice.DefaultCommunicator; +import com.baeldung.examples.guice.EmailCommunicationMode; +import com.baeldung.examples.guice.IMCommunicationMode; +import com.baeldung.examples.guice.SMSCommunicationMode; +import com.google.inject.AbstractModule; +import com.google.inject.name.Names; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author baeldung + */ +public class BasicModule extends AbstractModule { + + @Override + protected void configure() { + try { + bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.TYPE)); + } catch (NoSuchMethodException ex) { + Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } catch (SecurityException ex) { + Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } + bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); + + bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java b/guice/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java index d12420a0db..3483e9cefd 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java +++ b/guice/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java @@ -1,17 +1,17 @@ - -package com.baeldung.examples.guice.constant; - -/** - * - * @author baeldung - */ -public enum CommunicationModel { - - EMAIL("Email"), SMS("SMS"), IM("IM"), PHONE("Phone"); - - final String name; - - CommunicationModel(String name) { - this.name = name; - } -} + +package com.baeldung.examples.guice.constant; + +/** + * + * @author baeldung + */ +public enum CommunicationModel { + + EMAIL("Email"), SMS("SMS"), IM("IM"), PHONE("Phone"); + + final String name; + + CommunicationModel(String name) { + this.name = name; + } +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/marker/Communicator.java b/guice/src/main/java/com/baeldung/examples/guice/marker/Communicator.java index 7425f1c283..45c729a9a3 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/marker/Communicator.java +++ b/guice/src/main/java/com/baeldung/examples/guice/marker/Communicator.java @@ -1,14 +1,14 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package com.baeldung.examples.guice.marker; - -/** - * - * @author Tayo - */ -public interface Communicator { - -} +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.examples.guice.marker; + +/** + * + * @author Tayo + */ +public interface Communicator { + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java b/guice/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java index ed83cf3649..f27d8b3a53 100644 --- a/guice/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java +++ b/guice/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java @@ -1,38 +1,38 @@ - -package com.baeldung.examples.guice.modules; - -import com.baeldung.examples.guice.Communication; -import com.baeldung.examples.guice.CommunicationMode; -import com.baeldung.examples.guice.DefaultCommunicator; -import com.baeldung.examples.guice.EmailCommunicationMode; -import com.baeldung.examples.guice.IMCommunicationMode; -import com.baeldung.examples.guice.SMSCommunicationMode; -import com.google.inject.AbstractModule; -import com.google.inject.name.Names; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * - * @author baeldung - */ -public class BasicModule extends AbstractModule { - - @Override - protected void configure() { - try { - bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.class)); - bind(Boolean.class).toInstance(true); - } catch (NoSuchMethodException ex) { - Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); - } catch (SecurityException ex) { - Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); - } - bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); - - bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); - bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); - bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); - } - -} + +package com.baeldung.examples.guice.modules; + +import com.baeldung.examples.guice.Communication; +import com.baeldung.examples.guice.CommunicationMode; +import com.baeldung.examples.guice.DefaultCommunicator; +import com.baeldung.examples.guice.EmailCommunicationMode; +import com.baeldung.examples.guice.IMCommunicationMode; +import com.baeldung.examples.guice.SMSCommunicationMode; +import com.google.inject.AbstractModule; +import com.google.inject.name.Names; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author baeldung + */ +public class BasicModule extends AbstractModule { + + @Override + protected void configure() { + try { + bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.class)); + bind(Boolean.class).toInstance(true); + } catch (NoSuchMethodException ex) { + Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } catch (SecurityException ex) { + Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } + bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); + + bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); + } + +} diff --git a/jee7/src/main/java/com/baeldung/javaeeannotations/LogInFilter.java b/jee7/src/main/java/com/baeldung/javaeeannotations/LogInFilter.java index 9f1345139d..4e4aef2672 100644 --- a/jee7/src/main/java/com/baeldung/javaeeannotations/LogInFilter.java +++ b/jee7/src/main/java/com/baeldung/javaeeannotations/LogInFilter.java @@ -1,36 +1,36 @@ -package com.baeldung.javaeeannotations; - -import java.io.IOException; - -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.annotation.WebFilter; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -@WebFilter( - urlPatterns = "/bankAccount/*", - filterName = "LogInFilter", - description = "Filter all account transaction URLs" - ) -public class LogInFilter implements javax.servlet.Filter { - @Override - public void init(FilterConfig filterConfig) throws ServletException { - } - - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - HttpServletRequest req = (HttpServletRequest) request; - HttpServletResponse res = (HttpServletResponse) response; - - res.sendRedirect(req.getContextPath() + "/login.jsp"); - chain.doFilter(request, response); - } - - @Override - public void destroy() { - } - -} +package com.baeldung.javaeeannotations; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebFilter( + urlPatterns = "/bankAccount/*", + filterName = "LogInFilter", + description = "Filter all account transaction URLs" + ) +public class LogInFilter implements javax.servlet.Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpServletResponse res = (HttpServletResponse) response; + + res.sendRedirect(req.getContextPath() + "/login.jsp"); + chain.doFilter(request, response); + } + + @Override + public void destroy() { + } + +} diff --git a/libraries/pom.xml b/libraries/pom.xml index eaae539872..85c777b12c 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -1,77 +1,83 @@ - - parent-modules - com.baeldung - 1.0.0-SNAPSHOT - - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 - libraries - libraries - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - + libraries + libraries + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + - - - - cglib - cglib - ${cglib.version} - - - org.apache.commons - commons-lang3 - ${commons-lang.version} - - - junit - junit - ${junit.version} - test - - - org.jasypt - jasypt - ${jasypt.version} - - - org.javatuples - javatuples - ${javatuples.version} - - - org.javassist - javassist - ${javaassist.version} - - - - org.assertj - assertj-core - ${assertj.version} - - + + + + cglib + cglib + ${cglib.version} + + + org.apache.commons + commons-lang3 + ${commons-lang.version} + + + junit + junit + ${junit.version} + test + + + org.jasypt + jasypt + ${jasypt.version} + + + org.javatuples + javatuples + ${javatuples.version} + + + org.javassist + javassist + ${javaassist.version} + + + + org.assertj + assertj-core + ${assertj.version} + + + org.skyscreamer + jsonassert + ${jsonassert.version} + + - - 3.2.4 - 3.5 - 4.12 - 1.9.2 - 1.2 - 3.21.0-GA - 3.6.2 - + + 3.2.4 + 3.5 + 4.12 + 1.9.2 + 1.2 + 3.21.0-GA + 3.6.2 + 1.4.0 + - + \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/jsonassert/JsonAssertTest.java b/libraries/src/test/java/com/baeldung/jsonassert/JsonAssertTest.java new file mode 100644 index 0000000000..c169d83897 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/jsonassert/JsonAssertTest.java @@ -0,0 +1,115 @@ +package com.baeldung.jsonassert; + +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Test; +import org.skyscreamer.jsonassert.Customization; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.skyscreamer.jsonassert.RegularExpressionValueMatcher; +import org.skyscreamer.jsonassert.comparator.ArraySizeComparator; +import org.skyscreamer.jsonassert.comparator.CustomComparator; + +public class JsonAssertTest { + + @Test + public void givenLenientode_whenAssertEqualsSameJsonString_thenPass() throws JSONException { + String actual = "{id:123,name:\"John\"}"; + JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT); + + actual = "{id:123,name:\"John\",zip:\"33025\"}"; + JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT); + } + + @Test + public void givenStrictMode_whenAssertNotEqualsExtendedJsonString_thenPass() throws JSONException { + String actual = "{id:123,name:\"John\"}"; + JSONAssert.assertNotEquals("{name:\"John\"}", actual, JSONCompareMode.STRICT); + } + + @Test + public void whenUsingCompareModeOrBoolean_thenBothAreSame() throws JSONException { + String actual = "{id:123,name:\"John\",zip:\"33025\"}"; + JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT); + JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, false); + + actual = "{id:123,name:\"John\"}"; + JSONAssert.assertNotEquals("{name:\"John\"}", actual, JSONCompareMode.STRICT); + JSONAssert.assertNotEquals("{name:\"John\"}", actual, true); + } + + @Test + public void givenDifferentOrderForJsonObject_whenAssertEquals_thenPass() throws JSONException { + String result = "{id:1,name:\"John\"}"; + + JSONAssert.assertEquals("{name:\"John\",id:1}", result, JSONCompareMode.STRICT); + JSONAssert.assertEquals("{name:\"John\",id:1}", result, JSONCompareMode.LENIENT); + } + + @Test + public void givenDifferentTypes_whenAssertEqualsSameValue_thenPass() throws JSONException { + JSONObject expected = new JSONObject(); + JSONObject actual = new JSONObject(); + expected.put("id", Integer.valueOf(12345)); + actual.put("id", Double.valueOf(12345)); + + JSONAssert.assertEquals(expected, actual, false); + JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT); + } + + @Test + public void givenNestedObjects_whenAssertEquals_thenPass() throws JSONException { + String result = "{id:1,name:\"Juergen\", address:{city:\"Hollywood\", " + + "state:\"LA\", zip:91601}}"; + JSONAssert.assertEquals("{id:1,name:\"Juergen\", address:{city:\"Hollywood\", " + + "state:\"LA\", zip:91601}}", result, false); + } + + @Test + public void givenArray_whenComparing_thenOrderMustMatchForStrict() throws JSONException { + String result = "[Alex, Barbera, Charlie, Xavier]"; + JSONAssert.assertEquals("[Charlie, Alex, Xavier, Barbera]", result, JSONCompareMode.LENIENT); + JSONAssert.assertEquals("[Alex, Barbera, Charlie, Xavier]", result, JSONCompareMode.STRICT); + JSONAssert.assertNotEquals("[Charlie, Alex, Xavier, Barbera]", result, JSONCompareMode.STRICT); + } + + @Test + public void givenArray_whenComparingExtended_thenNotEqual() throws JSONException { + String result = "[1,2,3,4,5]"; + JSONAssert.assertEquals("[1,2,3,4,5]", result, JSONCompareMode.LENIENT); + JSONAssert.assertNotEquals("[1,2,3]", result, JSONCompareMode.LENIENT); + JSONAssert.assertNotEquals("[1,2,3,4,5,6]", result, JSONCompareMode.LENIENT); + } + + @Test + public void whenComparingSizeOfArray_thenPass() throws JSONException { + String names = "{names:[Alex, Barbera, Charlie, Xavier]}"; + JSONAssert.assertEquals( + "{names:[4]}", + names, + new ArraySizeComparator(JSONCompareMode.LENIENT)); + } + + @Test + public void whenComparingContentsOfArray_thenPass() throws JSONException { + String ratings = "{ratings:[3.2,3.5,4.1,5,1]}"; + JSONAssert.assertEquals( + "{ratings:[1,5]}", + ratings, + new ArraySizeComparator(JSONCompareMode.LENIENT)); + } + + @Test + public void givenValueMatcher_whenComparingUsingRegex_thenPass() throws IllegalArgumentException, JSONException { + JSONAssert.assertEquals("{entry:{id:x}}", "{entry:{id:1, id:2}}", + new CustomComparator( + JSONCompareMode.STRICT, + new Customization("entry.id", + new RegularExpressionValueMatcher("\\d")))); + + JSONAssert.assertNotEquals("{entry:{id:x}}", "{entry:{id:1, id:as}}", + new CustomComparator(JSONCompareMode.STRICT, + new Customization("entry.id", + new RegularExpressionValueMatcher("\\d")))); + } +} diff --git a/xml/src/main/resources/customer-binding.xml b/xml/src/main/resources/customer-binding.xml old mode 100755 new mode 100644