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

@@ -1,9 +0,0 @@
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

@@ -1,9 +0,0 @@
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

@@ -1,39 +0,0 @@
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

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

View File

@@ -1,10 +0,0 @@
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

@@ -1,9 +0,0 @@
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

@@ -1,68 +0,0 @@
package com.baeldung.constructors;
import java.time.LocalDateTime;
class BankAccount {
String name;
LocalDateTime opened;
double balance;
@Override
public String toString() {
return String.format("%s, %s, %f", this.name, this.opened.toString(), this.balance);
}
public String getName() {
return name;
}
public LocalDateTime getOpened() {
return opened;
}
public double getBalance() {
return this.balance;
}
}
class BankAccountEmptyConstructor extends BankAccount {
public BankAccountEmptyConstructor() {
this.name = "";
this.opened = LocalDateTime.now();
this.balance = 0.0d;
}
}
class BankAccountParameterizedConstructor extends BankAccount {
public BankAccountParameterizedConstructor(String name, LocalDateTime opened, double balance) {
this.name = name;
this.opened = opened;
this.balance = balance;
}
}
class BankAccountCopyConstructor extends BankAccount {
public BankAccountCopyConstructor(String name, LocalDateTime opened, double balance) {
this.name = name;
this.opened = opened;
this.balance = balance;
}
public BankAccountCopyConstructor(BankAccount other) {
this.name = other.name;
this.opened = LocalDateTime.now();
this.balance = 0.0f;
}
}
class BankAccountChainedConstructors extends BankAccount {
public BankAccountChainedConstructors(String name, LocalDateTime opened, double balance) {
this.name = name;
this.opened = opened;
this.balance = balance;
}
public BankAccountChainedConstructors(String name) {
this(name, LocalDateTime.now(), 0.0f);
}
}

View File

@@ -1,25 +0,0 @@
package com.baeldung.constructors;
import java.time.LocalDateTime;
class Transaction {
final BankAccountEmptyConstructor bankAccount;
final LocalDateTime date;
final double amount;
public Transaction(BankAccountEmptyConstructor account, LocalDateTime date, double amount) {
this.bankAccount = account;
this.date = date;
this.amount = amount;
}
/*
* Compilation Error :'(, all final variables must be explicitly initialised.
* public Transaction() {
* }
*/
public void invalidMethod() {
// this.amount = 102.03; // Results in a compiler error. You cannot change the value of a final variable.
}
}

View File

@@ -1,36 +0,0 @@
package com.baeldung.equalshashcode;
class Money {
int amount;
String currencyCode;
Money(int amount, String currencyCode) {
this.amount = amount;
this.currencyCode = currencyCode;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Money))
return false;
Money other = (Money)o;
boolean currencyCodeEquals = (this.currencyCode == null && other.currencyCode == null)
|| (this.currencyCode != null && this.currencyCode.equals(other.currencyCode));
return this.amount == other.amount
&& currencyCodeEquals;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + amount;
if (currencyCode != null) {
result = 31 * result + currencyCode.hashCode();
}
return result;
}
}

View File

@@ -1,39 +0,0 @@
package com.baeldung.equalshashcode;
class Team {
final String city;
final String department;
Team(String city, String department) {
this.city = city;
this.department = department;
}
@Override
public final boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Team))
return false;
Team otherTeam = (Team)o;
boolean cityEquals = (this.city == null && otherTeam.city == null)
|| this.city != null && this.city.equals(otherTeam.city);
boolean departmentEquals = (this.department == null && otherTeam.department == null)
|| this.department != null && this.department.equals(otherTeam.department);
return cityEquals && departmentEquals;
}
@Override
public final int hashCode() {
int result = 17;
if (city != null) {
result = 31 * result + city.hashCode();
}
if (department != null) {
result = 31 * result + department.hashCode();
}
return result;
}
}

View File

@@ -1,38 +0,0 @@
package com.baeldung.equalshashcode;
class Voucher {
private Money value;
private String store;
Voucher(int amount, String currencyCode, String store) {
this.value = new Money(amount, currencyCode);
this.store = store;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Voucher))
return false;
Voucher other = (Voucher)o;
boolean valueEquals = (this.value == null && other.value == null)
|| (this.value != null && this.value.equals(other.value));
boolean storeEquals = (this.store == null && other.store == null)
|| (this.store != null && this.store.equals(other.store));
return valueEquals && storeEquals;
}
@Override
public int hashCode() {
int result = 17;
if (this.value != null) {
result = 31 * result + value.hashCode();
}
if (this.store != null) {
result = 31 * result + store.hashCode();
}
return result;
}
}

View File

@@ -1,30 +0,0 @@
package com.baeldung.equalshashcode;
/* (non-Javadoc)
* This class overrides equals, but it doesn't override hashCode.
*
* To see which problems this leads to:
* TeamUnitTest.givenMapKeyWithoutHashCode_whenSearched_thenReturnsWrongValue
*/
class WrongTeam {
String city;
String department;
WrongTeam(String city, String department) {
this.city = city;
this.department = department;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof WrongTeam))
return false;
WrongTeam otherTeam = (WrongTeam)o;
return this.city == otherTeam.city
&& this.department == otherTeam.department;
}
}

View File

@@ -1,47 +0,0 @@
package com.baeldung.equalshashcode;
/* (non-Javadoc)
* This class extends the Money class that has overridden the equals method and once again overrides the equals method.
*
* To see which problems this leads to:
* MoneyUnitTest.givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric
*/
class WrongVoucher extends Money {
private String store;
WrongVoucher(int amount, String currencyCode, String store) {
super(amount, currencyCode);
this.store = store;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof WrongVoucher))
return false;
WrongVoucher other = (WrongVoucher)o;
boolean currencyCodeEquals = (this.currencyCode == null && other.currencyCode == null)
|| (this.currencyCode != null && this.currencyCode.equals(other.currencyCode));
boolean storeEquals = (this.store == null && other.store == null)
|| (this.store != null && this.store.equals(other.store));
return this.amount == other.amount
&& currencyCodeEquals
&& storeEquals;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + amount;
if (this.currencyCode != null) {
result = 31 * result + currencyCode.hashCode();
}
if (this.store != null) {
result = 31 * result + store.hashCode();
}
return result;
}
}

View File

@@ -1,63 +0,0 @@
package com.baeldung.equalshashcode.entities;
import java.util.List;
import java.util.Set;
public class ComplexClass {
private List<?> genericList;
private Set<Integer> integerSet;
public ComplexClass(List<?> genericArrayList, Set<Integer> integerHashSet) {
super();
this.genericList = genericArrayList;
this.integerSet = integerHashSet;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((genericList == null) ? 0 : genericList.hashCode());
result = prime * result + ((integerSet == null) ? 0 : integerSet.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof ComplexClass))
return false;
ComplexClass other = (ComplexClass) obj;
if (genericList == null) {
if (other.genericList != null)
return false;
} else if (!genericList.equals(other.genericList))
return false;
if (integerSet == null) {
if (other.integerSet != null)
return false;
} else if (!integerSet.equals(other.integerSet))
return false;
return true;
}
protected List<?> getGenericList() {
return genericList;
}
protected void setGenericArrayList(List<?> genericList) {
this.genericList = genericList;
}
protected Set<Integer> getIntegerSet() {
return integerSet;
}
protected void setIntegerSet(Set<Integer> integerSet) {
this.integerSet = integerSet;
}
}

View File

@@ -1,54 +0,0 @@
package com.baeldung.equalshashcode.entities;
public class PrimitiveClass {
private boolean primitiveBoolean;
private int primitiveInt;
public PrimitiveClass(boolean primitiveBoolean, int primitiveInt) {
super();
this.primitiveBoolean = primitiveBoolean;
this.primitiveInt = primitiveInt;
}
protected boolean isPrimitiveBoolean() {
return primitiveBoolean;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (primitiveBoolean ? 1231 : 1237);
result = prime * result + primitiveInt;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PrimitiveClass other = (PrimitiveClass) obj;
if (primitiveBoolean != other.primitiveBoolean)
return false;
if (primitiveInt != other.primitiveInt)
return false;
return true;
}
protected void setPrimitiveBoolean(boolean primitiveBoolean) {
this.primitiveBoolean = primitiveBoolean;
}
protected int getPrimitiveInt() {
return primitiveInt;
}
protected void setPrimitiveInt(int primitiveInt) {
this.primitiveInt = primitiveInt;
}
}

View File

@@ -1,58 +0,0 @@
package com.baeldung.equalshashcode.entities;
public class Rectangle extends Shape {
private double width;
private double length;
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
@Override
public double area() {
return width * length;
}
@Override
public double perimeter() {
return 2 * (width + length);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(length);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Rectangle other = (Rectangle) obj;
if (Double.doubleToLongBits(length) != Double.doubleToLongBits(other.length))
return false;
if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
return false;
return true;
}
protected double getWidth() {
return width;
}
protected double getLength() {
return length;
}
}

View File

@@ -1,7 +0,0 @@
package com.baeldung.equalshashcode.entities;
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}

View File

@@ -1,58 +0,0 @@
package com.baeldung.equalshashcode.entities;
import java.awt.*;
public class Square extends Rectangle {
private Color color;
public Square(double width, Color color) {
super(width, width);
this.color = color;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((color == null) ? 0 : color.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (!(obj instanceof Square)) {
return false;
}
Square other = (Square) obj;
if (color == null) {
if (other.color != null) {
return false;
}
} else if (!color.equals(other.color)) {
return false;
}
return true;
}
protected Color getColor() {
return color;
}
protected void setColor(Color color) {
this.color = color;
}
}

View File

@@ -1,18 +0,0 @@
package com.baeldung.immutableobjects;
public final class Currency {
private final String value;
private Currency(String currencyValue) {
value = currencyValue;
}
public String getValue() {
return value;
}
public static Currency of(String value) {
return new Currency(value);
}
}

View File

@@ -1,20 +0,0 @@
package com.baeldung.immutableobjects;
// 4. Immutability in Java
public final class Money {
private final double amount;
private final Currency currency;
public Money(double amount, Currency currency) {
this.amount = amount;
this.currency = currency;
}
public Currency getCurrency() {
return currency;
}
public double getAmount() {
return amount;
}
}

View File

@@ -1,27 +0,0 @@
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;
public class Application {
public static void main(String[] args) {
Person person = new Person("John", "john@domain.com", 35);
Waitress waitress = new Waitress("Mary", "mary@domain.com", 22);
System.out.println(waitress.serveStarter("mixed salad"));
System.out.println(waitress.serveMainCourse("steak"));
System.out.println(waitress.serveDessert("cup of cofee"));
Actress actress = new Actress("Susan", "susan@domain.com", 30);
System.out.println(actress.readScript("Psycho"));
System.out.println(actress.performRole());
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
System.out.println(computer);
}
}

View File

@@ -1,16 +0,0 @@
package com.baeldung.inheritancecomposition.model;
public class Actress extends Person {
public Actress(String name, String email, int age) {
super(name, email, age);
}
public String readScript(String movie) {
return "Reading the script of " + movie;
}
public String performRole() {
return "Performing a role";
}
}

View File

@@ -1,36 +0,0 @@
package com.baeldung.inheritancecomposition.model;
import java.util.Optional;
public class Computer {
private Processor processor;
private Memory memory;
private SoundCard soundCard;
public Computer(Processor processor, Memory memory) {
this.processor = processor;
this.memory = memory;
}
public void setSoundCard(SoundCard soundCard) {
this.soundCard = soundCard;
}
public Processor getProcessor() {
return processor;
}
public Memory getMemory() {
return memory;
}
public Optional<SoundCard> getSoundCard() {
return Optional.ofNullable(soundCard);
}
@Override
public String toString() {
return "Computer{" + "processor=" + processor + ", memory=" + memory + ", soundcard=" + soundCard +"}";
}
}

View File

@@ -1,8 +0,0 @@
package com.baeldung.inheritancecomposition.model;
public interface Memory {
String getBrand();
String getSize();
}

View File

@@ -1,31 +0,0 @@
package com.baeldung.inheritancecomposition.model;
public class Person {
private final String name;
private final String email;
private final int age;
public Person(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{" + "name=" + name + ", email=" + email + ", age=" + age + "}";
}
}

View File

@@ -1,6 +0,0 @@
package com.baeldung.inheritancecomposition.model;
public interface Processor {
String getModel();
}

View File

@@ -1,6 +0,0 @@
package com.baeldung.inheritancecomposition.model;
public interface SoundCard {
String getBrand();
}

View File

@@ -1,25 +0,0 @@
package com.baeldung.inheritancecomposition.model;
public class StandardMemory implements Memory {
private String brand;
private String size;
public StandardMemory(String brand, String size) {
this.brand = brand;
this.size = size;
}
public String getBrand() {
return brand;
}
public String getSize() {
return size;
}
@Override
public String toString() {
return "Memory{" + "brand=" + brand + ", size=" + size + "}";
}
}

View File

@@ -1,20 +0,0 @@
package com.baeldung.inheritancecomposition.model;
public class StandardProcessor implements Processor {
private String model;
public StandardProcessor(String model) {
this.model = model;
}
@Override
public String getModel() {
return model;
}
@Override
public String toString() {
return "Processor{" + "model=" + model + "}";
}
}

View File

@@ -1,20 +0,0 @@
package com.baeldung.inheritancecomposition.model;
public class StandardSoundCard implements SoundCard {
private String brand;
public StandardSoundCard(String brand) {
this.brand = brand;
}
@Override
public String getBrand() {
return brand;
}
@Override
public String toString() {
return "SoundCard{" + "brand=" + brand + "}";
}
}

View File

@@ -1,20 +0,0 @@
package com.baeldung.inheritancecomposition.model;
public class Waitress extends Person {
public Waitress(String name, String email, int age) {
super(name, email, age);
}
public String serveStarter(String starter) {
return "Serving a " + starter;
}
public String serveMainCourse(String mainCourse) {
return "Serving a " + mainCourse;
}
public String serveDessert(String dessert) {
return "Serving a " + dessert;
}
}

View File

@@ -1,16 +0,0 @@
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

@@ -1,20 +0,0 @@
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

@@ -1,26 +0,0 @@
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

@@ -1,49 +0,0 @@
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

@@ -1,5 +0,0 @@
package com.baeldung.markerinterface;
public interface DeletableShape extends Shape {
}

View File

@@ -1,22 +0,0 @@
package com.baeldung.markerinterface;
public class Rectangle implements DeletableShape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getCircumference() {
return 2 * (width + height);
}
}

View File

@@ -1,6 +0,0 @@
package com.baeldung.markerinterface;
public interface Shape {
double getArea();
double getCircumference();
}

View File

@@ -1,14 +0,0 @@
package com.baeldung.markerinterface;
public class ShapeDao {
public boolean delete(Object object) {
if (!(object instanceof DeletableShape)) {
return false;
}
// Calling the code that deletes the entity from the database
return true;
}
}