Merge pull request #345 from aborkov/master

Added code for Guava 19 article
This commit is contained in:
David Morley 2016-02-10 04:52:49 -06:00
commit 645e7a0bc2
6 changed files with 289 additions and 0 deletions

46
guava19/pom.xml Normal file
View File

@ -0,0 +1,46 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>guava</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<debug>true</debug>
<optimize>true</optimize>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,36 @@
package com.baeldung.guava.entity;
import com.google.common.base.MoreObjects;
public class User{
private long id;
private String name;
private int age;
public User(long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(User.class)
.add("id", id)
.add("name", name)
.add("age", age)
.toString();
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.guava;
import com.google.common.base.CharMatcher;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class CharMatcherTest {
@Test
public void whenMatchingLetterOrString_ShouldReturnTrueForCorrectString() throws Exception {
String inputString = "someString789";
boolean result = CharMatcher.javaLetterOrDigit().matchesAllOf(inputString);
assertTrue(result);
}
@Test
public void whenCollapsingString_ShouldReturnStringWithDashesInsteadOfWhitespaces() throws Exception {
String inputPhoneNumber = "8 123 456 123";
String result = CharMatcher.whitespace().collapseFrom(inputPhoneNumber, '-');
assertEquals("8-123-456-123", result);
}
@Test
public void whenCountingDigitsInString_ShouldReturnActualCountOfDigits() throws Exception {
String inputPhoneNumber = "8 123 456 123";
int result = CharMatcher.digit().countIn(inputPhoneNumber);
assertEquals(10, result);
}
}

View File

@ -0,0 +1,95 @@
package com.baeldung.guava;
import com.baeldung.guava.entity.User;
import com.google.common.base.CharMatcher;
import com.google.common.base.Throwables;
import com.google.common.collect.*;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.common.reflect.TypeToken;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.junit.Assert.*;
public class GuavaMiscUtilsTest {
@Test
public void whenGettingLazyStackTrace_ListShouldBeReturned() throws Exception {
IllegalArgumentException e = new IllegalArgumentException("Some argument is incorrect");
List<StackTraceElement> stackTraceElements = Throwables.lazyStackTrace(e);
assertTrue(stackTraceElements.size() > 0);
}
@Test
public void multisetShouldCountHitsOfMultipleDuplicateObjects() throws Exception {
List<String> userNames = Arrays.asList("David", "Eugene", "Alex", "Alex", "David", "David", "David");
Multiset<String> userNamesMultiset = HashMultiset.create(userNames);
assertEquals(7, userNamesMultiset.size());
assertEquals(4, userNamesMultiset.count("David"));
assertEquals(2, userNamesMultiset.count("Alex"));
assertEquals(1, userNamesMultiset.count("Eugene"));
assertThat(userNamesMultiset.elementSet(), anyOf(containsInAnyOrder("Alex", "David", "Eugene")));
}
@Test
public void whenAddingNewConnectedRange_RangesShouldBeMerged() throws Exception {
RangeSet<Integer> rangeSet = TreeRangeSet.create();
rangeSet.add(Range.closed(1, 10));
rangeSet.add(Range.closed(5, 15));
rangeSet.add(Range.closedOpen(10, 17));
assertTrue(rangeSet.encloses(Range.closedOpen(1, 17)));
assertTrue(rangeSet.encloses(Range.closed(2, 3)));
assertTrue(rangeSet.contains(15));
assertFalse(rangeSet.contains(17));
assertEquals(1, rangeSet.asDescendingSetOfRanges().size());
}
@Test
public void cartesianProductShouldReturnAllPossibleCombinations() throws Exception {
List<String> first = Lists.newArrayList("value1", "value2");
List<String> second = Lists.newArrayList("value3", "value4");
List<List<String>> cartesianProduct = Lists.cartesianProduct(first, second);
List<String> pair1 = Lists.newArrayList("value2", "value3");
List<String> pair2 = Lists.newArrayList("value2", "value4");
List<String> pair3 = Lists.newArrayList("value1", "value3");
List<String> pair4 = Lists.newArrayList("value1", "value4");
assertThat(cartesianProduct, anyOf(containsInAnyOrder(pair1, pair2, pair3, pair4)));
}
@Test
public void multisetShouldRemoveOccurrencesOfSpecifiedObjects() throws Exception {
Multiset<String> multisetToModify = HashMultiset.create();
Multiset<String> occurrencesToRemove = HashMultiset.create();
multisetToModify.add("John");
multisetToModify.add("Max");
multisetToModify.add("Alex");
occurrencesToRemove.add("Alex");
occurrencesToRemove.add("John");
Multisets.removeOccurrences(multisetToModify, occurrencesToRemove);
assertEquals(1, multisetToModify.size());
assertTrue(multisetToModify.contains("Max"));
assertFalse(multisetToModify.contains("John"));
assertFalse(multisetToModify.contains("Alex"));
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.guava;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HashingTest {
@Test
public void whenHashingInSha384_hashFunctionShouldBeReturned() throws Exception {
int inputData = 15;
HashFunction hashFunction = Hashing.sha384();
HashCode hashCode = hashFunction.hashInt(inputData);
assertEquals("0904b6277381dcfbdddd6b6c66e4e3e8f83d4690718d8e6f272c891f24773a12feaf8c449fa6e42240a621b2b5e3cda8",
hashCode.toString());
}
@Test
public void whenConcatenatingHashFunction_concatenatedHashShouldBeReturned() throws Exception {
int inputData = 15;
HashFunction hashFunction = Hashing.concatenating(Hashing.crc32(), Hashing.crc32());
HashFunction crc32Function = Hashing.crc32();
HashCode hashCode = hashFunction.hashInt(inputData);
HashCode crc32HashCode = crc32Function.hashInt(inputData);
assertEquals(crc32HashCode.toString() + crc32HashCode.toString(), hashCode.toString());
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.guava;
import com.google.common.reflect.TypeToken;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class TypeTokenTest {
@Test
public void whenCheckingIsAssignableFrom_shouldReturnTrueEvenIfGenericIsSpecified() throws Exception {
ArrayList<String> stringList = new ArrayList<>();
ArrayList<Integer> intList = new ArrayList<>();
boolean isAssignableFrom = stringList.getClass().isAssignableFrom(intList.getClass());
assertTrue(isAssignableFrom);
}
@Test
public void whenCheckingIsSupertypeOf_shouldReturnFalseIfGenericIsSpecified() throws Exception {
TypeToken<ArrayList<String>> listString = new TypeToken<ArrayList<String>>() {
};
TypeToken<ArrayList<Integer>> integerString = new TypeToken<ArrayList<Integer>>() {
};
boolean isSupertypeOf = listString.isSupertypeOf(integerString);
assertFalse(isSupertypeOf);
}
@Test
public void whenCheckingIsSubtypeOf_shouldReturnTrueIfClassIsExtendedFrom() throws Exception {
TypeToken<ArrayList<String>> stringList = new TypeToken<ArrayList<String>>() {
};
TypeToken<List> list = new TypeToken<List>() {
};
boolean isSubtypeOf = stringList.isSubtypeOf(list);
assertTrue(isSubtypeOf);
}
}