#10 effective java: item 3
This commit is contained in:
BIN
effective-java/book.obj
Normal file
BIN
effective-java/book.obj
Normal file
Binary file not shown.
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.enumtype;
|
||||||
|
|
||||||
|
// 열거 타입 방식의 싱글턴 - 바람직한 방법 (25쪽)
|
||||||
|
public enum Elvis {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
public void leaveTheBuilding() {
|
||||||
|
System.out.println("기다려 자기야, 지금 나갈께!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 이 메서드는 보통 클래스 바깥(다른 클래스)에 작성해야 한다!
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Elvis elvis = Elvis.INSTANCE;
|
||||||
|
elvis.leaveTheBuilding();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.enumtype;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
|
public class EnumElvisReflection {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
Constructor<Elvis> declaredConstructor = Elvis.class.getDeclaredConstructor();
|
||||||
|
System.out.println(declaredConstructor);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.enumtype;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class EnumElvisSerialization {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try (ObjectOutput out = new ObjectOutputStream(new FileOutputStream("elvis.obj"))) {
|
||||||
|
out.writeObject(Elvis.INSTANCE);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (ObjectInput in = new ObjectInputStream(new FileInputStream("elvis.obj"))) {
|
||||||
|
Elvis elvis = (Elvis) in.readObject();
|
||||||
|
System.out.println(elvis == Elvis.INSTANCE);
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.functionalinterface;
|
||||||
|
|
||||||
|
import com.example.effectivejava.chapter01.item03.methodreference.Person;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class DefaultFunctions {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Function<Integer, String> intToString = Object::toString;
|
||||||
|
|
||||||
|
Supplier<Person> personSupplier = Person::new;
|
||||||
|
Function<LocalDate, Person> personFunction = Person::new;
|
||||||
|
|
||||||
|
Consumer<Integer> integerConsumer = System.out::println;
|
||||||
|
|
||||||
|
Predicate<Person> predicate;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.functionalinterface;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyFunction {
|
||||||
|
|
||||||
|
String valueOf(Integer integer);
|
||||||
|
|
||||||
|
static String hello() {
|
||||||
|
return "hello";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.functionalinterface;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class UsageOfFunctions {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<LocalDate> dates = new ArrayList<>();
|
||||||
|
dates.add(LocalDate.of(1982, 7, 15));
|
||||||
|
dates.add(LocalDate.of(2011, 3, 2));
|
||||||
|
dates.add(LocalDate.of(2013, 1, 28));
|
||||||
|
|
||||||
|
List<Integer> before2000 = dates.stream()
|
||||||
|
.filter(d -> d.isBefore(LocalDate.of(2000, 1, 1)))
|
||||||
|
.map(LocalDate::getYear).toList();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.methodreference;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
LocalDate birthday;
|
||||||
|
|
||||||
|
public Person() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person(LocalDate birthday) {
|
||||||
|
this.birthday = birthday;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int compareByAge(Person a, Person b) {
|
||||||
|
return a.birthday.compareTo(b.birthday);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int compareByAge2(Person a, Person b) {
|
||||||
|
return a.birthday.compareTo(b.birthday);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 임의 객체 참조의 경우 첫번째 인자는 자기 자신
|
||||||
|
public int compareByAge3(Person b) {
|
||||||
|
return this.birthday.compareTo(b.birthday);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<Person> people = new ArrayList<>();
|
||||||
|
people.add(new Person(LocalDate.of(1982, 7, 15)));
|
||||||
|
people.add(new Person(LocalDate.of(2011, 3, 2)));
|
||||||
|
people.add(new Person(LocalDate.of(2013, 1, 28)));
|
||||||
|
|
||||||
|
Person person = new Person(LocalDate.of(2022, 5, 14));
|
||||||
|
|
||||||
|
people.sort(Person::compareByAge);
|
||||||
|
people.sort(person::compareByAge2);
|
||||||
|
people.sort(Person::compareByAge3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return LocalDate.now().getYear() - birthday.getYear();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.serialization;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public class Book implements Serializable {
|
||||||
|
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private String isbn;
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
private LocalDate published;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private transient int numberOfSold;
|
||||||
|
|
||||||
|
public Book(String isbn, String title, String author, LocalDate published) {
|
||||||
|
this.isbn = isbn;
|
||||||
|
this.title = title;
|
||||||
|
this.published = published;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Book{" +
|
||||||
|
"isbn='" + isbn + '\'' +
|
||||||
|
", title='" + title + '\'' +
|
||||||
|
", published=" + published +
|
||||||
|
", numberOfSold=" + numberOfSold +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIsbn() {
|
||||||
|
return isbn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsbn(String isbn) {
|
||||||
|
this.isbn = isbn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getPublished() {
|
||||||
|
return published;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPublished(LocalDate published) {
|
||||||
|
this.published = published;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumberOfSold() {
|
||||||
|
return numberOfSold;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumberOfSold(int numberOfSold) {
|
||||||
|
this.numberOfSold = numberOfSold;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.serialization;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public class SerializationExample {
|
||||||
|
|
||||||
|
private void serialize(Book book) {
|
||||||
|
try (ObjectOutput out = new ObjectOutputStream(new FileOutputStream("book.obj"))) {
|
||||||
|
out.writeObject(book);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Book deserialize() {
|
||||||
|
try (ObjectInput in = new ObjectInputStream(new FileInputStream("book.obj"))) {
|
||||||
|
return (Book) in.readObject();
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Book book = new Book("12345", "이팩티브 자바 완벽 공략", "백기선",
|
||||||
|
LocalDate.of(2022, 3, 21));
|
||||||
|
book.setNumberOfSold(200);
|
||||||
|
|
||||||
|
SerializationExample example = new SerializationExample();
|
||||||
|
example.serialize(book);
|
||||||
|
Book deserializedBook = example.deserialize();
|
||||||
|
|
||||||
|
System.out.println(book);
|
||||||
|
System.out.println(deserializedBook);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user