-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
818 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
modules/artifacts/module-10/lecture1/DeserializationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package lecture1; | ||
|
||
@SuppressWarnings("serial") | ||
public class DeserializationException extends RuntimeException | ||
{ | ||
public DeserializationException() | ||
{ | ||
super(); | ||
} | ||
|
||
public DeserializationException(String message, Throwable cause, boolean enableSuppression, | ||
boolean writableStackTrace) | ||
{ | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
|
||
public DeserializationException(String message, Throwable cause) | ||
{ | ||
super(message, cause); | ||
} | ||
|
||
public DeserializationException(String message) | ||
{ | ||
super(message); | ||
} | ||
|
||
public DeserializationException(Throwable cause) | ||
{ | ||
super(cause); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package lecture1; | ||
|
||
/** | ||
* Sample "flat" data structure that can easily be serialized | ||
* in Comma-separated value (CSV) format, or similarly | ||
* using other conventions. | ||
*/ | ||
public class Product1 | ||
{ | ||
private String aName; | ||
private int aId; | ||
|
||
public Product1(String pName, int pId) | ||
{ | ||
aName = pName; | ||
aId = pId; | ||
} | ||
|
||
public String getName() | ||
{ | ||
return aName; | ||
} | ||
|
||
public int getId() | ||
{ | ||
return aId; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return aId + "," + aName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package lecture1; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVParser; | ||
import org.apache.commons.csv.CSVRecord; | ||
|
||
/** | ||
* Sample "flat" data structure that can easily be serialized | ||
* in Comma-separated value (CSV) format, or similarly | ||
* using other conventions, more robust and better encapsulated than | ||
* product1. | ||
*/ | ||
public class Product1a | ||
{ | ||
private String aName; | ||
private int aId; | ||
|
||
public Product1a(String pName, int pId) | ||
{ | ||
aName = pName; | ||
aId = pId; | ||
} | ||
|
||
public String getName() | ||
{ | ||
return aName; | ||
} | ||
|
||
public int getId() | ||
{ | ||
return aId; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return aId + "," + aName; | ||
} | ||
|
||
public String serialize() | ||
{ | ||
return "\"" + aName + "\"," + aId; | ||
} | ||
|
||
public Product1a(String pSerialized) | ||
{ | ||
try | ||
{ | ||
CSVParser parser = CSVParser.parse(pSerialized, CSVFormat.DEFAULT); | ||
List<CSVRecord> records = parser.getRecords(); | ||
if( records.size() != 1 ) | ||
{ | ||
throw new DeserializationException(); | ||
} | ||
CSVRecord record = records.get(0); | ||
if(record.size() != 2 ) | ||
{ | ||
throw new DeserializationException(); | ||
} | ||
aName = record.get(0); | ||
aId = Integer.parseInt(record.get(1)); | ||
} | ||
catch(IOException | NumberFormatException e) | ||
{ | ||
throw new DeserializationException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package lecture1; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
|
||
import org.json.JSONObject; | ||
|
||
/** | ||
* Sample hierarchical structure.. | ||
*/ | ||
public class Product2 | ||
{ | ||
private String aName; | ||
private int aId; | ||
private HashMap<String,String> aProperties = new HashMap<>(); | ||
|
||
public Product2(String pName, int pId) | ||
{ | ||
aName = pName; | ||
aId = pId; | ||
} | ||
|
||
public void setProperty(String pKey, String pValue) | ||
{ | ||
aProperties.put(pKey, pValue); | ||
} | ||
|
||
public String getProperty(String pKey) | ||
{ | ||
return aProperties.get(pKey); | ||
} | ||
|
||
public Iterator<String> getPropertyNames() | ||
{ | ||
return aProperties.keySet().iterator(); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return aId + "," + aName + " " + aProperties; | ||
} | ||
|
||
public JSONObject toJSONObject() | ||
{ | ||
// First a properties object where keys become fields | ||
JSONObject properties = new JSONObject(); | ||
for( String key : aProperties.keySet() ) | ||
{ | ||
properties.put(key, aProperties.get(key)); | ||
} | ||
|
||
// Then construct the actual product object | ||
JSONObject product = new JSONObject(); | ||
product.put("name", aName); | ||
product.put("id", aId); | ||
product.put("properties", properties); | ||
|
||
return product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package lecture1; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
|
||
import org.json.JSONObject; | ||
|
||
/** | ||
* Sample hierarchical structure, initially without links, | ||
* but with aRelated added in, demonstrates the weakness | ||
* of JSON serialization for graphs. | ||
*/ | ||
public class Product2a | ||
{ | ||
private String aName; | ||
private int aId; | ||
private HashMap<String,String> aProperties = new HashMap<>(); | ||
private Product2a aRelated; | ||
|
||
public Product2a(String pName, int pId) | ||
{ | ||
aName = pName; | ||
aId = pId; | ||
} | ||
|
||
public void setRelated(Product2a p) | ||
{ | ||
aRelated = p; | ||
} | ||
|
||
public void setProperty(String pKey, String pValue) | ||
{ | ||
aProperties.put(pKey, pValue); | ||
} | ||
|
||
public String getProperties(String pKey) | ||
{ | ||
return aProperties.get(pKey); | ||
} | ||
|
||
public Iterator<String> getPropertyNames() | ||
{ | ||
return aProperties.keySet().iterator(); | ||
} | ||
|
||
public JSONObject toJSONObject() | ||
{ | ||
// First a properties object where keys become fields | ||
JSONObject properties = new JSONObject(); | ||
for( String key : aProperties.keySet() ) | ||
{ | ||
properties.put(key, aProperties.get(key)); | ||
} | ||
|
||
// Then construct the actual product object | ||
JSONObject product = new JSONObject(); | ||
product.put("name", aName); | ||
product.put("id", aId); | ||
product.put("properties", properties); | ||
if( aRelated != null ) | ||
{ | ||
product.put("related", aRelated.toJSONObject()); | ||
} | ||
|
||
return product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package lecture1; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* Product object in the JavaBeans framework. | ||
*/ | ||
public class Product3 | ||
{ | ||
private String aName = "default"; | ||
private int aId = -1; | ||
private HashMap<String,String> aProperties = new HashMap<>(); | ||
private Product3 aRelated; | ||
|
||
public Product3() | ||
{} | ||
|
||
public Product3(String pName, int pId) | ||
{ | ||
aName = pName; | ||
aId = pId; | ||
} | ||
|
||
public String getName() | ||
{ | ||
return aName; | ||
} | ||
|
||
public void setName(String pName) | ||
{ | ||
aName = pName; | ||
} | ||
|
||
public int getId() | ||
{ | ||
return aId; | ||
} | ||
|
||
public void setId(int pId) | ||
{ | ||
aId = pId; | ||
} | ||
|
||
public Product3 getRelated() | ||
{ | ||
return aRelated; | ||
} | ||
|
||
public void setRelated(Product3 pRelated) | ||
{ | ||
aRelated = pRelated; | ||
} | ||
|
||
public void setProperty(String pKey, String pValue) | ||
{ | ||
aProperties.put(pKey, pValue); | ||
} | ||
|
||
public String getProperty(String pKey) | ||
{ | ||
return aProperties.get(pKey); | ||
} | ||
|
||
public Iterator<String> getPropertyNames() | ||
{ | ||
return aProperties.keySet().iterator(); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return aId + "," + aName + " " + aProperties; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package lecture1; | ||
|
||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
|
||
/** | ||
*/ | ||
public class Product4 implements Serializable | ||
{ | ||
private String aName; | ||
private int aId; | ||
private transient HashMap<String,String> aProperties = new HashMap<>(); | ||
private Product4 aRelated; | ||
|
||
public Product4(String pName, int pId) | ||
{ | ||
aName = pName; | ||
aId = pId; | ||
} | ||
|
||
public void setRelated(Product4 p) | ||
{ | ||
aRelated = p; | ||
} | ||
|
||
public void setProperty(String pKey, String pValue) | ||
{ | ||
aProperties.put(pKey, pValue); | ||
} | ||
|
||
public String getProperties(String pKey) | ||
{ | ||
return aProperties.get(pKey); | ||
} | ||
|
||
public Iterator<String> getPropertyNames() | ||
{ | ||
return aProperties.keySet().iterator(); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return aId + "," + aName + " " + aProperties; | ||
} | ||
} |
Oops, something went wrong.