JAVA-1470 Move 10 articles to libraries-4 module
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.distinct;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class DistinctWithJavaFunction {
|
||||
|
||||
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
|
||||
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
|
||||
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package com.baeldung.distinct;
|
||||
|
||||
public class Person {
|
||||
int age;
|
||||
String name;
|
||||
String email;
|
||||
|
||||
public Person(int age, String name, String email) {
|
||||
super();
|
||||
this.age = age;
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("Person [age=");
|
||||
builder.append(age);
|
||||
builder.append(", name=");
|
||||
builder.append(name);
|
||||
builder.append(", email=");
|
||||
builder.append(email);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((email == null) ? 0 : email.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Person other = (Person) obj;
|
||||
if (email == null) {
|
||||
if (other.email != null)
|
||||
return false;
|
||||
} else if (!email.equals(other.email))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
|
||||
|
||||
public class ConvertContainerToAnother {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static List convertToList() {
|
||||
UnifiedSet<String> cars = new UnifiedSet<>();
|
||||
|
||||
cars.add("Toyota");
|
||||
cars.add("Mercedes");
|
||||
cars.add("Volkswagen");
|
||||
|
||||
return cars.toList();
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Student {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private List<String> addresses;
|
||||
|
||||
public Student(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Student(String firstName, String lastName, List<String> addresses) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return this.firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return this.lastName;
|
||||
}
|
||||
|
||||
public List<String> getAddresses() {
|
||||
return addresses;
|
||||
}
|
||||
|
||||
public void setAddresses(List<String> addresses) {
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.jdeffered;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
class FilterDemo {
|
||||
|
||||
private static String modifiedMsg;
|
||||
|
||||
static String filter(String msg) {
|
||||
|
||||
Deferred<String, ?, ?> d = new DeferredObject<>();
|
||||
Promise<String, ?, ?> p = d.promise();
|
||||
Promise<String, ?, ?> filtered = p.then((result) -> {
|
||||
modifiedMsg = "Hello " + result;
|
||||
});
|
||||
|
||||
filtered.done(r -> System.out.println("filtering done"));
|
||||
|
||||
d.resolve(msg);
|
||||
return modifiedMsg;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.jdeffered;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.DonePipe;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
class PipeDemo {
|
||||
|
||||
public enum Result {
|
||||
SUCCESS, FAILURE
|
||||
};
|
||||
|
||||
private static Result status;
|
||||
|
||||
static Result validate(int num) {
|
||||
Deferred<Integer, ?, ?> d = new DeferredObject<>();
|
||||
Promise<Integer, ?, ?> p = d.promise();
|
||||
|
||||
p.then((DonePipe<Integer, Integer, Exception, Void>) result -> {
|
||||
if (result < 90) {
|
||||
return new DeferredObject<Integer, Exception, Void>().resolve(result);
|
||||
} else {
|
||||
return new DeferredObject<Integer, Exception, Void>().reject(new Exception("Unacceptable value"));
|
||||
}
|
||||
}).done(r -> status = Result.SUCCESS).fail(r -> status = Result.FAILURE);
|
||||
|
||||
d.resolve(num);
|
||||
|
||||
return status;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.jdeffered;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
class PromiseDemo {
|
||||
|
||||
static void startJob(String jobName) {
|
||||
|
||||
Deferred<String, String, String> deferred = new DeferredObject<>();
|
||||
Promise<String, String, String> promise = deferred.promise();
|
||||
|
||||
promise.done(result -> System.out.println("Job done")).fail(rejection -> System.out.println("Job fail")).progress(progress -> System.out.println("Job is in progress")).always((state, result, rejection) -> System.out.println("Job execution started"));
|
||||
|
||||
deferred.resolve(jobName);
|
||||
// deferred.notify("");
|
||||
// deferred.reject("oops");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.jdeffered;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.DeferredManager;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DefaultDeferredManager;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
public class ThreadSafeDemo {
|
||||
|
||||
public static void task() {
|
||||
DeferredManager dm = new DefaultDeferredManager();
|
||||
Deferred<String, String, String> deferred = new DeferredObject<>();
|
||||
Promise<String, String, String> p1 = deferred.promise();
|
||||
Promise<String, String, String> p = dm.when(p1).done(r -> System.out.println("done")).fail(r -> System.out.println("fail"));
|
||||
|
||||
synchronized (p) {
|
||||
while (p.isPending()) {
|
||||
try {
|
||||
p.wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
p.waitSafely();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
deferred.resolve("Hello Baeldung");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.jdeffered.manager;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.DeferredManager;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DefaultDeferredManager;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
class DeferredManagerDemo {
|
||||
|
||||
public static void initiate() {
|
||||
Deferred<String, String, String> deferred = new DeferredObject<>();
|
||||
DeferredManager dm = new DefaultDeferredManager();
|
||||
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
|
||||
dm.when(p1, p2, p3).done((result) -> {
|
||||
System.out.println("done");
|
||||
}).fail((result) -> {
|
||||
System.out.println("fail");
|
||||
});
|
||||
deferred.resolve("Hello Baeldung");
|
||||
}
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.jdeffered.manager;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.DeferredManager;
|
||||
import org.jdeferred.Promise;
|
||||
import org.jdeferred.impl.DefaultDeferredManager;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
|
||||
class DeferredManagerWithExecutorDemo {
|
||||
|
||||
public static void initiate() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
Deferred<String, String, String> deferred = new DeferredObject<>();
|
||||
DeferredManager dm = new DefaultDeferredManager(executor);
|
||||
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
|
||||
dm.when(p1, p2, p3).done(r -> System.out.println("done")).fail(r -> System.out.println("fail"));
|
||||
deferred.resolve("done");
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.jdeffered.manager;
|
||||
|
||||
import org.jdeferred.DeferredManager;
|
||||
import org.jdeferred.impl.DefaultDeferredManager;
|
||||
|
||||
class SimpleDeferredManagerDemo {
|
||||
|
||||
public static void initiate() {
|
||||
DeferredManager dm = new DefaultDeferredManager();
|
||||
dm.when(() -> 1).done(r -> System.out.println("done")).fail(Throwable::printStackTrace);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.mbassador;
|
||||
|
||||
public class AckMessage extends Message {
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.mbassador;
|
||||
|
||||
public class Message {
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.mbassador;
|
||||
|
||||
public class RejectMessage extends Message {
|
||||
|
||||
int code;
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.noexception;
|
||||
|
||||
import com.machinezoo.noexception.ExceptionHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CustomExceptionHandler extends ExceptionHandler {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
|
||||
|
||||
@Override
|
||||
public boolean handle(Throwable throwable) {
|
||||
|
||||
if (throwable.getClass().isAssignableFrom(RuntimeException.class) || throwable.getClass().isAssignableFrom(Error.class)) {
|
||||
return false;
|
||||
} else {
|
||||
logger.error("Caught Exception ", throwable);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.pairs;
|
||||
|
||||
public class CustomPair {
|
||||
|
||||
private String key;
|
||||
private String value;
|
||||
|
||||
public CustomPair(String key, String value) {
|
||||
super();
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Object[] getPair() {
|
||||
return new Object[] { this.key, this.value};
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package com.baeldung.rome;
|
||||
|
||||
import com.sun.syndication.feed.synd.*;
|
||||
import com.sun.syndication.io.FeedException;
|
||||
import com.sun.syndication.io.SyndFeedInput;
|
||||
import com.sun.syndication.io.SyndFeedOutput;
|
||||
import com.sun.syndication.io.XmlReader;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class RSSRomeExample {
|
||||
|
||||
public static void main(String[] args) throws IOException, FeedException {
|
||||
SyndFeed feed = createFeed();
|
||||
addEntryToFeed(feed);
|
||||
publishFeed(feed);
|
||||
readFeed();
|
||||
}
|
||||
|
||||
private static SyndFeed createFeed() {
|
||||
SyndFeed feed = new SyndFeedImpl();
|
||||
feed.setFeedType("rss_1.0");
|
||||
feed.setTitle("Test title");
|
||||
feed.setLink("http://www.somelink.com");
|
||||
feed.setDescription("Basic description");
|
||||
|
||||
return feed;
|
||||
}
|
||||
|
||||
private static void addEntryToFeed(SyndFeed feed) {
|
||||
SyndEntry entry = new SyndEntryImpl();
|
||||
entry.setTitle("Entry title");
|
||||
entry.setLink("http://www.somelink.com/entry1");
|
||||
|
||||
addDescriptionToEntry(entry);
|
||||
addCategoryToEntry(entry);
|
||||
|
||||
feed.setEntries(Arrays.asList(entry));
|
||||
}
|
||||
|
||||
private static void addDescriptionToEntry(SyndEntry entry) {
|
||||
SyndContent description = new SyndContentImpl();
|
||||
description.setType("text/html");
|
||||
description.setValue("First entry");
|
||||
|
||||
entry.setDescription(description);
|
||||
}
|
||||
|
||||
private static void addCategoryToEntry(SyndEntry entry) {
|
||||
List<SyndCategory> categories = new ArrayList<>();
|
||||
SyndCategory category = new SyndCategoryImpl();
|
||||
category.setName("Sophisticated category");
|
||||
categories.add(category);
|
||||
|
||||
entry.setCategories(categories);
|
||||
}
|
||||
|
||||
private static void publishFeed(SyndFeed feed) throws IOException, FeedException {
|
||||
Writer writer = new FileWriter("xyz.txt");
|
||||
SyndFeedOutput syndFeedOutput = new SyndFeedOutput();
|
||||
syndFeedOutput.output(feed, writer);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private static SyndFeed readFeed() throws IOException, FeedException {
|
||||
URL feedSource = new URL("http://rssblog.whatisrss.com/feed/");
|
||||
SyndFeedInput input = new SyndFeedInput();
|
||||
return input.build(new XmlReader(feedSource));
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.yarg;
|
||||
|
||||
import com.haulmont.yarg.formatters.factory.DefaultFormatterFactory;
|
||||
import com.haulmont.yarg.loaders.factory.DefaultLoaderFactory;
|
||||
import com.haulmont.yarg.loaders.impl.JsonDataLoader;
|
||||
import com.haulmont.yarg.reporting.Reporting;
|
||||
import com.haulmont.yarg.reporting.RunParams;
|
||||
import com.haulmont.yarg.structure.Report;
|
||||
import com.haulmont.yarg.structure.ReportBand;
|
||||
import com.haulmont.yarg.structure.ReportOutputType;
|
||||
import com.haulmont.yarg.structure.impl.BandBuilder;
|
||||
import com.haulmont.yarg.structure.impl.ReportBuilder;
|
||||
import com.haulmont.yarg.structure.impl.ReportTemplateBuilder;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class DocumentController {
|
||||
|
||||
@RequestMapping(path = "/generate/doc", method = RequestMethod.GET)
|
||||
public void generateDocument(HttpServletResponse response) throws IOException {
|
||||
ReportBuilder reportBuilder = new ReportBuilder();
|
||||
ReportTemplateBuilder reportTemplateBuilder = new ReportTemplateBuilder().documentPath("./src/main/resources/Letter.docx").documentName("Letter.docx").outputType(ReportOutputType.docx).readFileFromPath();
|
||||
reportBuilder.template(reportTemplateBuilder.build());
|
||||
BandBuilder bandBuilder = new BandBuilder();
|
||||
String json = FileUtils.readFileToString(new File("./src/main/resources/Data.json"));
|
||||
ReportBand main = bandBuilder.name("Main").query("Main", "parameter=param1 $.main", "json").build();
|
||||
reportBuilder.band(main);
|
||||
Report report = reportBuilder.build();
|
||||
|
||||
Reporting reporting = new Reporting();
|
||||
reporting.setFormatterFactory(new DefaultFormatterFactory());
|
||||
reporting.setLoaderFactory(new DefaultLoaderFactory().setJsonDataLoader(new JsonDataLoader()));
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||
reporting.runReport(new RunParams(report).param("param1", json), response.getOutputStream());
|
||||
}
|
||||
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.distinct;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.collections.impl.block.factory.HashingStrategies;
|
||||
import org.eclipse.collections.impl.utility.ListIterate;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DistinctWithEclipseCollectionsUnitTest {
|
||||
List<Person> personList;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
personList = PersonDataGenerator.getPersonListWithFakeValues();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListByName_thenSizeShouldBe4() {
|
||||
List<Person> personListFiltered = ListIterate.distinct(personList, HashingStrategies.fromFunction(Person::getName));
|
||||
assertTrue(personListFiltered.size() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListByAge_thenSizeShouldBe2() {
|
||||
List<Person> personListFiltered = ListIterate.distinct(personList, HashingStrategies.fromIntFunction(Person::getAge));
|
||||
assertTrue(personListFiltered.size() == 2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.distinct;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static com.baeldung.distinct.DistinctWithJavaFunction.distinctByKey;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DistinctWithJavaFunctionUnitTest {
|
||||
List<Person> personList;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
personList = PersonDataGenerator.getPersonListWithFakeValues();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListByName_thenSizeShouldBe4() {
|
||||
List<Person> personListFiltered = personList.stream().filter(distinctByKey(p -> p.getName())).collect(Collectors.toList());
|
||||
assertTrue(personListFiltered.size() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListByAge_thenSizeShouldBe2() {
|
||||
List<Person> personListFiltered = personList.stream().filter(distinctByKey(p -> p.getAge())).collect(Collectors.toList());
|
||||
assertTrue(personListFiltered.size() == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListWithDefaultDistinct_thenSizeShouldBe5() {
|
||||
List<Person> personListFiltered = personList.stream().distinct().collect(Collectors.toList());
|
||||
assertTrue(personListFiltered.size() == 5);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.distinct;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import one.util.streamex.StreamEx;
|
||||
|
||||
public class DistinctWithStreamexUnitTest {
|
||||
List<Person> personList;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
personList = PersonDataGenerator.getPersonListWithFakeValues();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListByName_thenSizeShouldBe4() {
|
||||
List<Person> personListFiltered = StreamEx.of(personList).distinct(Person::getName).toList();
|
||||
assertTrue(personListFiltered.size() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListByAge_thenSizeShouldBe2() {
|
||||
List<Person> personListFiltered = StreamEx.of(personList).distinct(Person::getAge).toList();
|
||||
assertTrue(personListFiltered.size() == 2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.distinct;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DistinctWithVavrUnitTest {
|
||||
List<Person> personList;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
personList = PersonDataGenerator.getPersonListWithFakeValues();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListByName_thenSizeShouldBe4() {
|
||||
List<Person> personListFiltered = io.vavr.collection.List.ofAll(personList).distinctBy(Person::getName).toJavaList();
|
||||
assertTrue(personListFiltered.size() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListByAge_thenSizeShouldBe2() {
|
||||
List<Person> personListFiltered = io.vavr.collection.List.ofAll(personList).distinctBy(Person::getAge).toJavaList();
|
||||
assertTrue(personListFiltered.size() == 2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.distinct;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PersonDataGenerator {
|
||||
|
||||
public static List<Person> getPersonListWithFakeValues() {
|
||||
// @formatter:off
|
||||
return Arrays.asList(
|
||||
new Person(20, "Jhon", "[email protected]"),
|
||||
new Person(20, "Jhon", "[email protected]"),
|
||||
new Person(20, "Jhon", "[email protected]"),
|
||||
new Person(21, "Tom", "[email protected]"),
|
||||
new Person(21, "Mark", "[email protected]"),
|
||||
new Person(20, "Julia", "[email protected]"));
|
||||
// @formatter:on
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AllSatisfyPatternUnitTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||
boolean result = list.allSatisfy(Predicates.greaterThan(0));
|
||||
|
||||
assertTrue(result);
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AnySatisfyPatternUnitTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||
boolean result = list.anySatisfy(Predicates.greaterThan(30));
|
||||
|
||||
assertTrue(result);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CollectPatternUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCollect_thenCorrect() {
|
||||
Student student1 = new Student("John", "Hopkins");
|
||||
Student student2 = new Student("George", "Adams");
|
||||
|
||||
MutableList<Student> students = FastList.newListWith(student1, student2);
|
||||
|
||||
MutableList<String> lastNames = students.collect(Student::getLastName);
|
||||
|
||||
Assertions.assertThat(lastNames).containsExactly("Hopkins", "Adams");
|
||||
}
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConvertContainerToAnotherUnitTest {
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Test
|
||||
public void whenConvertContainerToAnother_thenCorrect() {
|
||||
MutableList<String> cars = (MutableList) ConvertContainerToAnother.convertToList();
|
||||
|
||||
Assertions.assertThat(cars).containsExactlyElementsOf(FastList.newListWith("Volkswagen", "Toyota", "Mercedes"));
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DetectPatternUnitTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDetect_thenCorrect() {
|
||||
Integer result = list.detect(Predicates.greaterThan(30));
|
||||
|
||||
Assertions.assertThat(result).isEqualTo(41);
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FlatCollectUnitTest {
|
||||
|
||||
MutableList<String> addresses1;
|
||||
MutableList<String> addresses2;
|
||||
MutableList<String> addresses3;
|
||||
MutableList<String> addresses4;
|
||||
|
||||
List<String> expectedAddresses;
|
||||
MutableList<Student> students;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
String address1 = "73 Pacific St., Forest Hills, NY 11375";
|
||||
String address2 = "93 Bayport Ave., South Richmond Hill, NY 11419";
|
||||
String address3 = "548 Market St, San Francisco, CA 94104";
|
||||
String address4 = "8605 Santa Monica Blvd, West Hollywood, CA 90069";
|
||||
|
||||
this.addresses1 = FastList.newListWith(address1, address2);
|
||||
this.addresses2 = FastList.newListWith(address3, address4);
|
||||
Student student1 = new Student("John", "Hopkins", addresses1);
|
||||
Student student2 = new Student("George", "Adams", addresses2);
|
||||
this.addresses2 = FastList.newListWith(address3, address4);
|
||||
this.students = FastList.newListWith(student1, student2);
|
||||
this.expectedAddresses = new ArrayList<>();
|
||||
this.expectedAddresses.add("73 Pacific St., Forest Hills, NY 11375");
|
||||
this.expectedAddresses.add("93 Bayport Ave., South Richmond Hill, NY 11419");
|
||||
this.expectedAddresses.add("548 Market St, San Francisco, CA 94104");
|
||||
this.expectedAddresses.add("8605 Santa Monica Blvd, West Hollywood, CA 90069");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFlatCollect_thenCorrect() {
|
||||
MutableList<String> addresses = students.flatCollect(Student::getAddresses);
|
||||
|
||||
Assertions.assertThat(addresses).containsExactlyElementsOf(this.expectedAddresses);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.eclipse.collections.api.tuple.Pair;
|
||||
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
|
||||
import org.eclipse.collections.impl.tuple.Tuples;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ForEachPatternUnitTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void whenInstantiateAndChangeValues_thenCorrect() {
|
||||
Pair<Integer, String> pair1 = Tuples.pair(1, "One");
|
||||
Pair<Integer, String> pair2 = Tuples.pair(2, "Two");
|
||||
Pair<Integer, String> pair3 = Tuples.pair(3, "Three");
|
||||
|
||||
UnifiedMap<Integer, String> map = UnifiedMap.newMapWith(pair1, pair2, pair3);
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
map.put(i + 1, "New Value");
|
||||
}
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
assertEquals("New Value", map.get(i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user