Merge remote-tracking branch 'refs/remotes/eugenp/master'

This commit is contained in:
iaforek 2017-03-15 07:15:57 +00:00
commit fc69dcd7d2
20 changed files with 232 additions and 104 deletions

View File

@ -1,11 +0,0 @@
package com.baeldung.examples.guice.marker;
/**
*
* @author Baeldung
*/
public interface Communicator {
public boolean sendMessage(String message);
}

View File

@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.examples.guice</groupId>
<artifactId>guice-intro</artifactId>
<artifactId>guice</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
@ -30,5 +30,5 @@
<maven.compiler.target>1.8</maven.compiler.target>
<guice.version>4.1.0</guice.version>
</properties>
<name>guice-intro</name>
<name>guice</name>
</project>

View File

@ -10,7 +10,7 @@ import java.util.Scanner;
/**
*
* @author Baeldung
* @author baeldung
*/
public class RunGuice {
@ -18,14 +18,10 @@ public class RunGuice {
Injector injector = Guice.createInjector(new BasicModule(), new AOPModule());
Communication comms = injector.getInstance(Communication.class);
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your message to be sent; press Q to quit and P to print the message log");
while (true) {
String input = scanner.nextLine();
if (input.equalsIgnoreCase("q")) {
System.exit(0);
}
if (input.equalsIgnoreCase("p")) {
comms.print();
} else {
comms.sendMessage(input);
}

View File

@ -10,7 +10,7 @@ import java.util.logging.Logger;
/**
*
* @author Baeldung
* @author baeldung
*/
public class Communication {
@ -19,37 +19,20 @@ public class Communication {
@Inject
private Logger logger;
private Queue<String> messageLog;
@Named("CommsUUID")
private String commsID;
@Inject
private DefaultCommunicator communicator;
public Communication(Boolean keepRecords) {
if (keepRecords) {
messageLog = new LinkedList();
System.out.println("keeping records");
}
}
public boolean sendMessage(String message) {
if (!message.isEmpty() && messageLog != null) {
messageLog.add(message);
}
return communicator.sendMessage(message);
}
public void print() {
if (messageLog != null) {
for (String message : messageLog) {
logger.info(message);
}
} else {
logger.info("Message logging wasn't enabled");
}
}
public DefaultCommunicator getCommunicator() {
return this.communicator;
}

View File

@ -5,7 +5,10 @@ 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;

View File

@ -1,3 +1,4 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
@ -5,7 +6,7 @@ import com.baeldung.examples.guice.constant.CommunicationModel;
/**
*
* @author Baekdung
* @author baeldung
*/
public class EmailCommunicationMode implements CommunicationMode {

View File

@ -8,7 +8,7 @@ import java.util.logging.Logger;
/**
*
* @author Baeldung
* @author baeldung
*/
public class IMCommunicationMode implements CommunicationMode {

View File

@ -1,3 +1,4 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
@ -7,7 +8,7 @@ import java.util.logging.Logger;
/**
*
* @author Baeldung
* @author baeldung
*/
public class SMSCommunicationMode implements CommunicationMode {

View File

@ -1,12 +1,14 @@
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
* @author baeldung
*/
public class MessageLogger implements MethodInterceptor {

View File

@ -1,3 +1,4 @@
package com.baeldung.examples.guice.aop;
import java.lang.annotation.ElementType;
@ -7,7 +8,7 @@ import java.lang.annotation.Target;
/**
*
* @author Baeldung
* @author baeldung
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

View File

@ -1,3 +1,4 @@
package com.baeldung.examples.guice.binding;
import com.baeldung.examples.guice.aop.MessageLogger;
@ -7,7 +8,7 @@ import com.google.inject.matcher.Matchers;
/**
*
* @author Baeldung
* @author baeldung
*/
public class AOPModule extends AbstractModule {

View File

@ -1,3 +1,4 @@
package com.baeldung.examples.guice.binding;
import com.baeldung.examples.guice.Communication;
@ -13,7 +14,7 @@ import java.util.logging.Logger;
/**
*
* @author Baeldung
* @author baeldung
*/
public class BasicModule extends AbstractModule {

View File

@ -1,8 +1,9 @@
package com.baeldung.examples.guice.constant;
/**
*
* @author Baeldung
* @author baeldung
*/
public enum CommunicationModel {

View File

@ -0,0 +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 {
}

View File

@ -1,3 +1,4 @@
package com.baeldung.examples.guice.modules;
import com.baeldung.examples.guice.Communication;
@ -13,7 +14,7 @@ import java.util.logging.Logger;
/**
*
* @author Baeldung
* @author baeldung
*/
public class BasicModule extends AbstractModule {

5
libraries/README.md Normal file
View File

@ -0,0 +1,5 @@
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.
The code examples related to different libraries should go in a new package.
Remember, for advanced libraries like JUnit, Jackson, etc. we already have separate modules. Please make sure to have a look at the existing modules in such cases.

View File

@ -1,60 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries</artifactId>
<name>libraries</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<artifactId>libraries</artifactId>
<name>libraries</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>${jasypt.version}</version>
</dependency>
<dependencies>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>${jasypt.version}</version>
</dependency>
<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>${javatuples.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
</dependencies>
</dependencies>
<properties>
<cglib.version>3.2.4</cglib.version>
<commons-lang.version>3.5</commons-lang.version>
<junit.version>4.12</junit.version>
<jasypt.version>1.9.2</jasypt.version>
</properties>
<properties>
<cglib.version>3.2.4</cglib.version>
<commons-lang.version>3.5</commons-lang.version>
<junit.version>4.12</junit.version>
<jasypt.version>1.9.2</jasypt.version>
<javatuples.version>1.2</javatuples.version>
<assertj.version>3.6.2</assertj.version>
</properties>
</project>

View File

@ -0,0 +1,118 @@
package com.baeldung.javatuples;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import org.javatuples.KeyValue;
import org.javatuples.LabelValue;
import org.javatuples.Pair;
import org.javatuples.Quartet;
import org.javatuples.Triplet;
import org.javatuples.Unit;
import org.junit.Test;
public class JavaTuplesTest {
@SuppressWarnings("unused")
@Test
public void whenCreatingTuples_thenCreateTuples() {
Pair<String, Integer> pair = new Pair<String, Integer>("This is a pair", 55);
Triplet<String, Integer, Double> triplet = Triplet.with("hello", 23, 33.2);
List<String> collectionOfNames = Arrays.asList("john", "doe", "anne", "alex");
Quartet<String, String, String, String> quartet = Quartet.fromCollection(collectionOfNames);
Pair<String, String> pairFromList = Pair.fromIterable(collectionOfNames, 2);
String[] names = new String[] { "john", "doe", "anne" };
Triplet<String, String, String> triplet2 = Triplet.fromArray(names);
}
@Test
public void whenGetValuexFromTuples_thenRetriveValueWithType() {
Quartet<String, Double, Integer, String> quartet = Quartet.with("john", 72.5, 32, "1051 SW");
String name = quartet.getValue0();
Integer age = quartet.getValue2();
assertThat(name).isEqualTo("john");
assertThat(age).isEqualTo(32);
}
@Test
public void whenGetKeyValue_thenGetKeyValue() {
KeyValue<Integer, String> keyValue = KeyValue.with(5, "F");
Integer key = keyValue.getKey();
String value = keyValue.getValue();
assertThat(key).isEqualTo(5);
assertThat(value).isEqualTo("F");
}
@Test
public void whenGetLabelValue_thenGetLabelValue() {
LabelValue<Integer, String> labelValue = LabelValue.with(5, "F");
Integer key = labelValue.getLabel();
String value = labelValue.getValue();
assertThat(key).isEqualTo(5);
assertThat(value).isEqualTo("F");
}
@Test
public void whenGetValueFromTuples_thenRetriveValueWithoutType() {
Quartet<String, Double, Integer, String> quartet = Quartet.with("john", 72.5, 32, "1051 SW");
String name = (String) quartet.getValue(0);
Integer age = (Integer) quartet.getValue(2);
assertThat(name).isEqualTo("john");
assertThat(age).isEqualTo(32);
}
@Test
public void whenSetValueInTuple_thenGetANewTuple() {
Pair<String, Integer> john = Pair.with("john", 32);
Pair<String, Integer> alex = john.setAt0("alex");
assertThat(john.toString()).isNotEqualTo(alex.toString());
}
@Test
public void whenAddNewElement_thenCreateNewTuple() {
Pair<String, Integer> pair1 = Pair.with("john", 32);
Triplet<String, Integer, String> triplet1 = pair1.add("1051 SW");
assertThat(triplet1.contains("john"));
assertThat(triplet1.contains(32));
assertThat(triplet1.contains("1051 SW"));
Pair<String, Integer> pair2 = Pair.with("alex", 45);
Quartet<String, Integer, String, Integer> quartet2 = pair1.add(pair2);
assertThat(quartet2.containsAll(pair1));
assertThat(quartet2.containsAll(pair2));
Quartet<String, Integer, String, Integer> quartet1 = pair1.add("alex", 45);
assertThat(quartet1.containsAll("alex", "john", 32, 45));
Triplet<String, String, Integer> triplet2 = pair1.addAt1("1051 SW");
assertThat(triplet2.indexOf("john")).isEqualTo(0);
assertThat(triplet2.indexOf("1051 SW")).isEqualTo(1);
assertThat(triplet2.indexOf(32)).isEqualTo(2);
Unit<Integer> unit = pair1.removeFrom0();
assertThat(unit.contains(32));
}
@Test
public void whenCallingToList_thenReturnList() {
Quartet<String, Double, Integer, String> quartet = Quartet.with("john", 72.5, 32, "1051 SW");
List<Object> list = quartet.toList();
assertThat(list.size()).isEqualTo(4);
}
@Test
public void whenCallingToArray_thenReturnArray() {
Quartet<String, Double, Integer, String> quartet = Quartet.with("john", 72.5, 32, "1051 SW");
Object[] array = quartet.toArray();
assertThat(array.length).isEqualTo(4);
}
}

View File

@ -47,7 +47,7 @@
<module>guava</module>
<module>guava18</module>
<module>guava19</module>
<module>guice-intro</module>
<module>guice</module>
<module>disruptor</module>
<module>handling-spring-static-resources</module>