* Externalizable test

* Guide to Externalizable interface +tests

* serialize Community
This commit is contained in:
Mher Baghinyan
2018-03-15 13:08:37 +04:00
committed by Grzegorz Piwowarek
parent 510ad19f90
commit 24acf35bce
4 changed files with 213 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
package com.baeldung.externalizable;
import java.io.*;
public class Community implements Serializable {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Community{" +
"id=" + id +
'}';
}
}

View File

@@ -0,0 +1,62 @@
package com.baeldung.externalizable;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class Country implements Externalizable {
private static final long serialVersionUID = 1L;
private String name;
private String capital;
private int code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeUTF(capital);
out.writeInt(code);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.name = in.readUTF();
this.capital = in.readUTF();
this.code = in.readInt();
}
@Override
public String toString() {
return "Country{" +
"name='" + name + '\'' +
", capital='" + capital + '\'' +
", code=" + code +
'}';
}
}

View File

@@ -0,0 +1,57 @@
package com.baeldung.externalizable;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class Region extends Country implements Externalizable {
private static final long serialVersionUID = 1L;
private String climate;
private Double population;
private Community community;
public String getClimate() {
return climate;
}
public void setClimate(String climate) {
this.climate = climate;
}
public Double getPopulation() {
return population;
}
public void setPopulation(Double population) {
this.population = population;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeUTF(climate);
community = new Community();
community.setId(5);
out.writeObject(community);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
this.climate = in.readUTF();
community = (Community) in.readObject();
}
@Override
public String toString() {
return "Region = {" +
"country='" + super.toString() + '\'' +
"community='" + community.toString() + '\'' +
"climate='" + climate + '\'' +
", population=" + population +
'}';
}
}