group and cleanup (#3027)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules * group testing modules * try fix conflict * cleanup * group and cleanup
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
2a85b9c96e
commit
2e5531edd0
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.util.concurrent.AtomicLongMap;
|
||||
|
||||
public class AtomicLongMapTutorials {
|
||||
|
||||
private AtomicLongMap<String> atomicLongMap;
|
||||
|
||||
public AtomicLongMapTutorials() {
|
||||
atomicLongMap = AtomicLongMap.create();
|
||||
}
|
||||
|
||||
public void addKeys() {
|
||||
atomicLongMap.addAndGet("apple", 250);
|
||||
atomicLongMap.addAndGet("bat", 350);
|
||||
atomicLongMap.addAndGet("cat", 450);
|
||||
atomicLongMap.addAndGet("dog", 550);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
AtomicLongMapTutorials atomicLongMapTutorials = new AtomicLongMapTutorials();
|
||||
atomicLongMapTutorials.addKeys();
|
||||
|
||||
System.out.println(atomicLongMapTutorials.atomicLongMap.get("2"));
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.collect.Comparators;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class ComparatorsExamples {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 4, 6, 7, 8, 9, 10);
|
||||
boolean isInAscendingOrder = Comparators.isInOrder(integers, new AscedingOrderComparator());
|
||||
System.out.println(isInAscendingOrder);
|
||||
|
||||
}
|
||||
|
||||
private static class AscedingOrderComparator implements Comparator<Integer> {
|
||||
@Override
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ConcatStreams {
|
||||
public static Stream concatStreams(Stream stream1, Stream stream2, Stream stream3) {
|
||||
return Streams.concat(stream1, stream2, stream3);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.collect.Interner;
|
||||
import com.google.common.collect.Interners;
|
||||
|
||||
public class InternerBuilderExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Interner<Integer> interners = Interners.<Integer> newBuilder()
|
||||
.concurrencyLevel(2)
|
||||
.strong().<Integer> build();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.util.concurrent.Monitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MonitorExample {
|
||||
private List<String> students = new ArrayList<String>();
|
||||
private static final int MAX_SIZE = 100;
|
||||
|
||||
private Monitor monitor = new Monitor();
|
||||
|
||||
public void addToCourse(String item) throws InterruptedException {
|
||||
Monitor.Guard studentsBelowCapacity = monitor.newGuard(this::isStudentsCapacityUptoLimit);
|
||||
monitor.enterWhen(studentsBelowCapacity);
|
||||
try {
|
||||
students.add(item);
|
||||
} finally {
|
||||
monitor.leave();
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean isStudentsCapacityUptoLimit() {
|
||||
return students.size() > MAX_SIZE;
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.collect.MoreCollectors;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MoreCollectorsExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Integer> numbers = Arrays.asList(1);
|
||||
Optional<Integer> number = numbers
|
||||
.stream()
|
||||
.map(e -> e * 2)
|
||||
.collect(MoreCollectors.toOptional());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.DoubleStream;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class StreamsUtility {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
|
||||
//Using Collection
|
||||
Stream<Integer> streamFromCollection = Streams.stream(numbers);
|
||||
//Using Iterator
|
||||
Stream<Integer> streamFromIterator = Streams.stream(numbers.iterator());
|
||||
//Using Iterable
|
||||
Stream<Integer> streamFromIterable = Streams.stream((Iterable<Integer>) numbers);
|
||||
//Using Optional
|
||||
Stream<Integer> streamFromOptional = Streams.stream(Optional.of(1));
|
||||
//Using OptionalLong to LongStream
|
||||
LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1));
|
||||
//Using OptionalInt to IntStream
|
||||
IntStream streamFromOptionalInt = Streams.stream(OptionalInt.of(1));
|
||||
//Using OptionalDouble to DoubleStream
|
||||
DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0));
|
||||
|
||||
Stream<Integer> concatenatedStreams = Streams.concat(streamFromCollection, streamFromIterable, streamFromIterator);
|
||||
|
||||
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
//This will return 10
|
||||
Optional<Integer> lastItem = Streams.findLast(integers.stream());
|
||||
|
||||
Streams.zip(Stream.of("candy", "chocolate", "bar"), Stream.of("$1", "$2", "$3"), (arg1, arg2) -> arg1 + ":" + arg2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.baeldung.guava.zip;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
import org.jooq.lambda.Seq;
|
||||
import org.jooq.lambda.tuple.Tuple2;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ZipCollectionTest {
|
||||
|
||||
private List<String> names;
|
||||
private List<Integer> ages;
|
||||
private List<String> expectedOutput;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
names = Arrays.asList("John", "Jane", "Jack", "Dennis");
|
||||
ages = Arrays.asList(24, 25, 27);
|
||||
expectedOutput = Arrays.asList("John:24", "Jane:25", "Jack:27");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipCollectionUsingGuava21() {
|
||||
List<String> output = Streams
|
||||
.zip(names.stream(), ages.stream(), (name, age) -> name + ":" + age)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(output, expectedOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipCollectionUsingIntStream() {
|
||||
List<String> output = IntStream
|
||||
.range(0, Math.min(names.size(), ages.size()))
|
||||
.mapToObj(i -> names.get(i) + ":" + ages.get(i))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(output, expectedOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipCollectionUsingJool() {
|
||||
Seq<String> output = Seq
|
||||
.of("John", "Jane", "Jack")
|
||||
.zip(Seq.of(24, 25, 27), (x, y) -> x + ":" + y);
|
||||
|
||||
assertEquals(output.toList(), expectedOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipCollectionUsingJoolTuple() {
|
||||
Seq<Tuple2<String, Integer>> output = Seq
|
||||
.of("John", "Jane", "Dennis")
|
||||
.zip(Seq.of(24, 25, 27));
|
||||
|
||||
Tuple2<String, Integer> element1 = new Tuple2<String, Integer>("John", 24);
|
||||
Tuple2<String, Integer> element2 = new Tuple2<String, Integer>("Jane", 25);
|
||||
Tuple2<String, Integer> element3 = new Tuple2<String, Integer>("Dennis", 27);
|
||||
|
||||
List<Tuple2> expectedOutput = Arrays.asList(element1, element2, element3);
|
||||
assertEquals(output.collect(Collectors.toList()), expectedOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipCollectionUsingJoolWithIndex() {
|
||||
Seq<Tuple2<String, Long>> output = Seq
|
||||
.of("John", "Jane", "Dennis")
|
||||
.zipWithIndex();
|
||||
|
||||
Tuple2<String, Long> element1 = new Tuple2<>("John", 0L);
|
||||
Tuple2<String, Long> element2 = new Tuple2<>("Jane", 1L);
|
||||
Tuple2<String, Long> element3 = new Tuple2<>("Dennis", 2L);
|
||||
|
||||
List<Tuple2> expectedOutput = Arrays.asList(element1, element2, element3);
|
||||
assertEquals(output.collect(Collectors.toList()), expectedOutput);
|
||||
}
|
||||
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.util.concurrent.AtomicLongMap;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AtomicLongMapIntegrationTest {
|
||||
|
||||
private static final String SPRING_COURSE_KEY = "Spring";
|
||||
private static final String HIBERNATE_COURSE_KEY = "hibernate";
|
||||
private static final String GUAVA_COURSE_KEY = "Guava";
|
||||
|
||||
AtomicLongMap<String> courses = AtomicLongMap.create();
|
||||
|
||||
public void setUp() {
|
||||
courses.put(SPRING_COURSE_KEY, 1056);
|
||||
courses.put(HIBERNATE_COURSE_KEY, 259);
|
||||
courses.put(GUAVA_COURSE_KEY, 78);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accumulateAndGet_withLongBinaryOperator_thenSuccessful() {
|
||||
long noOfStudents = 56;
|
||||
long oldValue = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
long totalNotesRequired = courses.accumulateAndGet("Guava", noOfStudents, (x, y) -> (x * y));
|
||||
|
||||
assertEquals(totalNotesRequired, oldValue * noOfStudents);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAndAccumulate_withLongBinaryOperator_thenSuccessful() {
|
||||
long noOfStudents = 56;
|
||||
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
long onUpdate = courses.accumulateAndGet("Guava", noOfStudents, (x, y) -> (x * y));
|
||||
|
||||
long afterUpdate = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
assertEquals(onUpdate, afterUpdate);
|
||||
assertEquals(afterUpdate, beforeUpdate * noOfStudents);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateAndGet_withLongUnaryOperator_thenSuccessful() {
|
||||
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
long onUpdate = courses.updateAndGet("Guava", (x) -> (x / 2));
|
||||
|
||||
long afterUpdate = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
assertEquals(onUpdate, afterUpdate);
|
||||
assertEquals(afterUpdate, beforeUpdate / 2);
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.collect.Comparators;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.ToDoubleFunction;
|
||||
import java.util.function.ToIntFunction;
|
||||
import java.util.function.ToLongFunction;
|
||||
|
||||
public class ComparatorsUnitTest {
|
||||
|
||||
@Test
|
||||
public void isInOrderTest() {
|
||||
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 4, 6, 7, 8, 9, 10);
|
||||
|
||||
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
|
||||
|
||||
Assert.assertTrue(isInAscendingOrder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isInStrictOrderTest() {
|
||||
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 3, 6, 7, 8, 9, 10);
|
||||
|
||||
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
|
||||
|
||||
Assert.assertFalse(isInAscendingOrder);
|
||||
}
|
||||
|
||||
private class AscendingOrderComparator<I extends Number> implements Comparator<Integer> {
|
||||
|
||||
@Override
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator<Integer> reversed() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator<Integer> thenComparing(Comparator<? super Integer> other) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <U> Comparator<Integer> thenComparing(Function<? super Integer, ? extends U> keyExtractor, Comparator<? super U> keyComparator) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <U extends Comparable<? super U>> Comparator<Integer> thenComparing(Function<? super Integer, ? extends U> keyExtractor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator<Integer> thenComparingInt(ToIntFunction<? super Integer> keyExtractor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator<Integer> thenComparingLong(ToLongFunction<? super Integer> keyExtractor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator<Integer> thenComparingDouble(ToDoubleFunction<? super Integer> keyExtractor) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.OptionalLong;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.DoubleStream;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.baeldung.guava.tutorial.StreamUtility.assertStreamEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class GuavaStreamsUnitTest {
|
||||
|
||||
private List<Integer> numbers;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
numbers = IntStream.rangeClosed(1, 20).boxed().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createStreamsWithCollection() {
|
||||
//Deprecated API to create stream from collection
|
||||
Stream streamFromCollection = Streams.stream(numbers);
|
||||
|
||||
//Assert.assertNotNull(streamFromCollection);
|
||||
assertStreamEquals(streamFromCollection, numbers.stream());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createStreamsWithIterable() {
|
||||
Iterable<Integer> numbersIterable = numbers;
|
||||
|
||||
Stream streamFromIterable = Streams.stream(numbersIterable);
|
||||
|
||||
assertNotNull(streamFromIterable);
|
||||
assertStreamEquals(streamFromIterable, numbers.stream());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createStreamsWithIterator() {
|
||||
Iterator<Integer> numbersIterator = numbers.iterator();
|
||||
|
||||
Stream streamFromIterator = Streams.stream(numbersIterator);
|
||||
|
||||
assertNotNull(streamFromIterator);
|
||||
assertStreamEquals(streamFromIterator, numbers.stream());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createStreamsWithOptional() {
|
||||
|
||||
Stream streamFromOptional = Streams.stream(Optional.of(1));
|
||||
|
||||
assertNotNull(streamFromOptional);
|
||||
assertEquals(streamFromOptional.count(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createStreamsWithOptionalLong() {
|
||||
|
||||
LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1));
|
||||
|
||||
assertNotNull(streamFromOptionalLong);
|
||||
assertEquals(streamFromOptionalLong.count(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createStreamsWithOptionalInt() {
|
||||
|
||||
IntStream streamFromOptionalInt = Streams.stream(OptionalInt.of(1));
|
||||
|
||||
//Assert.assertNotNull(streamFromOptionalInt);
|
||||
assertEquals(streamFromOptionalInt.count(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createStreamsWithOptionalDouble() {
|
||||
|
||||
DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0));
|
||||
|
||||
//Assert.assertNotNull(streamFromOptionalDouble);
|
||||
assertEquals(streamFromOptionalDouble.count(), 1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void concatStreamsOfSameType() {
|
||||
List<Integer> oddNumbers = Arrays
|
||||
.asList(1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
|
||||
List<Integer> evenNumbers = Arrays
|
||||
.asList(2, 4, 6, 8, 10, 12, 14, 16, 18, 20);
|
||||
|
||||
Stream<Integer> combinedStreams = Streams.concat(oddNumbers.stream(), evenNumbers.stream());
|
||||
|
||||
//Assert.assertNotNull(combinedStreams);
|
||||
assertStreamEquals(combinedStreams, Stream.concat(oddNumbers.stream(), evenNumbers.stream()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void concatStreamsOfTypeLongStream() {
|
||||
LongStream combinedStreams = Streams.concat(LongStream.range(1, 21), LongStream.range(21, 40));
|
||||
|
||||
assertNotNull(combinedStreams);
|
||||
assertStreamEquals(combinedStreams, LongStream.range(1, 40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void concatStreamsOfTypeIntStream() {
|
||||
IntStream combinedStreams = Streams.concat(IntStream.range(1, 20), IntStream.range(21, 40));
|
||||
|
||||
assertNotNull(combinedStreams);
|
||||
assertStreamEquals(combinedStreams, IntStream.concat(IntStream.range(1, 20), IntStream.range(21, 40)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findLastOfStream() {
|
||||
Optional<Integer> lastElement = Streams.findLast(numbers.stream());
|
||||
|
||||
assertEquals(lastElement.get(), numbers.get(19));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapWithIndexTest() {
|
||||
Stream<String> stringStream = Stream.of("a", "b", "c");
|
||||
|
||||
Stream<String> mappedStream = Streams.mapWithIndex(stringStream, (str, index) -> str + ":" + index);
|
||||
|
||||
//Assert.assertNotNull(mappedStream);
|
||||
assertEquals(mappedStream
|
||||
.findFirst()
|
||||
.get(), "a:0");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamsZipTest() {
|
||||
Stream<String> stringSream = Stream.of("a", "b", "c");
|
||||
Stream<Integer> intStream = Stream.of(1, 2, 3);
|
||||
Stream<String> mappedStream = Streams.zip(stringSream, intStream, (str, index) -> str + ":" + index);
|
||||
|
||||
//Assert.assertNotNull(mappedStream);
|
||||
assertEquals(mappedStream
|
||||
.findFirst()
|
||||
.get(), "a:1");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.collect.Interner;
|
||||
import com.google.common.collect.Interners;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class InternBuilderUnitTest {
|
||||
|
||||
@Test
|
||||
public void interBuilderTest() {
|
||||
|
||||
Interner<Integer> interners = Interners.<Integer> newBuilder()
|
||||
.concurrencyLevel(2)
|
||||
.strong().<Integer> build();
|
||||
|
||||
Assert.assertNotNull(interners);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.util.concurrent.Monitor;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MonitorUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenGaurdConditionIsTrue_IsSuccessful() {
|
||||
Monitor monitor = new Monitor();
|
||||
boolean enteredInCriticalSection = false;
|
||||
|
||||
Monitor.Guard gaurdCondition = monitor.newGuard(this::returnTrue);
|
||||
|
||||
if (monitor.enterIf(gaurdCondition)) {
|
||||
try {
|
||||
System.out.println("Entered in critical section");
|
||||
enteredInCriticalSection = true;
|
||||
} finally {
|
||||
monitor.leave();
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertTrue(enteredInCriticalSection);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGaurdConditionIsFalse_IsSuccessful() {
|
||||
Monitor monitor = new Monitor();
|
||||
boolean enteredInCriticalSection = false;
|
||||
|
||||
Monitor.Guard gaurdCondition = monitor.newGuard(this::returnFalse);
|
||||
|
||||
if (monitor.enterIf(gaurdCondition)) {
|
||||
try {
|
||||
System.out.println("Entered in critical section");
|
||||
enteredInCriticalSection = true;
|
||||
} finally {
|
||||
monitor.leave();
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertFalse(enteredInCriticalSection);
|
||||
}
|
||||
|
||||
private boolean returnTrue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean returnFalse() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.collect.MoreCollectors;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MoreCollectorsUnitTest {
|
||||
|
||||
@Test
|
||||
public void toOptionalTest() {
|
||||
|
||||
List<Integer> numbers = Arrays.asList(1);
|
||||
|
||||
Optional<Integer> number = numbers
|
||||
.stream()
|
||||
.map(e -> e * 2)
|
||||
.collect(MoreCollectors.toOptional());
|
||||
|
||||
Assert.assertEquals(number.get(), new Integer(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onlyElementTest() {
|
||||
List<Integer> numbers = Arrays.asList(1);
|
||||
|
||||
Integer number = numbers
|
||||
.stream()
|
||||
.map(e -> e * 2)
|
||||
.collect(MoreCollectors.onlyElement());
|
||||
|
||||
Assert.assertEquals(number, new Integer(2));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.stream.DoubleStream;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class StreamUtility {
|
||||
|
||||
public static <T> boolean assertStreamEquals(Stream<T> stream1, Stream<T> stream2) {
|
||||
|
||||
Iterator<T> iterator1 = stream1.iterator();
|
||||
Iterator<T> iterator2 = stream2.iterator();
|
||||
|
||||
while (iterator1.hasNext()) {
|
||||
Assert.assertEquals(iterator1.next(), iterator2.next());
|
||||
}
|
||||
|
||||
Assert.assertFalse(iterator2.hasNext());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean assertStreamEquals(LongStream stream1, LongStream stream2) {
|
||||
|
||||
Iterator iterator1 = stream1.iterator();
|
||||
Iterator iterator2 = stream2.iterator();
|
||||
|
||||
while (iterator1.hasNext()) {
|
||||
Assert.assertEquals(iterator1.next(), iterator2.next());
|
||||
}
|
||||
|
||||
Assert.assertFalse(iterator2.hasNext());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean assertStreamEquals(DoubleStream stream1, DoubleStream stream2) {
|
||||
|
||||
Iterator iterator1 = stream1.iterator();
|
||||
Iterator iterator2 = stream2.iterator();
|
||||
|
||||
while (iterator1.hasNext()) {
|
||||
Assert.assertEquals(iterator1.next(), iterator2.next());
|
||||
}
|
||||
|
||||
Assert.assertFalse(iterator2.hasNext());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean assertStreamEquals(IntStream stream1, IntStream stream2) {
|
||||
|
||||
Iterator iterator1 = stream1.iterator();
|
||||
Iterator iterator2 = stream2.iterator();
|
||||
|
||||
while (iterator1.hasNext()) {
|
||||
Assert.assertEquals(iterator1.next(), iterator2.next());
|
||||
}
|
||||
|
||||
Assert.assertFalse(iterator2.hasNext());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user