BAEL-718 Quick intro to javatuplesMaster (#1389)

* yasin.bhojawala@gmail.com

Evaluation article on Different Types of Bean Injection in Spring

* Revert "yasin.bhojawala@gmail.com"

This reverts commit 963cc51a7a15b75b550108fe4e198cd65a274032.

* Fixing compilation error and removing unused import

* Introduction to Java9 StackWalking API - yasin.bhojawala@gmail.com

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
This commit is contained in:
Yasin 2017-03-13 22:42:19 +05:30 committed by maibin
parent 4a773e79cd
commit b6c32dbcd3
3 changed files with 188 additions and 54 deletions

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);
}
}