BAEL-20537: Migrate guava-* modules to the com.baeldung package
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.guava;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class GuavaMapFromSet<K, V> extends AbstractMap<K, V> {
|
||||
|
||||
private class SingleEntry implements Entry<K, V> {
|
||||
private K key;
|
||||
|
||||
public SingleEntry(K key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getValue() {
|
||||
V value = GuavaMapFromSet.this.cache.get(this.key);
|
||||
if (value == null) {
|
||||
value = GuavaMapFromSet.this.function.apply(this.key);
|
||||
GuavaMapFromSet.this.cache.put(this.key, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V setValue(V value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
private class MyEntrySet extends AbstractSet<Entry<K, V>> {
|
||||
|
||||
public class EntryIterator implements Iterator<Entry<K, V>> {
|
||||
private Iterator<K> inner;
|
||||
|
||||
public EntryIterator() {
|
||||
this.inner = MyEntrySet.this.keys.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return this.inner.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map.Entry<K, V> next() {
|
||||
K key = this.inner.next();
|
||||
return new SingleEntry(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
private Set<K> keys;
|
||||
|
||||
public MyEntrySet(Set<K> keys) {
|
||||
this.keys = keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Map.Entry<K, V>> iterator() {
|
||||
return new EntryIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return this.keys.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private WeakHashMap<K, V> cache;
|
||||
private Set<Entry<K, V>> entries;
|
||||
private Function<? super K, ? extends V> function;
|
||||
|
||||
public GuavaMapFromSet(Set<K> keys, Function<? super K, ? extends V> function) {
|
||||
this.function = function;
|
||||
this.cache = new WeakHashMap<K, V>();
|
||||
this.entries = new MyEntrySet(keys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Map.Entry<K, V>> entrySet() {
|
||||
return this.entries;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.guava;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class GuavaMapFromSetUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenStringSet_whenMapsToElementLength_thenCorrect() {
|
||||
Function<Integer, String> function = new Function<Integer, String>() {
|
||||
@Override
|
||||
public String apply(Integer from) {
|
||||
return Integer.toBinaryString(from);
|
||||
}
|
||||
};
|
||||
Set<Integer> set = new TreeSet<>(Arrays.asList(32, 64, 128));
|
||||
Map<Integer, String> map = new GuavaMapFromSet<Integer, String>(set, function);
|
||||
assertTrue(map.get(32).equals("100000")
|
||||
&& map.get(64).equals("1000000")
|
||||
&& map.get(128).equals("10000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() {
|
||||
Function<String, Integer> function = new Function<String, Integer>() {
|
||||
|
||||
@Override
|
||||
public Integer apply(String from) {
|
||||
return from.length();
|
||||
}
|
||||
};
|
||||
Set<String> set = new TreeSet<>(Arrays.asList(
|
||||
"four", "three", "twelve"));
|
||||
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
|
||||
function);
|
||||
assertTrue(map.get("four") == 4 && map.get("three") == 5
|
||||
&& map.get("twelve") == 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() {
|
||||
Function<String, Integer> function = new Function<String, Integer>() {
|
||||
|
||||
@Override
|
||||
public Integer apply(String from) {
|
||||
return from.length();
|
||||
}
|
||||
};
|
||||
Set<String> set = new TreeSet<>(Arrays.asList(
|
||||
"four", "three", "twelve"));
|
||||
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
|
||||
function);
|
||||
set.add("one");
|
||||
assertTrue(map.get("one") == 3 && map.size() == 4);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.guava;
|
||||
|
||||
import com.google.common.collect.HashMultiset;
|
||||
import com.google.common.collect.Multiset;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
public class GuavaMultiSetUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMultiSet_whenAddingValues_shouldReturnCorrectCount() {
|
||||
Multiset<String> bookStore = HashMultiset.create();
|
||||
bookStore.add("Potter");
|
||||
bookStore.add("Potter");
|
||||
bookStore.add("Potter");
|
||||
|
||||
assertThat(bookStore.contains("Potter")).isTrue();
|
||||
assertThat(bookStore.count("Potter")).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiSet_whenRemovingValues_shouldReturnCorrectCount() {
|
||||
Multiset<String> bookStore = HashMultiset.create();
|
||||
bookStore.add("Potter");
|
||||
bookStore.add("Potter");
|
||||
|
||||
bookStore.remove("Potter");
|
||||
assertThat(bookStore.contains("Potter")).isTrue();
|
||||
assertThat(bookStore.count("Potter")).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiSet_whenSetCount_shouldReturnCorrectCount() {
|
||||
Multiset<String> bookStore = HashMultiset.create();
|
||||
bookStore.setCount("Potter", 50);
|
||||
assertThat(bookStore.count("Potter")).isEqualTo(50);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiSet_whenSettingNegativeCount_shouldThrowException() {
|
||||
Multiset<String> bookStore = HashMultiset.create();
|
||||
assertThatThrownBy(() -> bookStore.setCount("Potter", -1))
|
||||
.isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiSet_whenSettingCountWithEmptySet_shouldBeSuccessful() {
|
||||
Multiset<String> bookStore = HashMultiset.create();
|
||||
assertThat(bookStore.setCount("Potter", 0, 2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiSet_whenSettingCountWithCorrectValue_shouldBeSuccessful() {
|
||||
Multiset<String> bookStore = HashMultiset.create();
|
||||
bookStore.add("Potter");
|
||||
bookStore.add("Potter");
|
||||
|
||||
assertThat(bookStore.setCount("Potter", 2, 52)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiSet_whenSettingCountWithIncorrectValue_shouldFail() {
|
||||
Multiset<String> bookStore = HashMultiset.create();
|
||||
bookStore.add("Potter");
|
||||
bookStore.add("Potter");
|
||||
|
||||
assertThat(bookStore.setCount("Potter", 5, 52)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMap_compareMultiSetOperations() {
|
||||
Map<String, Integer> bookStore = new HashMap<>();
|
||||
bookStore.put("Potter", 3);
|
||||
|
||||
assertThat(bookStore.containsKey("Potter")).isTrue();
|
||||
assertThat(bookStore.get("Potter")).isEqualTo(3);
|
||||
|
||||
bookStore.put("Potter", 2);
|
||||
assertThat(bookStore.get("Potter")).isEqualTo(2);
|
||||
|
||||
bookStore.put("Potter", null);
|
||||
assertThat(bookStore.containsKey("Potter")).isTrue();
|
||||
|
||||
bookStore.put("Potter", -1);
|
||||
assertThat(bookStore.containsKey("Potter")).isTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.baeldung.guava;
|
||||
|
||||
import com.google.common.collect.ImmutableRangeSet;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.RangeSet;
|
||||
import com.google.common.collect.TreeRangeSet;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class GuavaRangeSetUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenQueryWithinRange_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
|
||||
assertTrue(numberRangeSet.contains(1));
|
||||
assertFalse(numberRangeSet.contains(9));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenEnclosesWithinRange_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 10));
|
||||
numberRangeSet.add(Range.closed(15, 18));
|
||||
|
||||
assertTrue(numberRangeSet.encloses(Range.closed(4, 5)));
|
||||
assertFalse(numberRangeSet.encloses(Range.closed(4, 11)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenComplementIsCalled_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
final RangeSet<Integer> numberRangeComplementSet = numberRangeSet.complement();
|
||||
|
||||
assertTrue(numberRangeComplementSet.contains(-1000));
|
||||
assertFalse(numberRangeComplementSet.contains(2));
|
||||
assertFalse(numberRangeComplementSet.contains(3));
|
||||
assertTrue(numberRangeComplementSet.contains(1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenIntersectsWithinRange_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 10));
|
||||
numberRangeSet.add(Range.closed(15, 18));
|
||||
|
||||
assertTrue(numberRangeSet.intersects(Range.closed(4, 17)));
|
||||
assertFalse(numberRangeSet.intersects(Range.closed(19, 200)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenRemoveRangeIsCalled_removesSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
numberRangeSet.add(Range.closed(9, 15));
|
||||
numberRangeSet.remove(Range.closed(3, 5));
|
||||
numberRangeSet.remove(Range.closed(7, 10));
|
||||
|
||||
assertTrue(numberRangeSet.contains(1));
|
||||
assertFalse(numberRangeSet.contains(9));
|
||||
assertTrue(numberRangeSet.contains(12));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenSpanIsCalled_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
final Range<Integer> experienceSpan = numberRangeSet.span();
|
||||
|
||||
assertEquals(0, experienceSpan.lowerEndpoint().intValue());
|
||||
assertEquals(8, experienceSpan.upperEndpoint().intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeSet_whenSubRangeSetIsCalled_returnsSubRangeSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = TreeRangeSet.create();
|
||||
|
||||
numberRangeSet.add(Range.closed(0, 2));
|
||||
numberRangeSet.add(Range.closed(3, 5));
|
||||
numberRangeSet.add(Range.closed(6, 8));
|
||||
final RangeSet<Integer> numberSubRangeSet = numberRangeSet.subRangeSet(Range.closed(4, 14));
|
||||
|
||||
assertFalse(numberSubRangeSet.contains(3));
|
||||
assertFalse(numberSubRangeSet.contains(14));
|
||||
assertTrue(numberSubRangeSet.contains(7));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImmutableRangeSet_whenQueryWithinRange_returnsSucessfully() {
|
||||
final RangeSet<Integer> numberRangeSet = ImmutableRangeSet.<Integer> builder()
|
||||
.add(Range.closed(0, 2))
|
||||
.add(Range.closed(3, 5))
|
||||
.add(Range.closed(6, 8)).build();
|
||||
|
||||
assertTrue(numberRangeSet.contains(6));
|
||||
assertFalse(numberRangeSet.contains(15));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenImmutableRangeMap_whenRangeOverlaps_ThrowsException() {
|
||||
ImmutableRangeSet.<Integer> builder()
|
||||
.add(Range.closed(0, 2))
|
||||
.add(Range.closed(3, 5))
|
||||
.add(Range.closed(5, 8)).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.baeldung.guava;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class GuavaSetOperationsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCalculatingUnionOfSets_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b', 'c');
|
||||
final Set<Character> second = ImmutableSet.of('b', 'c', 'd');
|
||||
|
||||
final Set<Character> union = Sets.union(first, second);
|
||||
assertThat(union, containsInAnyOrder('a', 'b', 'c', 'd'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingCartesianProductOfSets_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b');
|
||||
final Set<Character> second = ImmutableSet.of('c', 'd');
|
||||
final Set<List<Character>> result = Sets.cartesianProduct(ImmutableList.of(first, second));
|
||||
|
||||
final Function<List<Character>, String> func = new Function<List<Character>, String>() {
|
||||
@Override
|
||||
public final String apply(final List<Character> input) {
|
||||
return Joiner
|
||||
.on(" ").join(input);
|
||||
}
|
||||
};
|
||||
|
||||
final Iterable<String> joined = Iterables.transform(result, func);
|
||||
assertThat(joined, containsInAnyOrder("a c", "a d", "b c", "b d"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingSetIntersection_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b', 'c');
|
||||
final Set<Character> second = ImmutableSet.of('b', 'c', 'd');
|
||||
|
||||
final Set<Character> intersection = Sets.intersection(first, second);
|
||||
assertThat(intersection, containsInAnyOrder('b', 'c'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingSetSymmetricDifference_thenCorrect() {
|
||||
final Set<Character> first = ImmutableSet.of('a', 'b', 'c');
|
||||
final Set<Character> second = ImmutableSet.of('b', 'c', 'd');
|
||||
|
||||
final Set<Character> intersection = Sets.symmetricDifference(first, second);
|
||||
assertThat(intersection, containsInAnyOrder('a', 'd'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingPowerSet_thenCorrect() {
|
||||
final Set<Character> chars = ImmutableSet.of('a', 'b');
|
||||
final Set<Set<Character>> result = Sets.powerSet(chars);
|
||||
|
||||
final Set<Character> empty = ImmutableSet.<Character> builder().build();
|
||||
final Set<Character> a = ImmutableSet.of('a');
|
||||
final Set<Character> b = ImmutableSet.of('b');
|
||||
final Set<Character> aB = ImmutableSet.of('a', 'b');
|
||||
|
||||
assertThat(result, contains(empty, a, b, aB));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingRangeOfIntegersSet_thenCreated() {
|
||||
final int start = 10;
|
||||
final int end = 30;
|
||||
final ContiguousSet<Integer> set = ContiguousSet.create(Range.closed(start, end), DiscreteDomain.integers());
|
||||
|
||||
assertEquals(21, set.size());
|
||||
assertEquals(10, set.first().intValue());
|
||||
assertEquals(30, set.last().intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingRangeSet_thenCorrect() {
|
||||
final RangeSet<Integer> rangeSet = TreeRangeSet.create();
|
||||
rangeSet.add(Range.closed(1, 10));
|
||||
rangeSet.add(Range.closed(12, 15));
|
||||
|
||||
assertEquals(2, rangeSet.asRanges().size());
|
||||
|
||||
rangeSet.add(Range.closed(10, 12));
|
||||
assertTrue(rangeSet.encloses(Range.closed(1, 15)));
|
||||
assertEquals(1, rangeSet.asRanges().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertDuplicatesInMultiSet_thenInserted() {
|
||||
final Multiset<String> names = HashMultiset.create();
|
||||
names.add("John");
|
||||
names.add("Adam", 3);
|
||||
names.add("John");
|
||||
|
||||
assertEquals(2, names.count("John"));
|
||||
names.remove("John");
|
||||
assertEquals(1, names.count("John"));
|
||||
|
||||
assertEquals(3, names.count("Adam"));
|
||||
names.remove("Adam", 2);
|
||||
assertEquals(1, names.count("Adam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetTopOcurringElementsWithMultiSet_thenCorrect() {
|
||||
final Multiset<String> names = HashMultiset.create();
|
||||
names.add("John");
|
||||
names.add("Adam", 5);
|
||||
names.add("Jane");
|
||||
names.add("Tom", 2);
|
||||
|
||||
final Set<String> sorted = Multisets.copyHighestCountFirst(names).elementSet();
|
||||
final List<String> topTwo = Lists.newArrayList(sorted).subList(0, 2);
|
||||
assertEquals(2, topTwo.size());
|
||||
assertEquals("Adam", topTwo.get(0));
|
||||
assertEquals("Tom", topTwo.get(1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user