fix conflicts

This commit is contained in:
Loredana
2019-05-10 23:22:52 +03:00
1803 changed files with 33727 additions and 6129 deletions

View File

@@ -0,0 +1,4 @@
target/
.idea/
bin/
*.iml

View File

@@ -0,0 +1,8 @@
=========
## Core Java Lang OOP 2 Cookbooks and Examples
### Relevant Articles:
- [Generic Constructors in Java](https://www.baeldung.com/java-generic-constructors)
- [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)

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-lang-oop-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-lang-oop-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-lang-oop-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@@ -0,0 +1,15 @@
package com.baeldung.anonymous;
public class Book {
final String title;
public Book(String title) {
this.title = title;
}
public String description() {
return "Title: " + title;
}
}

View File

@@ -0,0 +1,64 @@
package com.baeldung.anonymous;
import java.util.ArrayList;
import java.util.List;
/**
* Code snippet that illustrates the usage of anonymous classes.
*
* Note that use of Runnable instances in this example does not demonstrate their
* common use.
*
* @author A. Shcherbakov
*
*/
public class Main {
public static void main(String[] args) {
final List<Runnable> actions = new ArrayList<Runnable>(2);
Runnable action = new Runnable() {
@Override
public void run() {
System.out.println("Hello from runnable.");
}
};
actions.add(action);
Book book = new Book("Design Patterns") {
@Override
public String description() {
return "Famous GoF book.";
}
};
System.out.println(String.format("Title: %s, description: %s", book.title, book.description()));
actions.add(new Runnable() {
@Override
public void run() {
System.out.println("Hello from runnable #2.");
}
});
int count = 1;
Runnable action2 = new Runnable() {
static final int x = 0;
// static int y = 0;
@Override
public void run() {
System.out.println(String.format("Runnable with captured variables: count = %s, x = %s", count, x));
}
};
actions.add(action2);
for (Runnable a : actions) {
a.run();
}
}
}

View File

@@ -0,0 +1,38 @@
package com.baeldung.generics;
import java.io.Serializable;
public class Entry {
private String data;
private int rank;
// non-generic constructor
public Entry(String data, int rank) {
this.data = data;
this.rank = rank;
}
// generic constructor
public <E extends Rankable & Serializable> Entry(E element) {
this.data = element.toString();
this.rank = element.getRank();
}
// getters and setters
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}

View File

@@ -0,0 +1,53 @@
package com.baeldung.generics;
import java.io.Serializable;
import java.util.Optional;
public class GenericEntry<T> {
private T data;
private int rank;
// non-generic constructor
public GenericEntry(int rank) {
this.rank = rank;
}
// generic constructor
public GenericEntry(T data, int rank) {
this.data = data;
this.rank = rank;
}
// generic constructor with different type
public <E extends Rankable & Serializable> GenericEntry(E element) {
this.data = (T) element;
this.rank = element.getRank();
}
// generic constructor with different type and wild card
public GenericEntry(Optional<? extends Rankable> optional) {
if (optional.isPresent()) {
this.data = (T) optional.get();
this.rank = optional.get()
.getRank();
}
}
// getters and setters
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.generics;
public class MapEntry<K, V> {
private K key;
private V value;
public MapEntry() {
super();
}
// generic constructor with two parameters
public MapEntry(K key, V value) {
this.key = key;
this.value = value;
}
// getters and setters
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}

View File

@@ -0,0 +1,50 @@
package com.baeldung.generics;
import java.io.Serializable;
public class Product implements Rankable, Serializable {
private String name;
private double price;
private int sales;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public int getRank() {
return sales;
}
@Override
public String toString() {
return "Product [name=" + name + ", price=" + price + ", sales=" + sales + "]";
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.generics;
public interface Rankable {
public int getRank();
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.supertypecompilerexception;
public class MyClass {
private int myField1 = 10;
private int myField2;
public MyClass() {
//uncomment this to see the supertype compiler error:
//this(myField1);
}
public MyClass(int i) {
myField2 = i;
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.supertypecompilerexception;
public class MyClassSolution1 {
private int myField1 = 10;
private int myField2;
public MyClassSolution1() {
myField2 = myField1;
}
public MyClassSolution1(int i) {
myField2 = i;
}
}

View File

@@ -0,0 +1,19 @@
package com.baeldung.supertypecompilerexception;
public class MyClassSolution2 {
private int myField1 = 10;
private int myField2;
public MyClassSolution2() {
setupMyFields(myField1);
}
public MyClassSolution2(int i) {
setupMyFields(i);
}
private void setupMyFields(int i) {
myField2 = i;
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.supertypecompilerexception;
public class MyClassSolution3 {
private static final int SOME_CONSTANT = 10;
private int myField2;
public MyClassSolution3() {
this(SOME_CONSTANT);
}
public MyClassSolution3(int i) {
myField2 = i;
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.supertypecompilerexception;
public class MyException extends RuntimeException {
private int errorCode = 0;
public MyException(String message) {
//uncomment this to see the supertype compiler error:
//super(message + getErrorCode());
}
public int getErrorCode() {
return errorCode;
}
}

View File

@@ -0,0 +1,76 @@
package com.baeldung.generics;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.Serializable;
import java.util.Optional;
import org.junit.Test;
public class GenericConstructorUnitTest {
@Test
public void givenNonGenericConstructor_whenCreateNonGenericEntry_thenOK() {
Entry entry = new Entry("sample", 1);
assertEquals("sample", entry.getData());
assertEquals(1, entry.getRank());
}
@Test
public void givenGenericConstructor_whenCreateNonGenericEntry_thenOK() {
Product product = new Product("milk", 2.5);
product.setSales(30);
Entry entry = new Entry(product);
assertEquals(product.toString(), entry.getData());
assertEquals(30, entry.getRank());
}
@Test
public void givenNonGenericConstructor_whenCreateGenericEntry_thenOK() {
GenericEntry<String> entry = new GenericEntry<String>(1);
assertNull(entry.getData());
assertEquals(1, entry.getRank());
}
@Test
public void givenGenericConstructor_whenCreateGenericEntry_thenOK() {
GenericEntry<String> entry = new GenericEntry<String>("sample", 1);
assertEquals("sample", entry.getData());
assertEquals(1, entry.getRank());
}
@Test
public void givenGenericConstructorWithDifferentType_whenCreateGenericEntry_thenOK() {
Product product = new Product("milk", 2.5);
product.setSales(30);
GenericEntry<Serializable> entry = new GenericEntry<Serializable>(product);
assertEquals(product, entry.getData());
assertEquals(30, entry.getRank());
}
@Test
public void givenGenericConstructorWithWildCard_whenCreateGenericEntry_thenOK() {
Product product = new Product("milk", 2.5);
product.setSales(30);
Optional<Product> optional = Optional.of(product);
GenericEntry<Serializable> entry = new GenericEntry<Serializable>(optional);
assertEquals(product, entry.getData());
assertEquals(30, entry.getRank());
}
@Test
public void givenGenericConstructor_whenCreateGenericEntryWithTwoTypes_thenOK() {
MapEntry<String, Integer> entry = new MapEntry<String, Integer>("sample", 1);
assertEquals("sample", entry.getKey());
assertEquals(1, entry.getValue()
.intValue());
}
}