Skip to content

Commit

Permalink
Sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
prmr committed Apr 4, 2017
1 parent 7f5e288 commit 4ebd28a
Show file tree
Hide file tree
Showing 15 changed files with 818 additions and 0 deletions.
32 changes: 32 additions & 0 deletions modules/artifacts/module-10/lecture1/DeserializationException.java
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);
}

}
34 changes: 34 additions & 0 deletions modules/artifacts/module-10/lecture1/Product1.java
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;
}
}
71 changes: 71 additions & 0 deletions modules/artifacts/module-10/lecture1/Product1a.java
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);
}
}
}
61 changes: 61 additions & 0 deletions modules/artifacts/module-10/lecture1/Product2.java
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;
}
}
67 changes: 67 additions & 0 deletions modules/artifacts/module-10/lecture1/Product2a.java
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;
}
}
75 changes: 75 additions & 0 deletions modules/artifacts/module-10/lecture1/Product3.java
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;
}
}
47 changes: 47 additions & 0 deletions modules/artifacts/module-10/lecture1/Product4.java
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;
}
}
Loading

0 comments on commit 4ebd28a

Please sign in to comment.