Corrected Indentation using formatter. (#1541)
This commit is contained in:
parent
e28dfe6a4a
commit
ce589cc7b5
@ -6,19 +6,18 @@ public class AtomicLongMapTutorials {
|
|||||||
|
|
||||||
private AtomicLongMap<String> atomicLongMap;
|
private AtomicLongMap<String> atomicLongMap;
|
||||||
|
|
||||||
public AtomicLongMapTutorials(){
|
public AtomicLongMapTutorials() {
|
||||||
atomicLongMap = AtomicLongMap.create();
|
atomicLongMap = AtomicLongMap.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addKeys() {
|
||||||
public void addKeys(){
|
atomicLongMap.addAndGet("apple", 250);
|
||||||
atomicLongMap.addAndGet("apple",250);
|
atomicLongMap.addAndGet("bat", 350);
|
||||||
atomicLongMap.addAndGet("bat",350);
|
atomicLongMap.addAndGet("cat", 450);
|
||||||
atomicLongMap.addAndGet("cat",450);
|
atomicLongMap.addAndGet("dog", 550);
|
||||||
atomicLongMap.addAndGet("dog",550);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args) {
|
||||||
AtomicLongMapTutorials atomicLongMapTutorials = new AtomicLongMapTutorials();
|
AtomicLongMapTutorials atomicLongMapTutorials = new AtomicLongMapTutorials();
|
||||||
atomicLongMapTutorials.addKeys();
|
atomicLongMapTutorials.addKeys();
|
||||||
|
|
||||||
|
@ -8,12 +8,10 @@ import java.util.List;
|
|||||||
|
|
||||||
public class ComparatorsExamples {
|
public class ComparatorsExamples {
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args) {
|
||||||
|
|
||||||
List<Integer> integers = Arrays.asList(1,2,3,4,4,6,7,8,9,10);
|
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 4, 6, 7, 8, 9, 10);
|
||||||
//This will return true
|
|
||||||
boolean isInAscendingOrder = Comparators.isInOrder(integers, new AscedingOrderComparator());
|
boolean isInAscendingOrder = Comparators.isInOrder(integers, new AscedingOrderComparator());
|
||||||
|
|
||||||
System.out.println(isInAscendingOrder);
|
System.out.println(isInAscendingOrder);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import com.google.common.collect.Streams;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class ConcatStreams {
|
public class ConcatStreams {
|
||||||
public static Stream concatStreams(Stream stream1, Stream stream2, Stream stream3){
|
public static Stream concatStreams(Stream stream1, Stream stream2, Stream stream3) {
|
||||||
return Streams.concat(stream1,stream2,stream3);
|
return Streams.concat(stream1, stream2, stream3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,12 @@ package com.baeldung.guava.tutorial;
|
|||||||
import com.google.common.collect.Interner;
|
import com.google.common.collect.Interner;
|
||||||
import com.google.common.collect.Interners;
|
import com.google.common.collect.Interners;
|
||||||
|
|
||||||
import static com.google.common.collect.Interners.newBuilder;
|
|
||||||
|
|
||||||
public class InternerBuilderExample {
|
public class InternerBuilderExample {
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args) {
|
||||||
Interner<Integer> interners = Interners.<Integer>newBuilder()
|
Interner<Integer> interners = Interners.<Integer> newBuilder()
|
||||||
.concurrencyLevel(2)
|
.concurrencyLevel(2)
|
||||||
.strong()
|
.strong().<Integer> build();
|
||||||
.<Integer>build();
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import com.google.common.util.concurrent.Monitor;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.BooleanSupplier;
|
|
||||||
|
|
||||||
public class MonitorExample {
|
public class MonitorExample {
|
||||||
private List<String> students = new ArrayList<String>();
|
private List<String> students = new ArrayList<String>();
|
||||||
@ -12,7 +11,6 @@ public class MonitorExample {
|
|||||||
|
|
||||||
private Monitor monitor = new Monitor();
|
private Monitor monitor = new Monitor();
|
||||||
|
|
||||||
|
|
||||||
public void addToCourse(String item) throws InterruptedException {
|
public void addToCourse(String item) throws InterruptedException {
|
||||||
Monitor.Guard studentsBelowCapacity = monitor.newGuard(this::isStudentsCapacityUptoLimit);
|
Monitor.Guard studentsBelowCapacity = monitor.newGuard(this::isStudentsCapacityUptoLimit);
|
||||||
monitor.enterWhen(studentsBelowCapacity);
|
monitor.enterWhen(studentsBelowCapacity);
|
||||||
@ -23,7 +21,7 @@ public class MonitorExample {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean isStudentsCapacityUptoLimit(){
|
public Boolean isStudentsCapacityUptoLimit() {
|
||||||
return students.size() > MAX_SIZE;
|
return students.size() > MAX_SIZE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,9 @@ public class MoreCollectorsExample {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
List<Integer> numbers = Arrays.asList(1);
|
List<Integer> numbers = Arrays.asList(1);
|
||||||
Optional<Integer> number = numbers.stream()
|
Optional<Integer> number = numbers
|
||||||
.map(e -> e * 2)
|
.stream()
|
||||||
.collect(MoreCollectors.toOptional());
|
.map(e -> e * 2)
|
||||||
|
.collect(MoreCollectors.toOptional());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,9 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
public class StreamsUtility {
|
public class StreamsUtility {
|
||||||
|
|
||||||
public static void main(String[] args){
|
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);
|
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
|
//Using Collection
|
||||||
Stream<Integer> streamFromCollection = Streams.stream(numbers);
|
Stream<Integer> streamFromCollection = Streams.stream(numbers);
|
||||||
//Using Iterator
|
//Using Iterator
|
||||||
@ -28,15 +28,12 @@ public class StreamsUtility {
|
|||||||
//Using OptionalDouble to DoubleStream
|
//Using OptionalDouble to DoubleStream
|
||||||
DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0));
|
DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0));
|
||||||
|
|
||||||
Stream<Integer> concatenatedStreams = Streams.concat(streamFromCollection,streamFromIterable,streamFromIterator);
|
Stream<Integer> concatenatedStreams = Streams.concat(streamFromCollection, streamFromIterable, streamFromIterator);
|
||||||
|
|
||||||
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
|
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||||
//This will return 10
|
//This will return 10
|
||||||
Optional<Integer> lastItem = Streams.findLast(integers.stream());
|
Optional<Integer> lastItem = Streams.findLast(integers.stream());
|
||||||
|
|
||||||
Streams.zip(
|
Streams.zip(Stream.of("candy", "chocolate", "bar"), Stream.of("$1", "$2", "$3"), (arg1, arg2) -> arg1 + ":" + arg2);
|
||||||
Stream.of("candy", "chocolate", "bar"),
|
|
||||||
Stream.of("$1", "$2","$3"),
|
|
||||||
(arg1, arg2) -> arg1 + ":" + arg2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import com.google.common.util.concurrent.AtomicLongMap;
|
import com.google.common.util.concurrent.AtomicLongMap;
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
@ -12,35 +11,28 @@ public class AtomicLongMapTests {
|
|||||||
|
|
||||||
AtomicLongMap<String> courses = AtomicLongMap.create();
|
AtomicLongMap<String> courses = AtomicLongMap.create();
|
||||||
|
|
||||||
public void setUp(){
|
public void setUp() {
|
||||||
courses.put(SPRING_COURSE_KEY, 1056);
|
courses.put(SPRING_COURSE_KEY, 1056);
|
||||||
courses.put(HIBERNATE_COURSE_KEY, 259);
|
courses.put(HIBERNATE_COURSE_KEY, 259);
|
||||||
courses.put(GUAVA_COURSE_KEY, 78);
|
courses.put(GUAVA_COURSE_KEY, 78);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void accumulateAndGet_withLongBinaryOperator_thenSuccessful() {
|
||||||
|
long noOfStudents = 56;
|
||||||
|
long oldValue = courses.get(SPRING_COURSE_KEY);
|
||||||
|
|
||||||
@Test
|
long totalNotesRequired = courses.accumulateAndGet("Guava", noOfStudents, (x, y) -> (x * y));
|
||||||
public void accumulateAndGet_withLongBinaryOperator_thenSuccessful(){
|
|
||||||
long noOfStudents = 56;
|
|
||||||
long oldValue = courses.get(SPRING_COURSE_KEY);
|
|
||||||
|
|
||||||
long totalNotesRequired = courses.accumulateAndGet(
|
assertEquals(totalNotesRequired, oldValue * noOfStudents);
|
||||||
"Guava",
|
}
|
||||||
noOfStudents,
|
|
||||||
(x,y) -> (x * y));
|
|
||||||
|
|
||||||
assertEquals(totalNotesRequired, oldValue * noOfStudents );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAndAccumulate_withLongBinaryOperator_thenSuccessful(){
|
public void getAndAccumulate_withLongBinaryOperator_thenSuccessful() {
|
||||||
long noOfStudents = 56;
|
long noOfStudents = 56;
|
||||||
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
|
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
|
||||||
|
|
||||||
long onUpdate = courses.accumulateAndGet("Guava",
|
long onUpdate = courses.accumulateAndGet("Guava", noOfStudents, (x, y) -> (x * y));
|
||||||
noOfStudents,
|
|
||||||
(x,y) -> (x * y)
|
|
||||||
);
|
|
||||||
|
|
||||||
long afterUpdate = courses.get(SPRING_COURSE_KEY);
|
long afterUpdate = courses.get(SPRING_COURSE_KEY);
|
||||||
|
|
||||||
@ -48,17 +40,15 @@ public void accumulateAndGet_withLongBinaryOperator_thenSuccessful(){
|
|||||||
assertEquals(afterUpdate, beforeUpdate * noOfStudents);
|
assertEquals(afterUpdate, beforeUpdate * noOfStudents);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateAndGet_withLongUnaryOperator_thenSuccessful(){
|
public void updateAndGet_withLongUnaryOperator_thenSuccessful() {
|
||||||
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
|
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
|
||||||
|
|
||||||
long onUpdate = courses.updateAndGet(
|
long onUpdate = courses.updateAndGet("Guava", (x) -> (x / 2));
|
||||||
"Guava",
|
|
||||||
(x) -> (x/2));
|
|
||||||
|
|
||||||
long afterUpdate = courses.get(SPRING_COURSE_KEY);
|
long afterUpdate = courses.get(SPRING_COURSE_KEY);
|
||||||
|
|
||||||
assertEquals(onUpdate, afterUpdate);
|
assertEquals(onUpdate, afterUpdate);
|
||||||
assertEquals(afterUpdate, beforeUpdate/2);
|
assertEquals(afterUpdate, beforeUpdate / 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,9 @@ import com.google.common.collect.Comparators;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.ToDoubleFunction;
|
import java.util.function.ToDoubleFunction;
|
||||||
import java.util.function.ToIntFunction;
|
import java.util.function.ToIntFunction;
|
||||||
@ -11,9 +13,9 @@ import java.util.function.ToLongFunction;
|
|||||||
public class ComparatorsUnitTests {
|
public class ComparatorsUnitTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isInOrderTest(){
|
public void isInOrderTest() {
|
||||||
|
|
||||||
List<Integer> numbers = Arrays.asList(1,2,3,4,4,6,7,8,9,10);
|
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 4, 6, 7, 8, 9, 10);
|
||||||
|
|
||||||
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
|
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
|
||||||
|
|
||||||
@ -21,17 +23,16 @@ public class ComparatorsUnitTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isInStrictOrderTest(){
|
public void isInStrictOrderTest() {
|
||||||
|
|
||||||
List<Integer> numbers = Arrays.asList(1,2,3,4,3,6,7,8,9,10);
|
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 3, 6, 7, 8, 9, 10);
|
||||||
|
|
||||||
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
|
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
|
||||||
|
|
||||||
Assert.assertFalse(isInAscendingOrder);
|
Assert.assertFalse(isInAscendingOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class AscendingOrderComparator<I extends Number> implements Comparator<Integer> {
|
||||||
private class AscendingOrderComparator<I extends Number> implements Comparator<Integer>{
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(Integer o1, Integer o2) {
|
public int compare(Integer o1, Integer o2) {
|
||||||
|
@ -14,13 +14,12 @@ public class GauavaStreamsTests {
|
|||||||
List<Integer> numbers;
|
List<Integer> numbers;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp(){
|
public void setUp() {
|
||||||
numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,11,12,13,14,15,16,17,18,19,20);
|
numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createStreamsWithCollection(){
|
public void createStreamsWithCollection() {
|
||||||
//Deprecated API to create stream from collection
|
//Deprecated API to create stream from collection
|
||||||
Stream streamFromCollection = Streams.stream(numbers);
|
Stream streamFromCollection = Streams.stream(numbers);
|
||||||
|
|
||||||
@ -29,7 +28,7 @@ public class GauavaStreamsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createStreamsWithIterable(){
|
public void createStreamsWithIterable() {
|
||||||
Iterable<Integer> numbersIterable = (Iterable<Integer>) numbers;
|
Iterable<Integer> numbersIterable = (Iterable<Integer>) numbers;
|
||||||
|
|
||||||
Stream streamFromIterable = Streams.stream(numbersIterable);
|
Stream streamFromIterable = Streams.stream(numbersIterable);
|
||||||
@ -39,7 +38,7 @@ public class GauavaStreamsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createStreamsWithIterator(){
|
public void createStreamsWithIterator() {
|
||||||
Iterator<Integer> numbersIterator = numbers.iterator();
|
Iterator<Integer> numbersIterator = numbers.iterator();
|
||||||
|
|
||||||
Stream streamFromIterator = Streams.stream(numbersIterator);
|
Stream streamFromIterator = Streams.stream(numbersIterator);
|
||||||
@ -49,7 +48,7 @@ public class GauavaStreamsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createStreamsWithOptional(){
|
public void createStreamsWithOptional() {
|
||||||
|
|
||||||
Stream streamFromOptional = Streams.stream(Optional.of(1));
|
Stream streamFromOptional = Streams.stream(Optional.of(1));
|
||||||
|
|
||||||
@ -58,7 +57,7 @@ public class GauavaStreamsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createStreamsWithOptionalLong(){
|
public void createStreamsWithOptionalLong() {
|
||||||
|
|
||||||
LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1));
|
LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1));
|
||||||
|
|
||||||
@ -67,7 +66,7 @@ public class GauavaStreamsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createStreamsWithOptionalInt(){
|
public void createStreamsWithOptionalInt() {
|
||||||
|
|
||||||
IntStream streamFromOptionalInt = Streams.stream(OptionalInt.of(1));
|
IntStream streamFromOptionalInt = Streams.stream(OptionalInt.of(1));
|
||||||
|
|
||||||
@ -76,7 +75,7 @@ public class GauavaStreamsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createStreamsWithOptionalDouble(){
|
public void createStreamsWithOptionalDouble() {
|
||||||
|
|
||||||
DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0));
|
DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0));
|
||||||
|
|
||||||
@ -86,41 +85,44 @@ public class GauavaStreamsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void concatStreamsOfSameType(){
|
public void concatStreamsOfSameType() {
|
||||||
Stream oddNumbers = Arrays.asList(1,3,5,7,9,11,13,15,17,19).stream();
|
Stream oddNumbers = Arrays
|
||||||
Stream evenNumbers = Arrays.asList(2,4,6,8,10,12,14,16,18,20).stream();
|
.asList(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
|
||||||
|
.stream();
|
||||||
|
Stream evenNumbers = Arrays
|
||||||
|
.asList(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
|
||||||
|
.stream();
|
||||||
|
|
||||||
Stream combinedStreams = Streams.concat(oddNumbers,evenNumbers);
|
Stream combinedStreams = Streams.concat(oddNumbers, evenNumbers);
|
||||||
|
|
||||||
//Assert.assertNotNull(combinedStreams);
|
//Assert.assertNotNull(combinedStreams);
|
||||||
StreamUtility.assertStreamEquals(combinedStreams, Stream.concat(oddNumbers, evenNumbers));
|
StreamUtility.assertStreamEquals(combinedStreams, Stream.concat(oddNumbers, evenNumbers));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void concatStreamsOfTypeLongStream(){
|
public void concatStreamsOfTypeLongStream() {
|
||||||
LongStream firstTwenty = LongStream.range(1,20);
|
LongStream firstTwenty = LongStream.range(1, 20);
|
||||||
LongStream nextTwenty = LongStream.range(21,40);
|
LongStream nextTwenty = LongStream.range(21, 40);
|
||||||
|
|
||||||
LongStream combinedStreams = Streams.concat(firstTwenty,nextTwenty);
|
LongStream combinedStreams = Streams.concat(firstTwenty, nextTwenty);
|
||||||
|
|
||||||
Assert.assertNotNull(combinedStreams);
|
Assert.assertNotNull(combinedStreams);
|
||||||
StreamUtility.assertStreamEquals(combinedStreams, LongStream.concat(firstTwenty, nextTwenty));
|
StreamUtility.assertStreamEquals(combinedStreams, LongStream.concat(firstTwenty, nextTwenty));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void concatStreamsOfTypeIntStream(){
|
public void concatStreamsOfTypeIntStream() {
|
||||||
IntStream firstTwenty = IntStream.range(1,20);
|
IntStream firstTwenty = IntStream.range(1, 20);
|
||||||
IntStream nextTwenty = IntStream.range(21,40);
|
IntStream nextTwenty = IntStream.range(21, 40);
|
||||||
|
|
||||||
IntStream combinedStreams = Streams.concat(firstTwenty,nextTwenty);
|
IntStream combinedStreams = Streams.concat(firstTwenty, nextTwenty);
|
||||||
|
|
||||||
Assert.assertNotNull(combinedStreams);
|
Assert.assertNotNull(combinedStreams);
|
||||||
StreamUtility.assertStreamEquals(combinedStreams, IntStream.concat(firstTwenty, nextTwenty));
|
StreamUtility.assertStreamEquals(combinedStreams, IntStream.concat(firstTwenty, nextTwenty));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findLastOfStream(){
|
public void findLastOfStream() {
|
||||||
Optional<Integer> lastElement = Streams.findLast(numbers.stream());
|
Optional<Integer> lastElement = Streams.findLast(numbers.stream());
|
||||||
|
|
||||||
Assert.assertNotNull(lastElement.get());
|
Assert.assertNotNull(lastElement.get());
|
||||||
@ -128,28 +130,29 @@ public class GauavaStreamsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mapWithIndexTest(){
|
public void mapWithIndexTest() {
|
||||||
Stream stringSream = Stream.of("a","b","c");
|
Stream stringSream = Stream.of("a", "b", "c");
|
||||||
|
|
||||||
Stream<String> mappedStream = Streams.mapWithIndex(stringSream,(str,index) -> str +":"+ index);
|
Stream<String> mappedStream = Streams.mapWithIndex(stringSream, (str, index) -> str + ":" + index);
|
||||||
|
|
||||||
//Assert.assertNotNull(mappedStream);
|
//Assert.assertNotNull(mappedStream);
|
||||||
Assert.assertEquals(mappedStream.findFirst().get(), "a:0");
|
Assert.assertEquals(mappedStream
|
||||||
|
.findFirst()
|
||||||
|
.get(), "a:0");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void streamsZipTest(){
|
public void streamsZipTest() {
|
||||||
Stream stringSream = Stream.of("a","b","c");
|
Stream stringSream = Stream.of("a", "b", "c");
|
||||||
Stream intStream = Stream.of(1,2,3);
|
Stream intStream = Stream.of(1, 2, 3);
|
||||||
Stream<String> mappedStream = Streams.zip(stringSream,intStream, (str,index) -> str +":"+ index);
|
Stream<String> mappedStream = Streams.zip(stringSream, intStream, (str, index) -> str + ":" + index);
|
||||||
|
|
||||||
//Assert.assertNotNull(mappedStream);
|
//Assert.assertNotNull(mappedStream);
|
||||||
Assert.assertEquals(mappedStream.findFirst().get(), "a:1");
|
Assert.assertEquals(mappedStream
|
||||||
|
.findFirst()
|
||||||
|
.get(), "a:1");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,11 @@ import org.junit.Test;
|
|||||||
public class InternBuilderUnitTests {
|
public class InternBuilderUnitTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void interBuilderTest(){
|
public void interBuilderTest() {
|
||||||
|
|
||||||
Interner<Integer> interners = Interners.<Integer>newBuilder()
|
Interner<Integer> interners = Interners.<Integer> newBuilder()
|
||||||
.concurrencyLevel(2)
|
.concurrencyLevel(2)
|
||||||
.strong()
|
.strong().<Integer> build();
|
||||||
.<Integer>build();
|
|
||||||
|
|
||||||
Assert.assertNotNull(interners);
|
Assert.assertNotNull(interners);
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,6 @@ import com.google.common.util.concurrent.Monitor;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
import static com.google.common.util.concurrent.Uninterruptibles.joinUninterruptibly;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
|
|
||||||
public class MonitorUnitTests {
|
public class MonitorUnitTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -19,11 +11,11 @@ public class MonitorUnitTests {
|
|||||||
|
|
||||||
Monitor.Guard gaurdCondition = monitor.newGuard(this::returnTrue);
|
Monitor.Guard gaurdCondition = monitor.newGuard(this::returnTrue);
|
||||||
|
|
||||||
if(monitor.enterIf(gaurdCondition)){
|
if (monitor.enterIf(gaurdCondition)) {
|
||||||
try{
|
try {
|
||||||
System.out.println("Entered in critical section");
|
System.out.println("Entered in critical section");
|
||||||
enteredInCriticalSection = true;
|
enteredInCriticalSection = true;
|
||||||
}finally {
|
} finally {
|
||||||
monitor.leave();
|
monitor.leave();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,11 +31,11 @@ public class MonitorUnitTests {
|
|||||||
|
|
||||||
Monitor.Guard gaurdCondition = monitor.newGuard(this::returnFalse);
|
Monitor.Guard gaurdCondition = monitor.newGuard(this::returnFalse);
|
||||||
|
|
||||||
if(monitor.enterIf(gaurdCondition)){
|
if (monitor.enterIf(gaurdCondition)) {
|
||||||
try{
|
try {
|
||||||
System.out.println("Entered in critical section");
|
System.out.println("Entered in critical section");
|
||||||
enteredInCriticalSection = true;
|
enteredInCriticalSection = true;
|
||||||
}finally {
|
} finally {
|
||||||
monitor.leave();
|
monitor.leave();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,11 +43,11 @@ public class MonitorUnitTests {
|
|||||||
Assert.assertFalse(enteredInCriticalSection);
|
Assert.assertFalse(enteredInCriticalSection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean returnTrue(){
|
private boolean returnTrue() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean returnFalse(){
|
private boolean returnFalse() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,28 +9,28 @@ import java.util.Optional;
|
|||||||
public class MoreCollectorsUnitTests {
|
public class MoreCollectorsUnitTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toOptionalTest(){
|
public void toOptionalTest() {
|
||||||
|
|
||||||
List<Integer> numbers = Arrays.asList(1);
|
List<Integer> numbers = Arrays.asList(1);
|
||||||
|
|
||||||
Optional<Integer> number = numbers.stream()
|
Optional<Integer> number = numbers
|
||||||
.map( e -> e*2)
|
.stream()
|
||||||
.collect(MoreCollectors.toOptional());
|
.map(e -> e * 2)
|
||||||
|
.collect(MoreCollectors.toOptional());
|
||||||
|
|
||||||
Assert.assertEquals(number.get(),new Integer(2));
|
Assert.assertEquals(number.get(), new Integer(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onlyElementTest(){
|
public void onlyElementTest() {
|
||||||
List<Integer> numbers = Arrays.asList(1);
|
List<Integer> numbers = Arrays.asList(1);
|
||||||
|
|
||||||
Integer number = numbers.stream()
|
Integer number = numbers
|
||||||
.map( e -> e*2)
|
.stream()
|
||||||
.collect(MoreCollectors.onlyElement());
|
.map(e -> e * 2)
|
||||||
|
.collect(MoreCollectors.onlyElement());
|
||||||
|
|
||||||
Assert.assertEquals(number,new Integer(2));
|
Assert.assertEquals(number, new Integer(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,12 +8,12 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
public class StreamUtility {
|
public class StreamUtility {
|
||||||
|
|
||||||
public static <T> boolean assertStreamEquals(Stream<T> stream1, Stream<T> stream2){
|
public static <T> boolean assertStreamEquals(Stream<T> stream1, Stream<T> stream2) {
|
||||||
|
|
||||||
Iterator<T> iterator1 = stream1.iterator();
|
Iterator<T> iterator1 = stream1.iterator();
|
||||||
Iterator<T> iterator2 = stream2.iterator();
|
Iterator<T> iterator2 = stream2.iterator();
|
||||||
|
|
||||||
while (iterator1.hasNext()){
|
while (iterator1.hasNext()) {
|
||||||
Assert.assertEquals(iterator1.next(), iterator2.next());
|
Assert.assertEquals(iterator1.next(), iterator2.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ public class StreamUtility {
|
|||||||
Iterator iterator1 = stream1.iterator();
|
Iterator iterator1 = stream1.iterator();
|
||||||
Iterator iterator2 = stream2.iterator();
|
Iterator iterator2 = stream2.iterator();
|
||||||
|
|
||||||
while (iterator1.hasNext()){
|
while (iterator1.hasNext()) {
|
||||||
Assert.assertEquals(iterator1.next(), iterator2.next());
|
Assert.assertEquals(iterator1.next(), iterator2.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ public class StreamUtility {
|
|||||||
Iterator iterator1 = stream1.iterator();
|
Iterator iterator1 = stream1.iterator();
|
||||||
Iterator iterator2 = stream2.iterator();
|
Iterator iterator2 = stream2.iterator();
|
||||||
|
|
||||||
while (iterator1.hasNext()){
|
while (iterator1.hasNext()) {
|
||||||
Assert.assertEquals(iterator1.next(), iterator2.next());
|
Assert.assertEquals(iterator1.next(), iterator2.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ public class StreamUtility {
|
|||||||
Iterator iterator1 = stream1.iterator();
|
Iterator iterator1 = stream1.iterator();
|
||||||
Iterator iterator2 = stream2.iterator();
|
Iterator iterator2 = stream2.iterator();
|
||||||
|
|
||||||
while (iterator1.hasNext()){
|
while (iterator1.hasNext()) {
|
||||||
Assert.assertEquals(iterator1.next(), iterator2.next());
|
Assert.assertEquals(iterator1.next(), iterator2.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user