Merge remote-tracking branch 'upstream/master'
This commit is contained in:
+2
-2
@@ -18,7 +18,7 @@ public class BaeldungSychronizedBlockTest {
|
||||
|
||||
IntStream.range(0, 1000)
|
||||
.forEach(count -> service.submit(synchronizedBlocks::performSynchronisedTask));
|
||||
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
||||
service.awaitTermination(500, TimeUnit.MILLISECONDS);
|
||||
|
||||
assertEquals(1000, synchronizedBlocks.getCount());
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public class BaeldungSychronizedBlockTest {
|
||||
|
||||
IntStream.range(0, 1000)
|
||||
.forEach(count -> service.submit(BaeldungSynchronizedBlocks::performStaticSyncTask));
|
||||
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
||||
service.awaitTermination(500, TimeUnit.MILLISECONDS);
|
||||
|
||||
assertEquals(1000, BaeldungSynchronizedBlocks.getStaticCount());
|
||||
}
|
||||
|
||||
+3
-1
@@ -239,6 +239,7 @@
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
@@ -415,7 +416,8 @@
|
||||
<executions>
|
||||
<execution>
|
||||
<id>run-benchmarks</id>
|
||||
<phase>integration-test</phase>
|
||||
<!-- <phase>integration-test</phase>-->
|
||||
<phase>none</phase>
|
||||
<goals>
|
||||
<goal>exec</goal>
|
||||
</goals>
|
||||
|
||||
+3
-3
@@ -8,10 +8,10 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NioEchoIntegrationTest {
|
||||
public class NioEchoLiveTest {
|
||||
|
||||
Process server;
|
||||
EchoClient client;
|
||||
private Process server;
|
||||
private EchoClient client;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException, InterruptedException {
|
||||
+2
-2
@@ -21,9 +21,9 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JdbcIntegrationTest {
|
||||
public class JdbcLiveTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(JdbcIntegrationTest.class);
|
||||
private static final Logger LOG = Logger.getLogger(JdbcLiveTest.class);
|
||||
|
||||
private Connection con;
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ import java.io.IOException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class UDPIntegrationTest {
|
||||
public class UDPLiveTest {
|
||||
private EchoClient client;
|
||||
|
||||
@Before
|
||||
+1
-1
@@ -7,7 +7,7 @@ import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BroadcastIntegrationTest {
|
||||
public class BroadcastLiveTest {
|
||||
private BroadcastingClient client;
|
||||
|
||||
@Test
|
||||
+1
-1
@@ -7,7 +7,7 @@ import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MulticastIntegrationTest {
|
||||
public class MulticastLiveTest {
|
||||
private MulticastingClient client;
|
||||
|
||||
@Test
|
||||
+1
-1
@@ -7,7 +7,7 @@ import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ScreenshotIntegrationTest {
|
||||
public class ScreenshotLiveTest {
|
||||
|
||||
private Screenshot screenshot = new Screenshot("Screenshot.jpg");
|
||||
private File file = new File("Screenshot.jpg");
|
||||
@@ -0,0 +1,74 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>geotools</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>geotools</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-shapefile</artifactId>
|
||||
<version>${geotools-shapefile.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-epsg-hsql</artifactId>
|
||||
<version>${geotools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-swing</artifactId>
|
||||
<version>${geotools-swing.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven2-repository.dev.java.net</id>
|
||||
<name>Java.net repository</name>
|
||||
<url>http://download.java.net/maven/2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>osgeo</id>
|
||||
<name>Open Source Geospatial Foundation Repository</name>
|
||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>opengeo</id>
|
||||
<name>OpenGeo Maven Repository</name>
|
||||
<url>http://repo.opengeo.org</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<inherited>true</inherited>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<geotools.version>15.2</geotools.version>
|
||||
<geotools-swing.version>15.2</geotools-swing.version>
|
||||
<geotools-shapefile.version>15.2</geotools-shapefile.version>
|
||||
</properties>
|
||||
</project>
|
||||
+47
-40
@@ -50,42 +50,10 @@ public class ShapeFile {
|
||||
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
|
||||
|
||||
Map<String, Serializable> params = new HashMap<String, Serializable>();
|
||||
params.put("url", shapeFile.toURI()
|
||||
.toURL());
|
||||
params.put("create spatial index", Boolean.TRUE);
|
||||
|
||||
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
|
||||
dataStore.createSchema(CITY);
|
||||
ShapefileDataStore dataStore = setDataStoreParams(dataStoreFactory, params, shapeFile, CITY);
|
||||
|
||||
// If you decide to use the TYPE type and create a Data Store with it,
|
||||
// You will need to uncomment this line to set the Coordinate Reference System
|
||||
// newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
|
||||
|
||||
Transaction transaction = new DefaultTransaction("create");
|
||||
|
||||
String typeName = dataStore.getTypeNames()[0];
|
||||
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
|
||||
|
||||
if (featureSource instanceof SimpleFeatureStore) {
|
||||
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
|
||||
|
||||
featureStore.setTransaction(transaction);
|
||||
try {
|
||||
featureStore.addFeatures(collection);
|
||||
transaction.commit();
|
||||
|
||||
} catch (Exception problem) {
|
||||
problem.printStackTrace();
|
||||
transaction.rollback();
|
||||
|
||||
} finally {
|
||||
transaction.close();
|
||||
}
|
||||
System.exit(0); // success!
|
||||
} else {
|
||||
System.out.println(typeName + " does not support read/write access");
|
||||
System.exit(1);
|
||||
}
|
||||
writeToFile(dataStore, collection);
|
||||
|
||||
}
|
||||
|
||||
@@ -152,7 +120,7 @@ public class ShapeFile {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static void addToLocationMap(String name, double lat, double lng, Map<String, List<Double>> locations) {
|
||||
List<Double> coordinates = new ArrayList<>();
|
||||
|
||||
@@ -163,7 +131,6 @@ public class ShapeFile {
|
||||
|
||||
private static File getNewShapeFile() {
|
||||
String filePath = new File(".").getAbsolutePath() + FILE_NAME;
|
||||
|
||||
|
||||
JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
|
||||
chooser.setDialogTitle("Save shapefile");
|
||||
@@ -176,12 +143,52 @@ public class ShapeFile {
|
||||
}
|
||||
|
||||
File shapeFile = chooser.getSelectedFile();
|
||||
if (shapeFile.equals(filePath)) {
|
||||
System.out.println("Error: cannot replace " + filePath);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
return shapeFile;
|
||||
}
|
||||
|
||||
private static ShapefileDataStore setDataStoreParams(ShapefileDataStoreFactory dataStoreFactory, Map<String, Serializable> params, File shapeFile, SimpleFeatureType CITY) throws Exception {
|
||||
params.put("url", shapeFile.toURI()
|
||||
.toURL());
|
||||
params.put("create spatial index", Boolean.TRUE);
|
||||
|
||||
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
|
||||
dataStore.createSchema(CITY);
|
||||
|
||||
return dataStore;
|
||||
}
|
||||
|
||||
private static void writeToFile(ShapefileDataStore dataStore, DefaultFeatureCollection collection) throws Exception {
|
||||
|
||||
// If you decide to use the TYPE type and create a Data Store with it,
|
||||
// You will need to uncomment this line to set the Coordinate Reference System
|
||||
// newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
|
||||
|
||||
Transaction transaction = new DefaultTransaction("create");
|
||||
|
||||
String typeName = dataStore.getTypeNames()[0];
|
||||
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
|
||||
|
||||
if (featureSource instanceof SimpleFeatureStore) {
|
||||
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
|
||||
|
||||
featureStore.setTransaction(transaction);
|
||||
try {
|
||||
featureStore.addFeatures(collection);
|
||||
transaction.commit();
|
||||
|
||||
} catch (Exception problem) {
|
||||
problem.printStackTrace();
|
||||
transaction.rollback();
|
||||
|
||||
} finally {
|
||||
transaction.close();
|
||||
}
|
||||
System.exit(0); // success!
|
||||
} else {
|
||||
System.out.println(typeName + " does not support read/write access");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -7,7 +7,7 @@ import org.geotools.feature.simple.SimpleFeatureBuilder;
|
||||
import org.junit.Test;
|
||||
import org.opengis.feature.simple.SimpleFeatureType;
|
||||
|
||||
public class GeoToolsUnitTestTest {
|
||||
public class GeoToolsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenFeatureType_whenAddLocations_returnFeatureCollection() {
|
||||
+1
-1
@@ -22,7 +22,7 @@ import com.baeldung.arquillian.Component;
|
||||
import com.baeldung.arquillian.ConvertToLowerCase;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ArquillianTest {
|
||||
public class ArquillianLiveTest {
|
||||
|
||||
@Deployment
|
||||
public static JavaArchive createDeployment() {
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
class JvmSample(text:String) {
|
||||
@JvmField
|
||||
val sampleText:String = text
|
||||
}
|
||||
|
||||
class CompanionSample {
|
||||
companion object {
|
||||
@JvmField val MAX_LIMIT = 20
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class JvmSampleTest {
|
||||
|
||||
var sample = ""
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
sample = JvmSample("Hello!").sampleText
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenField_whenCheckValue_thenMatchesValue() {
|
||||
assertTrue(sample == "Hello!")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenStaticVariable_whenCheckValue_thenMatchesValue() {
|
||||
// Sample when is treated as a static variable
|
||||
assertTrue(CompanionSample.MAX_LIMIT == 20)
|
||||
}
|
||||
}
|
||||
+10
-46
@@ -140,7 +140,7 @@
|
||||
<artifactId>unit-ri</artifactId>
|
||||
<version>1.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons.collections.version}</version>
|
||||
@@ -487,21 +487,7 @@
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-shapefile</artifactId>
|
||||
<version>${geotools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-epsg-hsql</artifactId>
|
||||
<version>${geotools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-swing</artifactId>
|
||||
<version>${geotools.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Retrofit -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
@@ -517,6 +503,11 @@
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>adapter-rxjava</artifactId>
|
||||
<version>${retrofit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>logging-interceptor</artifactId>
|
||||
<version>3.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.darwinsys</groupId>
|
||||
@@ -559,21 +550,6 @@
|
||||
<artifactId>functionaljava</artifactId>
|
||||
<version>4.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.functionaljava</groupId>
|
||||
<artifactId>functionaljava-java8</artifactId>
|
||||
<version>4.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.functionaljava</groupId>
|
||||
<artifactId>functionaljava-quickcheck</artifactId>
|
||||
<version>4.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.functionaljava</groupId>
|
||||
<artifactId>functionaljava-java-core</artifactId>
|
||||
<version>4.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.cache</groupId>
|
||||
<artifactId>cache-api</artifactId>
|
||||
@@ -591,19 +567,6 @@
|
||||
<name>Java.net repository</name>
|
||||
<url>http://download.java.net/maven/2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>osgeo</id>
|
||||
<name>Open Source Geospatial Foundation Repository</name>
|
||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>opengeo</id>
|
||||
<name>OpenGeo Maven Repository</name>
|
||||
<url>http://repo.opengeo.org</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
@@ -657,11 +620,12 @@
|
||||
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
||||
<streamex.version>0.6.5</streamex.version>
|
||||
<vavr.version>0.9.0</vavr.version>
|
||||
<geotools.version>15.2</geotools.version>
|
||||
<joda-time.version>2.9.9</joda-time.version>
|
||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||
<retrofit.version>2.3.0</retrofit.version>
|
||||
<joda-time.version>2.9.9</joda-time.version>
|
||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||
<protonpack.version>1.14</protonpack.version>
|
||||
<protonpack.version>1.14</protonpack.version>
|
||||
<unit-ri.version>1.0.3</unit-ri.version>
|
||||
<cache.version>1.0.0</cache.version>
|
||||
<hazelcast.version>3.8.4</hazelcast.version>
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.retrofitguide;
|
||||
|
||||
import java.io.IOException;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class GitHubServiceGenerator {
|
||||
|
||||
private static final String BASE_URL = "https://api.github.com/";
|
||||
|
||||
private static Retrofit.Builder builder
|
||||
= new Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create());
|
||||
|
||||
private static Retrofit retrofit = builder.build();
|
||||
|
||||
private static OkHttpClient.Builder httpClient
|
||||
= new OkHttpClient.Builder();
|
||||
|
||||
private static HttpLoggingInterceptor logging
|
||||
= new HttpLoggingInterceptor()
|
||||
.setLevel(HttpLoggingInterceptor.Level.BASIC);
|
||||
|
||||
public static <S> S createService(Class<S> serviceClass) {
|
||||
if (!httpClient.interceptors().contains(logging)) {
|
||||
httpClient.addInterceptor(logging);
|
||||
builder.client(httpClient.build());
|
||||
retrofit = builder.build();
|
||||
}
|
||||
return retrofit.create(serviceClass);
|
||||
}
|
||||
|
||||
public static <S> S createService(Class<S> serviceClass, final String token) {
|
||||
if (token != null) {
|
||||
httpClient.interceptors().clear();
|
||||
httpClient.addInterceptor(new Interceptor() {
|
||||
@Override
|
||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||
Request original = chain.request();
|
||||
Request.Builder builder = original.newBuilder()
|
||||
.header("Authorization", token);
|
||||
Request request = builder.build();
|
||||
return chain.proceed(request);
|
||||
}
|
||||
});
|
||||
builder.client(httpClient.build());
|
||||
retrofit = builder.build();
|
||||
}
|
||||
return retrofit.create(serviceClass);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.retrofitguide;
|
||||
|
||||
import java.io.IOException;
|
||||
import okhttp3.OkHttpClient;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//Manual creation
|
||||
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl("https://api.github.com/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.client(httpClient.build())
|
||||
.build();
|
||||
UserService service = retrofit.create(UserService.class);
|
||||
//Using GitHubServiceGenerator
|
||||
service = GitHubServiceGenerator.createService(UserService.class);
|
||||
Call<User> callSync = service.getUser("eugenp");
|
||||
Call<User> callAsync = service.getUser("eugenp");
|
||||
|
||||
try {
|
||||
Response<User> response = callSync.execute();
|
||||
User user = response.body();
|
||||
System.out.println(user);
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
|
||||
// Execute the call asynchronously. Get a positive or negative callback.
|
||||
callAsync.enqueue(new Callback<User>() {
|
||||
@Override
|
||||
public void onResponse(Call<User> call, Response<User> response) {
|
||||
User user = response.body();
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<User> call, Throwable throwable) {
|
||||
System.out.println(throwable);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.retrofitguide;
|
||||
|
||||
public class User {
|
||||
|
||||
private String login;
|
||||
private long id;
|
||||
private String url;
|
||||
private String company;
|
||||
private String blog;
|
||||
private String email;
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getCompany() {
|
||||
return company;
|
||||
}
|
||||
|
||||
public void setCompany(String company) {
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
public String getBlog() {
|
||||
return blog;
|
||||
}
|
||||
|
||||
public void setBlog(String blog) {
|
||||
this.blog = blog;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "login=" + login + ", id=" + id + ", url=" + url + ", company=" + company + ", blog=" + blog + ", email=" + email + '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.retrofitguide;
|
||||
|
||||
import java.util.List;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Path;
|
||||
import retrofit2.http.Query;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
@GET("/users")
|
||||
public Call<List<User>> getUsers(@Query("per_page") int per_page, @Query("page") int page);
|
||||
|
||||
@GET("/users/{username}")
|
||||
public Call<User> getUser(@Path("username") String username);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.fj;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import fj.F;
|
||||
import fj.data.Array;
|
||||
import fj.data.List;
|
||||
import fj.data.Option;
|
||||
import fj.function.Characters;
|
||||
import fj.function.Integers;
|
||||
|
||||
public class FunctionalJavaTest {
|
||||
|
||||
public static final F<Integer, Boolean> isEven = i -> i % 2 == 0;
|
||||
|
||||
@Test
|
||||
public void calculateEvenNumbers_givenIntList_returnTrue() {
|
||||
List<Integer> fList = List.list(3, 4, 5, 6);
|
||||
List<Boolean> evenList = fList.map(isEven);
|
||||
List<Boolean> evenListTrueResult = List.list(false, true, false, true);
|
||||
List<Boolean> evenListFalseResult = List.list(true, false, false, true);
|
||||
assertEquals(evenList.equals(evenListTrueResult), true);
|
||||
assertEquals(evenList.equals(evenListFalseResult), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapList_givenIntList_returnResult() {
|
||||
List<Integer> fList = List.list(3, 4, 5, 6);
|
||||
fList = fList.map(i -> i + 100);
|
||||
List<Integer> resultList = List.list(103, 104, 105, 106);
|
||||
List<Integer> falseResultList = List.list(15, 504, 105, 106);
|
||||
assertEquals(fList.equals(resultList), true);
|
||||
assertEquals(fList.equals(falseResultList), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterList_givenIntList_returnResult() {
|
||||
Array<Integer> array = Array.array(3, 4, 5, 6);
|
||||
Array<Integer> filteredArray = array.filter(Integers.even);
|
||||
Array<Integer> result = Array.array(4, 6);
|
||||
Array<Integer> wrongResult = Array.array(3, 5);
|
||||
assertEquals(filteredArray.equals(result), true);
|
||||
assertEquals(filteredArray.equals(wrongResult), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkForLowerCase_givenStringArray_returnResult() {
|
||||
Array<String> array = Array.array("Welcome", "To", "baeldung");
|
||||
Array<String> array2 = Array.array("Welcome", "To", "Baeldung");
|
||||
Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase));
|
||||
Boolean isExist2 = array2.exists(s -> List.fromString(s).forall(Characters.isLowerCase));
|
||||
assertEquals(isExist, true);
|
||||
assertEquals(isExist2, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkOptions_givenOptions_returnResult() {
|
||||
Option<Integer> n1 = Option.some(1);
|
||||
Option<Integer> n2 = Option.some(2);
|
||||
|
||||
F<Integer, Option<Integer>> f1 = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none();
|
||||
|
||||
Option<Integer> result1 = n1.bind(f1);
|
||||
Option<Integer> result2 = n2.bind(f1);
|
||||
|
||||
assertEquals(result1, Option.none());
|
||||
assertEquals(result2, Option.some(102));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void foldLeft_givenArray_returnResult() {
|
||||
Array<Integer> intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27);
|
||||
int sum = intArray.foldLeft(Integers.add, 0);
|
||||
assertEquals(sum, 260);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -16,7 +16,7 @@ import com.baeldung.retrofit.models.Repository;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class GitHubBasicApiTest {
|
||||
public class GitHubBasicApiLiveTest {
|
||||
|
||||
GitHubBasicApi gitHub;
|
||||
|
||||
+2
-3
@@ -22,10 +22,9 @@ public class ArticleController {
|
||||
.mapToObj(i -> generateArticle("Article Title " + i))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Map<String, Object> modelMap = new HashMap<>();
|
||||
modelMap.put("articles", articles);
|
||||
model.put("articles", articles);
|
||||
|
||||
return new ModelAndView("index", modelMap);
|
||||
return new ModelAndView("index", model);
|
||||
}
|
||||
|
||||
private Article generateArticle(String title) {
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
|
||||
<!-- <module>gatling</module> --> <!-- not meant to run as part of the standard build -->
|
||||
|
||||
|
||||
<module>geotools</module>
|
||||
<module>groovy-spock</module>
|
||||
<module>gson</module>
|
||||
<module>guava</module>
|
||||
|
||||
@@ -19,6 +19,18 @@
|
||||
<artifactId>rxjava</artifactId>
|
||||
<version>${rx.java.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.reactivex.rxjava2</groupId>
|
||||
<artifactId>rxjava</artifactId>
|
||||
<version>2.1.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>1.7.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -9,7 +9,8 @@ public class ConnectableObservableImpl {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
ConnectableObservable<Long> connectable = Observable.interval(200, TimeUnit.MILLISECONDS).publish();
|
||||
ConnectableObservable<Long> connectable
|
||||
= Observable.interval(200, TimeUnit.MILLISECONDS).publish();
|
||||
connectable.subscribe(System.out::println);
|
||||
|
||||
System.out.println("Connect");
|
||||
|
||||
@@ -3,12 +3,22 @@ package com.baelding.rxjava;
|
||||
import rx.Observable;
|
||||
import rx.observables.BlockingObservable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ObservableImpl {
|
||||
|
||||
public static void main(String[] args) {
|
||||
static Integer[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
|
||||
Integer[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
|
||||
static String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};
|
||||
static String[] titles = {"title"};
|
||||
public static List<String> titleList = Arrays.asList(titles);
|
||||
|
||||
public static Observable<String> getTitle() {
|
||||
return Observable.from(titleList);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println("-------Just-----------");
|
||||
Observable<String> observable = Observable.just("Hello");
|
||||
@@ -28,14 +38,9 @@ public class ObservableImpl {
|
||||
|
||||
System.out.println();
|
||||
System.out.println("-------FlatMap-----------");
|
||||
Observable.from(letters)
|
||||
.flatMap((letter) -> {
|
||||
String[] returnStrings = {letter.toUpperCase(), letter.toLowerCase()};
|
||||
return Observable.from(returnStrings);
|
||||
})
|
||||
.subscribe(
|
||||
System.out::print
|
||||
);
|
||||
Observable.just("book1", "book2")
|
||||
.flatMap(s -> getTitle())
|
||||
.subscribe(System.out::print);
|
||||
|
||||
System.out.println();
|
||||
System.out.println("--------Scan----------");
|
||||
@@ -55,16 +60,12 @@ public class ObservableImpl {
|
||||
System.out.println("-------Filter-----------");
|
||||
Observable.from(numbers)
|
||||
.filter(i -> (i % 2 == 1))
|
||||
.subscribe(
|
||||
System.out::println
|
||||
);
|
||||
.subscribe(System.out::println);
|
||||
|
||||
System.out.println("------DefaultIfEmpty------------");
|
||||
Observable.empty()
|
||||
.defaultIfEmpty("Observable is empty")
|
||||
.subscribe(
|
||||
System.out::println
|
||||
);
|
||||
.subscribe(System.out::println);
|
||||
|
||||
System.out.println("------DefaultIfEmpty-2-----------");
|
||||
Observable.from(letters)
|
||||
|
||||
@@ -13,8 +13,9 @@ public class ResourceManagement {
|
||||
return resource;
|
||||
},
|
||||
r -> Observable.create(o -> {
|
||||
for (Character c : r.toCharArray())
|
||||
for (Character c : r.toCharArray()) {
|
||||
o.onNext(c);
|
||||
}
|
||||
o.onCompleted();
|
||||
}),
|
||||
r -> System.out.println("Disposed: " + r)
|
||||
|
||||
@@ -1,69 +1,73 @@
|
||||
package com.baelding.rxjava;
|
||||
|
||||
import rx.Observable;
|
||||
import rx.schedulers.Schedulers;
|
||||
import rx.Observer;
|
||||
import rx.subjects.PublishSubject;
|
||||
|
||||
public class SubjectImpl {
|
||||
|
||||
public static final String[] subscriber1 = {""};
|
||||
public static final String[] subscriber2 = {""};
|
||||
public static Integer subscriber1 = 0;
|
||||
public static Integer subscriber2 = 0;
|
||||
|
||||
public static String subjectMethod() throws InterruptedException {
|
||||
public static Integer subjectMethod() {
|
||||
PublishSubject<Integer> subject = PublishSubject.create();
|
||||
|
||||
String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
|
||||
Long signal = 500L;
|
||||
PublishSubject<String> subject;
|
||||
subject.subscribe(getFirstObserver());
|
||||
|
||||
synchronized (signal) {
|
||||
subject = PublishSubject.create();
|
||||
subject.subscribe(
|
||||
(letter) -> {
|
||||
subscriber1[0] += letter;
|
||||
System.out.println("Subscriber 1: " + subscriber1[0]);
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (letter.equals("c")) {
|
||||
synchronized (signal) {
|
||||
signal.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
subject.onNext(1);
|
||||
subject.onNext(2);
|
||||
subject.onNext(3);
|
||||
|
||||
Observable.from(letters)
|
||||
.subscribeOn(Schedulers.computation())
|
||||
.subscribe(
|
||||
subject::onNext,
|
||||
subject::onError,
|
||||
() -> {
|
||||
System.out.println("Subscriber 1 completed ");
|
||||
subject.onCompleted();
|
||||
synchronized (signal) {
|
||||
signal.notify();
|
||||
}
|
||||
subject.subscribe(getSecondObserver());
|
||||
|
||||
subject.onNext(4);
|
||||
subject.onCompleted();
|
||||
return subscriber1 + subscriber2;
|
||||
}
|
||||
|
||||
|
||||
public static Observer<Integer> getFirstObserver() {
|
||||
return new Observer<Integer>() {
|
||||
|
||||
@Override
|
||||
public void onNext(Integer value) {
|
||||
subscriber1 += value;
|
||||
System.out.println("Subscriber1: " + value);
|
||||
}
|
||||
);
|
||||
|
||||
synchronized (signal) {
|
||||
signal.wait();
|
||||
subject.subscribe(
|
||||
(letter) -> {
|
||||
subscriber2[0] += letter;
|
||||
System.out.println("Subscriber 2: " + subscriber2[0]);
|
||||
},
|
||||
subject::onError,
|
||||
() -> System.out.println("Subscriber 2 completed ")
|
||||
);
|
||||
}
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
System.out.println("error");
|
||||
}
|
||||
|
||||
synchronized (signal) {
|
||||
signal.wait();
|
||||
return subscriber1[0] + subscriber2[0];
|
||||
}
|
||||
@Override
|
||||
public void onCompleted() {
|
||||
System.out.println("Subscriber1 completed");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Observer<Integer> getSecondObserver() {
|
||||
return new Observer<Integer>() {
|
||||
|
||||
@Override
|
||||
public void onNext(Integer value) {
|
||||
subscriber2 += value;
|
||||
System.out.println("Subscriber2: " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
System.out.println("error");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompleted() {
|
||||
System.out.println("Subscriber2 completed");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
System.out.println(subjectMethod());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import rx.observables.ConnectableObservable;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
@@ -13,15 +14,14 @@ public class ConnectableObservableTest {
|
||||
|
||||
@Test
|
||||
public void givenConnectableObservable_whenConnect_thenGetMessage() throws InterruptedException {
|
||||
final String[] result = {""};
|
||||
ConnectableObservable<Long> connectable = Observable.interval(200, TimeUnit.MILLISECONDS).publish();
|
||||
String[] result = {""};
|
||||
ConnectableObservable<Long> connectable
|
||||
= Observable.interval(500, TimeUnit.MILLISECONDS).publish();
|
||||
connectable.subscribe(i -> result[0] += i);
|
||||
|
||||
assertFalse(result[0].equals("01"));
|
||||
|
||||
connectable.connect();
|
||||
Thread.currentThread().sleep(500);
|
||||
|
||||
assertTrue(result[0].equals("01"));
|
||||
await()
|
||||
.until(() -> assertTrue(result[0].equals("01")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,8 @@ package com.baeldung.rxjava;
|
||||
|
||||
import org.junit.Test;
|
||||
import rx.Observable;
|
||||
import rx.observables.ConnectableObservable;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.baelding.rxjava.ObservableImpl.getTitle;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
public class ObservableTest {
|
||||
@@ -24,41 +22,23 @@ public class ObservableTest {
|
||||
String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
|
||||
Observable<String> observable = Observable.from(letters);
|
||||
observable.subscribe(
|
||||
//onNext
|
||||
(i) -> {
|
||||
result += i;
|
||||
},
|
||||
//onError
|
||||
(t) -> {
|
||||
t.printStackTrace();
|
||||
},
|
||||
//onCompleted
|
||||
() -> {
|
||||
result += "Complete";
|
||||
}
|
||||
i -> result += i,
|
||||
Throwable::printStackTrace,
|
||||
() -> result += "_Complete"
|
||||
);
|
||||
assertTrue(result.equals("abcdefgComplete"));
|
||||
assertTrue(result.equals("abcdefg_Complete"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test
|
||||
public void givenArray_whenConvertsObservabletoBlockingObservable_thenReturnFirstElement() {
|
||||
String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
|
||||
Observable<String> observable = Observable.from(letters);
|
||||
String blockingObservable = observable.toBlocking().first();
|
||||
|
||||
observable.subscribe(
|
||||
//onNext
|
||||
(i) -> {
|
||||
result += i;
|
||||
},
|
||||
//onError
|
||||
(t) -> {
|
||||
t.printStackTrace();
|
||||
},
|
||||
//onCompleted
|
||||
() -> {
|
||||
result += "Complete";
|
||||
}
|
||||
i -> result += i,
|
||||
Throwable::printStackTrace,
|
||||
() -> result += "_Completed"
|
||||
);
|
||||
assertTrue(String.valueOf(result.charAt(0)).equals(blockingObservable));
|
||||
}
|
||||
@@ -68,45 +48,31 @@ public class ObservableTest {
|
||||
String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
|
||||
|
||||
Observable.from(letters)
|
||||
.map((letter) -> {
|
||||
return letter.toUpperCase();
|
||||
})
|
||||
.subscribe((letter) -> {
|
||||
result += letter;
|
||||
});
|
||||
.map(String::toUpperCase)
|
||||
.subscribe(letter -> result += letter);
|
||||
|
||||
assertTrue(result.equals("ABCDEFG"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArray_whenFlatMapAndSubscribe_thenReturnUpperAndLowerCaseLetters() {
|
||||
String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
|
||||
|
||||
Observable.from(letters)
|
||||
.flatMap((letter) -> {
|
||||
String[] returnStrings = {letter.toUpperCase(), letter.toLowerCase()};
|
||||
return Observable.from(returnStrings);
|
||||
})
|
||||
.subscribe((letter) -> {
|
||||
result += letter;
|
||||
});
|
||||
Observable.just("book1", "book2")
|
||||
.flatMap(s -> getTitle())
|
||||
.subscribe(l -> result += l);
|
||||
|
||||
assertTrue(result.equals("AaBbCcDdEeFfGg"));
|
||||
assertTrue(result.equals("titletitle"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArray_whenScanAndSubscribe_thenReturnTheSumOfAllLetters() {
|
||||
String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
|
||||
String[] letters = {"a", "b", "c"};
|
||||
|
||||
Observable.from(letters)
|
||||
.scan(new StringBuilder(), (buffer, nextLetter) -> {
|
||||
return buffer.append(nextLetter);
|
||||
})
|
||||
.subscribe((total) -> {
|
||||
result = total.toString();
|
||||
});
|
||||
.scan(new StringBuilder(), StringBuilder::append)
|
||||
.subscribe(total -> result += total.toString());
|
||||
|
||||
assertTrue(result.equals("abcdefg"));
|
||||
assertTrue(result.equals("aababc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -116,18 +82,16 @@ public class ObservableTest {
|
||||
String[] ODD = {""};
|
||||
|
||||
Observable.from(numbers)
|
||||
.groupBy((i) -> {
|
||||
return 0 == (i % 2) ? "EVEN" : "ODD";
|
||||
})
|
||||
.subscribe((group) -> {
|
||||
group.subscribe((number) -> {
|
||||
if (group.getKey().toString().equals("EVEN")) {
|
||||
EVEN[0] += number;
|
||||
} else {
|
||||
ODD[0] += number;
|
||||
}
|
||||
});
|
||||
});
|
||||
.groupBy(i -> 0 == (i % 2) ? "EVEN" : "ODD")
|
||||
.subscribe(group ->
|
||||
group.subscribe((number) -> {
|
||||
if (group.getKey().toString().equals("EVEN")) {
|
||||
EVEN[0] += number;
|
||||
} else {
|
||||
ODD[0] += number;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
assertTrue(EVEN[0].equals("0246810"));
|
||||
assertTrue(ODD[0].equals("13579"));
|
||||
@@ -138,12 +102,8 @@ public class ObservableTest {
|
||||
Integer[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
|
||||
Observable.from(numbers)
|
||||
.filter((i) -> {
|
||||
return (i % 2 == 1);
|
||||
})
|
||||
.subscribe((i) -> {
|
||||
result += i;
|
||||
});
|
||||
.filter(i -> (i % 2 == 1))
|
||||
.subscribe(i -> result += i);
|
||||
|
||||
assertTrue(result.equals("13579"));
|
||||
}
|
||||
@@ -152,10 +112,8 @@ public class ObservableTest {
|
||||
public void givenEmptyObservable_whenDefaultIfEmpty_thenGetDefaultMessage() {
|
||||
|
||||
Observable.empty()
|
||||
.defaultIfEmpty("Observable is empty")
|
||||
.subscribe((s) -> {
|
||||
result += s;
|
||||
});
|
||||
.defaultIfEmpty("Observable is empty")
|
||||
.subscribe(s -> result += s);
|
||||
|
||||
assertTrue(result.equals("Observable is empty"));
|
||||
}
|
||||
@@ -165,11 +123,9 @@ public class ObservableTest {
|
||||
String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
|
||||
|
||||
Observable.from(letters)
|
||||
.defaultIfEmpty("Observable is empty")
|
||||
.first()
|
||||
.subscribe((s) -> {
|
||||
result += s;
|
||||
});
|
||||
.defaultIfEmpty("Observable is empty")
|
||||
.first()
|
||||
.subscribe(s -> result += s);
|
||||
|
||||
assertTrue(result.equals("a"));
|
||||
}
|
||||
@@ -180,12 +136,8 @@ public class ObservableTest {
|
||||
final Integer[] sum = {0};
|
||||
|
||||
Observable.from(numbers)
|
||||
.takeWhile((i) -> {
|
||||
return i < 5;
|
||||
})
|
||||
.subscribe((s) -> {
|
||||
sum[0] += s;
|
||||
});
|
||||
.takeWhile(i -> i < 5)
|
||||
.subscribe(s -> sum[0] += s);
|
||||
|
||||
assertTrue(sum[0] == 10);
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user