Split or move core-java-modules/core-java-lang-oop module (#7767)

This commit is contained in:
Catalin Burcea
2019-09-12 12:32:10 +03:00
committed by Josh Cummings
parent 71818a79cc
commit ec23ab367e
76 changed files with 124 additions and 71 deletions

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

@@ -0,0 +1,9 @@
package com.baeldung.accessmodifiers;
public class Public {
public Public() {
SuperPublic.publicMethod(); // Available everywhere.
SuperPublic.protectedMethod(); // Available in the same package or subclass.
SuperPublic.defaultMethod(); // Available in the same package.
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.accessmodifiers;
public class SubClass extends SuperPublic {
public SubClass() {
SuperPublic.publicMethod(); // Available everywhere.
SuperPublic.protectedMethod(); // Available in the same package or subclass.
SuperPublic.defaultMethod(); // Available in the same package.
}
}

View File

@@ -0,0 +1,39 @@
package com.baeldung.accessmodifiers;
//Only public or default access modifiers are permitted
public class SuperPublic {
// Always available from anywhere
static public void publicMethod() {
System.out.println(SuperPublic.class.getName() + " publicMethod()");
}
// Available within the same package
static void defaultMethod() {
System.out.println(SuperPublic.class.getName() + " defaultMethod()");
}
// Available within the same package and subclasses
static protected void protectedMethod() {
System.out.println(SuperPublic.class.getName() + " protectedMethod()");
}
// Available within the same class only
static private void privateMethod() {
System.out.println(SuperPublic.class.getName() + " privateMethod()");
}
// Method in the same class = has access to all members within the same class
private void anotherPrivateMethod() {
privateMethod();
defaultMethod();
protectedMethod();
publicMethod(); // Available in the same class only.
}
}
// Only public or default access modifiers are permitted
class SuperDefault {
public void publicMethod() {
System.out.println(this.getClass().getName() + " publicMethod()");
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.accessmodifiers.another;
import com.baeldung.accessmodifiers.SuperPublic;
public class AnotherPublic {
public AnotherPublic() {
SuperPublic.publicMethod(); // Available everywhere.
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.accessmodifiers.another;
import com.baeldung.accessmodifiers.SuperPublic;
public class AnotherSubClass extends SuperPublic {
public AnotherSubClass() {
SuperPublic.publicMethod(); // Available everywhere.
SuperPublic.protectedMethod(); // Available in subclass. Let's note different package.
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.accessmodifiers.another;
import com.baeldung.accessmodifiers.SuperPublic;
public class AnotherSuperPublic {
public AnotherSuperPublic() {
SuperPublic.publicMethod(); // Available everywhere. Let's note different package.
}
}

View File

@@ -0,0 +1,31 @@
package com.baeldung.accessmodifiers.publicmodifier;
import java.util.AbstractList;
import java.util.Arrays;
public class ListOfThree<E> extends AbstractList<E> {
private static final int LENGTH = 3;
private Object[] elements;
public ListOfThree(E[] data) {
if(data == null
|| data.length != LENGTH)
throw new IllegalArgumentException();
this.elements = Arrays.copyOf(data, data.length); //shallow copy
}
@Override
@SuppressWarnings("unchecked")
public E get(int index) {
return (E)elements[index];
}
@Override
public int size() {
return LENGTH;
}
}

View File

@@ -0,0 +1,7 @@
package com.baeldung.accessmodifiers.publicmodifier;
public class SpecialCharacters {
public static final String SLASH = "/";
}

View File

@@ -0,0 +1,67 @@
package com.baeldung.accessmodifiers.publicmodifier;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
public class Student {
private StudentGrade grade; //new data representation
// private int grade; //old data representation
private String name;
private int age;
public void setGrade(int grade) {
this.grade = new StudentGrade(grade);
}
public int getGrade() {
return this.grade.getGrade().intValue(); //int is returned for backward compatibility
}
public Connection getConnection() throws SQLException {
final String URL = "jdbc:h2:~/test";
return DriverManager.getConnection(URL, "sa", "");
}
public void setAge(int age) {
if (age < 0 || age > 150) {
throw new IllegalArgumentException();
}
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return this.name;
}
private class StudentGrade {
private BigDecimal grade = BigDecimal.ZERO;
private Date updatedAt;
public StudentGrade(int grade) {
this.grade = new BigDecimal(grade);
this.updatedAt = new Date();
}
public BigDecimal getGrade() {
return grade;
}
public Date getDate() {
return updatedAt;
}
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.keyword;
import com.baeldung.keyword.superkeyword.SuperSub;
import com.baeldung.keyword.thiskeyword.KeywordUnitTest;
/**
* Created by Gebruiker on 5/14/2018.
*/
public class KeywordDemo {
public static void main(String[] args) {
KeywordUnitTest keyword = new KeywordUnitTest();
SuperSub child = new SuperSub("message from the child class");
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.keyword.superkeyword;
/**
* Created by Gebruiker on 5/14/2018.
*/
public class SuperBase {
String message = "super class";
public SuperBase() {
}
public SuperBase(String message) {
this.message = message;
}
public void printMessage() {
System.out.println(message);
}
}

View File

@@ -0,0 +1,26 @@
package com.baeldung.keyword.superkeyword;
/**
* Created by Gebruiker on 5/15/2018.
*/
public class SuperSub extends SuperBase {
String message = "child class";
public SuperSub(String message) {
super(message);
}
public SuperSub() {
super.printMessage();
printMessage();
}
public void getParentMessage() {
System.out.println(super.message);
}
public void printMessage() {
System.out.println(message);
}
}

View File

@@ -0,0 +1,49 @@
package com.baeldung.keyword.thiskeyword;
public class KeywordUnitTest {
private String name;
private int age;
public KeywordUnitTest() {
this("John", 27);
this.printMessage();
printInstance(this);
}
public KeywordUnitTest(String name, int age) {
this.name = name;
this.age = age;
}
public void printMessage() {
System.out.println("invoked by this");
}
public void printInstance(KeywordUnitTest thisKeyword) {
System.out.println(thisKeyword);
}
public KeywordUnitTest getCurrentInstance() {
return this;
}
class ThisInnerClass {
boolean isInnerClass = true;
public ThisInnerClass() {
KeywordUnitTest thisKeyword = KeywordUnitTest.this;
String outerString = KeywordUnitTest.this.name;
System.out.println(this.isInnerClass);
}
}
@Override
public String toString() {
return "KeywordTest{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.relationships.aggregation;
import java.util.List;
public class Car {
private List<Wheel> wheels;
}

View File

@@ -0,0 +1,13 @@
package com.baeldung.relationships.aggregation;
import java.util.List;
public class CarWithStaticInnerWheel {
private List<Wheel> wheels;
public static class Wheel {
}
}

View File

@@ -0,0 +1,7 @@
package com.baeldung.relationships.aggregation;
public class Wheel {
private Car car;
}

View File

@@ -0,0 +1,7 @@
package com.baeldung.relationships.association;
public class Child {
private Mother mother;
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.relationships.association;
import java.util.List;
public class Mother {
private List<Child> children;
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.relationships.composition;
import java.util.List;
public class Building {
private String address;
private List<Room> rooms;
public class Room {
public String getBuildingAddress() {
return Building.this.address;
}
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.relationships.composition;
public class BuildingWithDefinitionRoomInMethod {
public Room createAnonymousRoom() {
return new Room() {
@Override
public void doInRoom() {}
};
}
public Room createInlineRoom() {
class InlineRoom implements Room {
@Override
public void doInRoom() {}
}
return new InlineRoom();
}
public Room createLambdaRoom() {
return () -> {};
}
public interface Room {
void doInRoom();
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.relationships.university;
import java.util.List;
public class Department {
private List<Professor> professors;
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.relationships.university;
import java.util.List;
public class Professor {
private List<Department> department;
private List<Professor> friends;
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.relationships.university;
import java.util.List;
public class University {
private List<Department> department;
}

View File

@@ -0,0 +1,94 @@
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 java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
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 {
@Test
public void whenUsingBigDecimalIntValueMethod_correspondingIntIsReturned() {
assertEquals(0, new BigDecimal(0).intValue()); //instance member
}
@Test
public void whenUsingIntegerMaxValueField_maxPossibleIntValueIsReturned() {
assertEquals(2147483647, Integer.MAX_VALUE); //static field
}
@Test
public void whenChangingStudentInternalRepresentation_clientCodeWillNotBreak() {
Student student = new Student();
student.setGrade(100);
assertEquals(100, student.getGrade());
}
@Test
public void whenUsingEntrySet_keyValuePairsAreReturned() {
Map<String, String> mapObject = new HashMap<String, String>();
mapObject.put("name", "Alex");
for(Map.Entry<String, String> entry : mapObject.entrySet()) {
assertEquals("name", entry.getKey());
assertEquals("Alex", entry.getValue());
}
}
@Test
public void whenUsingStringToLowerCase_stringTurnsToLowerCase() {
assertEquals("alex", "ALEX".toLowerCase());
}
@Test
public void whenParsingStringOne_parseIntReturns1() {
assertEquals(1, Integer.parseInt("1"));
}
@Test
public void whenConnectingToH2_connectionInstanceIsReturned() throws SQLException {
final String url = "jdbc:h2:~/test";
Connection conn = DriverManager.getConnection(url, "sa", "");
assertNotNull(conn);
}
@Test
public void whenCreatingCustomList_concreteAndInheritedMethodsWork() {
String[] dataSet1 = new String[] {"zero", "one", "two"};
List<String> list1 = new ListOfThree<String>(dataSet1);
//our implemented methods
assertEquals("one", list1.get(1));
assertEquals(3, list1.size());
//inherited implementations
assertEquals(1, list1.indexOf("one"));
String[] dataSet2 = new String[] {"two", "zero", "one"};
List<String> list2 = new ListOfThree<String>(dataSet2);
assertTrue(list1.containsAll(list2));
}
}

View File

@@ -0,0 +1,37 @@
package com.baeldung.parameterpassing;
import org.junit.Assert;
import org.junit.Test;
public class NonPrimitivesUnitTest {
@Test
public void whenModifyingObjects_thenOriginalObjectChanged() {
Foo a = new Foo(1);
Foo b = new Foo(1);
// Before Modification
Assert.assertEquals(a.num, 1);
Assert.assertEquals(b.num, 1);
modify(a, b);
// After Modification
Assert.assertEquals(a.num, 2);
Assert.assertEquals(b.num, 1);
}
public static void modify(Foo a1, Foo b1) {
a1.num++;
b1 = new Foo(1);
b1.num++;
}
}
class Foo {
public int num;
public Foo(int num) {
this.num = num;
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.parameterpassing;
import org.junit.Assert;
import org.junit.Test;
public class PrimitivesUnitTest {
@Test
public void whenModifyingPrimitives_thenOriginalValuesNotModified() {
int x = 1;
int y = 2;
// Before Modification
Assert.assertEquals(x, 1);
Assert.assertEquals(y, 2);
modify(x, y);
// After Modification
Assert.assertEquals(x, 1);
Assert.assertEquals(y, 2);
}
public static void modify(int x1, int y1) {
x1 = 5;
y1 = 10;
}
}