Merge branch 'master' into core-java-move-1

This commit is contained in:
Loredana Crusoveanu
2019-09-14 11:45:57 +03:00
committed by GitHub
444 changed files with 1960 additions and 670 deletions

View File

@@ -42,7 +42,7 @@ public class ListInitializationUnitTest {
}
@Test
public void givenArrayAsList_whenCreated_thenShareReference() {
public void givenArraysAsList_whenCreated_thenShareReference() {
String[] array = { "foo", "bar" };
List<String> list = Arrays.asList(array);
array[0] = "baz";

View File

@@ -0,0 +1,26 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

View File

@@ -0,0 +1,7 @@
=========
## Core Java Concurrency 2 Examples
### Relevant Articles:
- [Using a Mutex Object in Java](https://www.baeldung.com/java-mutex)

View File

@@ -0,0 +1,27 @@
<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>core-java-concurrency-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-concurrency-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<build>
<finalName>core-java-concurrency-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@@ -1,4 +1,4 @@
package com.baeldung.file;
package com.baeldung.readfile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

View File

@@ -0,0 +1,33 @@
package com.baeldung.systemgc;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import static java.util.UUID.randomUUID;
public class DemoApplication {
private static final Map<String, String> cache = new HashMap<String, String>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
final String next = scanner.next();
if ("fill".equals(next)) {
for (int i = 0; i < 1000000; i++) {
cache.put(randomUUID().toString(), randomUUID().toString());
}
} else if ("invalidate".equals(next)) {
cache.clear();
} else if ("gc".equals(next)) {
System.gc();
} else if ("exit".equals(next)) {
System.exit(0);
} else {
System.out.println("unknown");
}
}
}
}

View File

@@ -7,3 +7,9 @@
- [Cannot Reference “X” Before Supertype Constructor Has Been Called](https://www.baeldung.com/java-cannot-reference-x-before-supertype-constructor-error)
- [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
- [Raw Types in Java](https://www.baeldung.com/raw-types-java)
- [Java private Access Modifier](https://www.baeldung.com/java-private-keyword)
- [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces)
- [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts)
- [Immutable Objects in Java](http://www.baeldung.com/java-immutable-object)
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](http://www.baeldung.com/java-inheritance-composition)
- [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors)

View File

@@ -15,16 +15,26 @@
</parent>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>${equalsverifier.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<h2.version>1.4.199</h2.version>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
<equalsverifier.version>3.0.3</equalsverifier.version>
</properties>
<build>

View File

@@ -0,0 +1,37 @@
package com.baeldung.basicmethods;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class PersonName {
public String getName(String firstName, String lastName) throws RuntimeException {
return firstName + " " + lastName;
}
public String getName(String firstName, String middleName, String lastName) {
if (!middleName.equals("")) {
return firstName + " " + lastName;
}
return firstName + " " + middleName + " " + lastName;
}
public void printFullName(String firstName, String lastName) {
System.out.println(firstName + " " + lastName);
}
public void writeName(String name) throws IOException {
PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"));
out.println("Name: " + name);
out.close();
}
public static String getNameStatic(String firstName, String lastName) {
return firstName + " " + lastName;
}
public static void callToStaticMethod() {
System.out.println("Name is: " + PersonName.getNameStatic("Alan", "Turing"));
}
}

View File

@@ -1,12 +1,6 @@
package com.baeldung.inheritancecomposition.application;
import com.baeldung.inheritancecomposition.model.Actress;
import com.baeldung.inheritancecomposition.model.Computer;
import com.baeldung.inheritancecomposition.model.StandardMemory;
import com.baeldung.inheritancecomposition.model.Person;
import com.baeldung.inheritancecomposition.model.StandardProcessor;
import com.baeldung.inheritancecomposition.model.StandardSoundCard;
import com.baeldung.inheritancecomposition.model.Waitress;
import com.baeldung.inheritancecomposition.model.*;
public class Application {

View File

@@ -1,11 +1,9 @@
package com.baeldung.constructors;
import com.google.common.collect.Comparators;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.logging.Logger;
import static org.assertj.core.api.Assertions.*;

View File

@@ -1,10 +1,10 @@
package com.baeldung.equalshashcode;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class MoneyUnitTest {
@Test

View File

@@ -1,14 +1,13 @@
package com.baeldung.equalshashcode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import nl.jqno.equalsverifier.EqualsVerifier;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
public class TeamUnitTest {

View File

@@ -1,13 +1,11 @@
package com.baeldung.equalshashcode.entities;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.equalshashcode.entities.ComplexClass;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class ComplexClassUnitTest {

View File

@@ -1,11 +1,9 @@
package com.baeldung.equalshashcode.entities;
import java.awt.Color;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.equalshashcode.entities.Square;
import java.awt.*;
public class SquareClassUnitTest {

View File

@@ -1,11 +1,11 @@
package com.baeldung.immutableobjects;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ImmutableObjectsUnitTest {

View File

@@ -1,8 +1,9 @@
package com.baeldung.inheritancecomposition.test;
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.Actress;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class ActressUnitTest {

View File

@@ -1,15 +1,12 @@
package com.baeldung.inheritancecomposition.test;
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.Computer;
import com.baeldung.inheritancecomposition.model.Memory;
import com.baeldung.inheritancecomposition.model.Processor;
import com.baeldung.inheritancecomposition.model.StandardMemory;
import com.baeldung.inheritancecomposition.model.StandardProcessor;
import com.baeldung.inheritancecomposition.model.StandardSoundCard;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import com.baeldung.inheritancecomposition.model.*;
import org.junit.Test;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
public class CompositionUnitTest {

View File

@@ -1,11 +1,12 @@
package com.baeldung.inheritancecomposition.test;
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.Actress;
import com.baeldung.inheritancecomposition.model.Person;
import com.baeldung.inheritancecomposition.model.Waitress;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class InheritanceUnitTest {
@Test

View File

@@ -1,8 +1,9 @@
package com.baeldung.inheritancecomposition.test;
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.Person;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class PersonUnitTest {

View File

@@ -1,8 +1,9 @@
package com.baeldung.inheritancecomposition.test;
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.Waitress;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class WaitressUnitTest {

View File

@@ -1,9 +1,9 @@
package com.baeldung.markerinterface;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MarkerInterfaceUnitTest {
@Test

View File

@@ -0,0 +1,26 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

View File

@@ -0,0 +1,11 @@
=========
## Core Java Lang OOP 3 Cookbooks and Examples
### Relevant Articles:
- [Pass-By-Value as a Parameter Passing Mechanism in Java](http://www.baeldung.com/java-pass-by-value-or-pass-by-reference)
- [Access Modifiers in Java](http://www.baeldung.com/java-access-modifiers)
- [Guide to the super Java Keyword](http://www.baeldung.com/java-super)
- [Guide to the this Java Keyword](http://www.baeldung.com/java-this)
- [Java Public Access Modifier](https://www.baeldung.com/java-public-keyword)
- [Composition, Aggregation and Association in Java](https://www.baeldung.com/java-composition-aggregation-association)

View File

@@ -0,0 +1,26 @@
<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>core-java-lang-oop-3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-lang-oop-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,14 +1,11 @@
package com.baeldung.accessmodifiers;
import com.baeldung.accessmodifiers.publicmodifier.ListOfThree;
import com.baeldung.accessmodifiers.publicmodifier.Student;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import com.baeldung.accessmodifiers.publicmodifier.ListOfThree;
import com.baeldung.accessmodifiers.publicmodifier.Student;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -17,6 +14,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestInstance(Lifecycle.PER_CLASS)
public class PublicAccessModifierUnitTest {

View File

@@ -12,13 +12,4 @@
- [Object Type Casting in Java](http://www.baeldung.com/java-type-casting)
- [The “final” Keyword in Java](http://www.baeldung.com/java-final)
- [Type Erasure in Java Explained](http://www.baeldung.com/java-type-erasure)
- [Pass-By-Value as a Parameter Passing Mechanism in Java](http://www.baeldung.com/java-pass-by-value-or-pass-by-reference)
- [Variable and Method Hiding in Java](http://www.baeldung.com/java-variable-method-hiding)
- [Access Modifiers in Java](http://www.baeldung.com/java-access-modifiers)
- [Guide to the super Java Keyword](http://www.baeldung.com/java-super)
- [Guide to the this Java Keyword](http://www.baeldung.com/java-this)
- [Immutable Objects in Java](http://www.baeldung.com/java-immutable-object)
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](http://www.baeldung.com/java-inheritance-composition)
- [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors)
- [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts)
- [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces)

View File

@@ -56,12 +56,6 @@
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>${equalsverifier.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -78,7 +72,6 @@
<gson.version>2.8.2</gson.version>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
<equalsverifier.version>3.0.3</equalsverifier.version>
</properties>
</project>

View File

@@ -3,3 +3,5 @@
- [Checking if a URL Exists in Java](https://www.baeldung.com/java-check-url-exists)
- [Making a JSON POST Request With HttpURLConnection](https://www.baeldung.com/httpurlconnection-post)
- [Using Curl in Java](https://www.baeldung.com/java-curl)
- [Do a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
- [Sending Emails with Java](http://www.baeldung.com/java-email)

View File

@@ -12,9 +12,23 @@
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${javax.mail.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-networking-2</finalName>
</build>
</build>
<properties>
<javax.mail.version>1.5.0-b01</javax.mail.version>
</properties>
</project>

View File

@@ -1,15 +1,12 @@
package com.baeldung.mail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.util.Properties;
public class EmailService {

View File

@@ -3,14 +3,7 @@ package com.baeldung.httprequest;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import com.baeldung.httprequest.FullResponseBuilder;
import com.baeldung.httprequest.ParameterStringBuilder;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;

View File

@@ -7,12 +7,10 @@
- [Connecting Through Proxy Servers in Core Java](https://www.baeldung.com/java-connect-via-proxy-server)
- [Broadcasting and Multicasting in Java](http://www.baeldung.com/java-broadcast-multicast)
- [A Guide To UDP In Java](http://www.baeldung.com/udp-in-java)
- [Sending Emails with Java](http://www.baeldung.com/java-email)
- [A Guide To HTTP Cookies In Java](http://www.baeldung.com/cookies-java)
- [A Guide to the Java URL](http://www.baeldung.com/java-url)
- [Working with Network Interfaces in Java](http://www.baeldung.com/java-network-interfaces)
- [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets)
- [Guide to Java URL Encoding/Decoding](http://www.baeldung.com/java-url-encoding-decoding)
- [Do a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
- [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri)
- [Read an InputStream using the Java Server Socket](https://www.baeldung.com/java-inputstream-server-socket)

View File

@@ -14,11 +14,6 @@
</parent>
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${javax.mail.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@@ -29,11 +24,6 @@
<artifactId>spring-web</artifactId>
<version>${springframework.spring-web.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<build>
@@ -41,7 +31,6 @@
</build>
<properties>
<javax.mail.version>1.5.0-b01</javax.mail.version>
<springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version>
</properties>
</project>

View File

@@ -0,0 +1,46 @@
package com.baeldung.jsonposturlconnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class PostJSONWithHttpURLConnection {
public static void main (String []args) throws IOException{
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
URL url = new URL ("https://reqres.in/api/users");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
//JSON String need to be constructed for the specific resource.
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
try(OutputStream os = con.getOutputStream()){
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
int code = con.getResponseCode();
System.out.println(code);
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
}
}
}