improvements 7

generic-observer 7
Lisa 7 years ago
parent a5f57c0d4e
commit 67280ac2cd

@ -3,27 +3,26 @@ package _7._1;
public class LambdaTest {
public static void main(String[] args) {
Function<Double,Double> id = (x) -> x;
Function<Double, Double> id = (x) -> x;
Function<Double,Double> inverse = (x) -> x*-1;
Function<Double, Double> inverse = (x) -> -x;
Function<Double,Double> timesTen = (x) -> x*10;
Function<Double, Double> timesTen = (x) -> 10*x;
Function<Double,Double> divideByPi = (x) -> x/Math.PI;
Function<Double,Long> round = Math::round;
Function<Double, Double> divideByPi = (x) -> x/Math.PI;
Function<Double, Long> round = Math::round;
@SuppressWarnings("unchecked")
Function<Double, Double> chain = makeChain(new Function[] {inverse, id, timesTen, divideByPi});
Function<Double, Double> chain = makeChain(new Function[]{ inverse, id, timesTen, divideByPi });
System.out.println(round.calculate((chain.calculate(5.5))));
System.out.println(round.calculate(chain.calculate(5.5)));
}
private static Function<Double, Double> makeChain(final Function<Double, Double>[] functions) {
return (x) -> {
for (Function<Double, Double> i : functions) {
x = i.calculate(x);
return (x) -> {
for (Function<Double, Double> f : functions) {
x = f.calculate(x);
}
return x;
};

@ -5,16 +5,7 @@ import java.util.stream.Stream;
public class StreamTest {
public static void main(String[] args) {
Stream<Integer> naturals = Stream.iterate(1, i -> i + 1);
Stream<Integer> integers = Stream.iterate(0, i -> {
int next;
if (i>0) {
next = i*(-1);
} else {
next = i*(-1)+1;
}
return next;
});
Stream<Integer> integers = Stream.iterate(0, i -> i > 0 ? -i : -i + 1);
System.out.println(filterAndSum(naturals));
System.out.println(filterAndSum(integers));
@ -23,7 +14,7 @@ public class StreamTest {
private static Integer filterAndSum(Stream<Integer> stream) {
return stream.filter((i) -> i % 2 == 0)
.limit(10)
.reduce((a,b) -> a+b)
.reduce(Integer::sum)
.orElse(0);
}
}

@ -1,117 +1,129 @@
package _7._3;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
class Person implements Serializable {
private String firstname;
private String lastname;
private transient String sortname;
public Person() { }
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
updateSortname();
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
updateSortname();
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
updateSortname();
}
public String getSortname() {
return sortname;
}
public void updateSortname() {
sortname = lastname + firstname;
}
@Override
public String toString() {
return firstname + " " + lastname + " (" + sortname + ")";
}
public static List<Person> load(String filename) throws IOException {
List<Person> people = new ArrayList<>();
try (DataInputStream in = new DataInputStream(new BufferedInputStream(
new FileInputStream(filename)))) {
boolean nextExists = in.readBoolean();
while (nextExists) {
people.add(load(in));
nextExists = in.readBoolean();
}
}
return people;
}
public static Person load(DataInputStream in) throws IOException {
return new Person(in.readUTF(),in.readUTF());
}
public static void save(String filename, List<Person> list) throws IOException {
try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)))) {
for (Person p : list) {
out.writeBoolean(true);
save(out, p);
}
out.writeBoolean(false);
}
}
public static void save(DataOutputStream out, Person person) throws IOException {
out.writeUTF(person.getFirstname());
out.writeUTF(person.getLastname());
}
@SuppressWarnings("unchecked")
public static List<Person> unserialize(String filename) throws IOException, ClassNotFoundException {
List<Person> people;
try (ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(new FileInputStream(filename)))) {
people = (List<Person>) in.readObject();
for (Person p : people) {
p.updateSortname();
}
}
return people;
}
public static void serialize(String filename, List<Person> persons) throws IOException {
try (ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(filename)))) {
out.writeObject(persons);
}
}
private String firstname;
private String lastname;
private transient String sortname;
public Person() {
}
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
updateSortname();
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
updateSortname();
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
updateSortname();
}
public String getSortname() {
return sortname;
}
public void updateSortname() {
sortname = lastname + firstname;
}
@Override
public String toString() {
return firstname + " " + lastname + " (" + sortname + ")";
}
public static List<Person> load(String filename) throws IOException {
try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)))) {
return loadAll(in);
}
}
public static List<Person> loadAll(DataInputStream in) throws IOException {
int size = in.readInt();
List<Person> persons = new ArrayList<>();
for (int i = 0; i < size; i++) {
persons.add(load(in));
}
return persons;
}
public static Person load(DataInputStream in) throws IOException {
return new Person(in.readUTF(), in.readUTF());
}
public static void save(String filename, List<Person> list) throws IOException {
try(DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)))) {
saveAll(out, list);
}
}
public static void saveAll(DataOutputStream out, List<Person> persons) throws IOException {
out.writeInt(persons.size());
for (Person person : persons) {
save(out, person);
}
}
public static void save(DataOutputStream out, Person person) throws IOException {
out.writeUTF(person.getFirstname());
out.writeUTF(person.getLastname());
}
@SuppressWarnings("unchecked")
public static List<Person> unserialize(String filename) throws IOException, ClassNotFoundException {
try (ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(new FileInputStream(filename)))) {
List<Person> people = (List<Person>) in.readObject();
people.forEach(Person::updateSortname);
return people;
}
}
public static void serialize(String filename, List<Person> persons) throws IOException {
try (ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(filename)))) {
out.writeObject(persons);
}
}
}
public class PersonTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Willy", "Wonka"));
persons.add(new Person("Charlie", "Bucket"));
persons.add(new Person("Grandpa", "Joe"));
System.out.println(persons);
Person.save("persons.sav", persons);
persons = Person.load("persons.sav");
System.out.println(persons);
Person.serialize("persons.ser", persons);
persons = Person.unserialize("persons.ser");
System.out.println(persons);
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Willy", "Wonka"));
persons.add(new Person("Charlie", "Bucket"));
persons.add(new Person("Grandpa", "Joe"));
System.out.println(persons);
Person.save("persons.sav", persons);
persons = Person.load("persons.sav");
System.out.println(persons);
Person.serialize("persons.ser", persons);
persons = Person.unserialize("persons.ser");
System.out.println(persons);
}
}

Loading…
Cancel
Save