[JAVA-621] Added missing code

* https://www.baeldung.com/java-type-erasure introduction code

* https://www.baeldung.com/java-interfaces Electronic interface and Computer class

* https://www.baeldung.com/java-abstract-class BoardGame and Checkers classes

* https://www.baeldung.com/java-hashcode different ways of implementing hashCode

* https://www.baeldung.com/java-inheritance-composition ComputerBuilder and StandardComputerBuilder classes

* https://www.baeldung.com/java-equals-hashcode-contracts Renamed method to match article

* https://www.baeldung.com/java-static Renamed class to match article

* https://www.baeldung.com/java-nested-classes Renamed class to match article
This commit is contained in:
dupirefr 2020-04-28 08:05:50 +02:00
parent 4f75f2d5b0
commit aa7daf7901
19 changed files with 328 additions and 8 deletions

View File

@ -0,0 +1,13 @@
package com.baeldung.typeerasure;
public class Example {
public static <E> boolean containsElement(E [] elements, E element){
for (E e : elements){
if(e.equals(element)){
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.abstractclasses.overview;
public abstract class BoardGame {
//... field declarations, constructors
public abstract void play();
//... concrete methods
}

View File

@ -0,0 +1,8 @@
package com.baeldung.abstractclasses.overview;
public class Checkers extends BoardGame {
@Override
public void play() {
//... implementation
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.interfaces;
public class Computer implements Electronic {
@Override
public int getElectricityUse() {
return 1000;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.interfaces;
public interface Electronic {
// Constant variable
String LED = "LED";
// Abstract method
int getElectricityUse();
// Static method
static boolean isEnergyEfficient(String electtronicType) {
if (electtronicType.equals(LED)) {
return true;
}
return false;
}
//Default method
default void printDescription() {
System.out.println("Electronic Description");
}
}

View File

@ -14,6 +14,17 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<dependencies> <dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
@ -29,6 +40,9 @@
</dependencies> </dependencies>
<properties> <properties>
<lombok.version>1.18.12</lombok.version>
<commons-lang.version>2.6</commons-lang.version>
<assertj-core.version>3.10.0</assertj-core.version> <assertj-core.version>3.10.0</assertj-core.version>
<equalsverifier.version>3.0.3</equalsverifier.version> <equalsverifier.version>3.0.3</equalsverifier.version>
</properties> </properties>

View File

@ -0,0 +1,42 @@
package com.baeldung.hashcode.apachecommons;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37).
append(id).
append(name).
append(email).
toHashCode();
}
// getters and setters here
}

View File

@ -0,0 +1,42 @@
package com.baeldung.hashcode.eclipse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
// getters and setters here
}

View File

@ -0,0 +1,37 @@
package com.baeldung.hashcode.improved;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
return (int) id * name.hashCode() * email.hashCode();
}
// getters and setters here
}

View File

@ -0,0 +1,40 @@
package com.baeldung.hashcode.intellij;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + name.hashCode();
result = 31 * result + email.hashCode();
return result;
}
// getters and setters here
}

View File

@ -0,0 +1,22 @@
package com.baeldung.hashcode.lombok;
import lombok.EqualsAndHashCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@EqualsAndHashCode
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
// getters and setters here
}

View File

@ -0,0 +1,37 @@
package com.baeldung.hashcode.naive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User {
private final Logger logger = LoggerFactory.getLogger(User.class);
private long id;
private String name;
private String email;
public User(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;
User user = (User) o;
return id == user.id && (name.equals(user.name) && email.equals(user.email));
}
@Override
public int hashCode() {
return 1;
}
// getters and setters here
}

View File

@ -1,4 +1,4 @@
package com.baeldung.hashcode.entities; package com.baeldung.hashcode.standard;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -34,7 +34,6 @@ public class User {
hash = 31 * hash + (int) id; hash = 31 * hash + (int) id;
hash = 31 * hash + (name == null ? 0 : name.hashCode()); hash = 31 * hash + (name == null ? 0 : name.hashCode());
hash = 31 * hash + (email == null ? 0 : email.hashCode()); hash = 31 * hash + (email == null ? 0 : email.hashCode());
logger.info("hashCode() method called - Computed hash: " + hash);
return hash; return hash;
} }
// getters and setters here // getters and setters here

View File

@ -38,7 +38,7 @@ public class TeamUnitTest {
} }
@Test @Test
public void equalsContract() { public void equalsHashCodeContracts() {
EqualsVerifier.forClass(Team.class).verify(); EqualsVerifier.forClass(Team.class).verify();
} }

View File

@ -3,7 +3,7 @@ package com.baeldung.staticmodifier;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
public class StaticBlock { public class StaticBlockDemo {
private static List<String> ranks = new LinkedList<>(); private static List<String> ranks = new LinkedList<>();
static { static {
@ -23,6 +23,6 @@ public class StaticBlock {
} }
public static void setRanks(List<String> ranks) { public static void setRanks(List<String> ranks) {
StaticBlock.ranks = ranks; StaticBlockDemo.ranks = ranks;
} }
} }

View File

@ -7,11 +7,11 @@ import java.util.List;
import org.junit.Test; import org.junit.Test;
public class StaticBlockUnitTest { public class StaticBlockDemoUnitTest {
@Test @Test
public void whenAddedListElementsThroughStaticBlock_thenEnsureCorrectOrder() { public void whenAddedListElementsThroughStaticBlock_thenEnsureCorrectOrder() {
List<String> actualList = StaticBlock.getRanks(); List<String> actualList = StaticBlockDemo.getRanks();
assertThat(actualList, contains("Lieutenant", "Captain", "Major", "Colonel", "General")); assertThat(actualList, contains("Lieutenant", "Captain", "Major", "Colonel", "General"));
} }
} }

View File

@ -0,0 +1,13 @@
package com.baeldung.inheritancecomposition.model;
public abstract class ComputerBuilder {
public final void buildComputer() {
addProcessor();
addMemory();
}
public abstract void addProcessor();
public abstract void addMemory();
}

View File

@ -0,0 +1,13 @@
package com.baeldung.inheritancecomposition.model;
public class StandardComputerBuilder extends ComputerBuilder {
@Override
public void addProcessor() {
// method implementation
}
@Override
public void addMemory() {
// method implementation
}
}

View File

@ -6,7 +6,7 @@ abstract class SimpleAbstractClass {
abstract void run(); abstract void run();
} }
public class AnonymousInner { public class AnonymousInnerTest {
@Test @Test
public void run() { public void run() {