A
Aryeh.Friedman
I have the following class that attempts to save a
HashMap<T1,ArrayList<T2>>
public class Roles
{
public Roles()
{
contributers=new
HashMap<Contributer,ArrayList<Role>>();
}
public void addRole(Contributer contributer,Role role)
{
ArrayList<Role> r=contributers.get(contributer);
if(r==null) {
r=new ArrayList<Role>();
} else r=contributers.get(contributer);
r.add(role);
contributers.put(contributer,r);
}
public ArrayList<Role> getRoles(Contributer contributer)
{
return contributers.get(contributer);
}
public void save()
{
FileUtils.serialize("contributers",contributers);
}
public void load()
{
contributers=FileUtils.deserialize("contributers",contributers.getClass());
}
public void reset()
{
contributers=new
HashMap<Contributer,ArrayList<Role>>();
}
public String toString()
{
String s=new String();
s+=contributers;
return s;
}
private HashMap<Contributer,ArrayList<Role>> contributers;
}
When I run it against the following test code I get the output below:
public class RolesTest extends TestCase
{
public void setUp()
{
mc=new MockContributer();
mr=new MockRole();
mrl=new ArrayList<MockRole>();
mcl=new ArrayList<MockContributer>();
mrl.add(mr);
mcl.add(mc);
}
public void testAdd() {
Roles roles=new Roles();
roles.addRole(mc,mr);
System.out.println(roles.getRoles(mc));
roles.save();
roles.reset();
roles.load();
System.out.println(roles.getRoles(mc));
}
private MockContributer mc;
private MockRole mr;
private ArrayList<MockContributer> mcl;
private ArrayList<MockRole> mrl;
}
Output:
[Name: mock role]
null
For reference here are the MockContributer, MockRole, Contributer,
Role, FileUtils and IOUtils (which FIleUtils.[de]serialize calls on):
public class MockContributer extends Contributer
{
public MockContributer()
{
super("aryeh");
}
private static final long serialVersionUID=0;
}
public class MockRole extends Role
{
public MockRole()
{
super("mock role");
}
private static final long serialVersionUID=0;
}
public abstract class Contributer implements Serializable
{
public Contributer(String id)
{
this.id=id;
}
public String getId()
{
return id;
}
public String toString()
{
String s=new String();
s+="authorId: "+id;
return s;
}
private String id;
}
public abstract class Role implements Serializable
{
public Role(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public String toString()
{
String s=new String();
s+="Name: "+name;
return s;
}
private static final long serialVersionUID=0;
private String name;
}
public class FileUtils
{
public static void serialize(String fname,Serializable obj)
{
FileOutputStream out=null;
try {
out=new FileOutputStream(fname);
} catch(FileNotFoundException e) {
throw new FileError(e);
}
IOUtils.serialize(out,obj);
try {
out.close();
} catch(IOException e) {
throw new FileError(e);
}
}
public static <T extends Serializable> T deserialize(String
fname,Class<? extends T> c)
{
FileInputStream in=null;
T obj=null;
try {
in=new FileInputStream(fname);
} catch(FileNotFoundException e) {
throw new FileError(e);
}
obj=IOUtils.deserialize(in,c);
try {
in.close();
} catch(IOException e) {
throw new FileError(e);
}
return obj;
}
}
public class IOUtils
{
public static void serialize(OutputStream out,Serializable obj)
{
try {
ObjectOutputStream stream=new
ObjectOutputStream(out);
stream.writeObject(obj);
} catch(IOException e) {
throw new IOError(e);
}
}
public static <T> T deserialize(InputStream in,Class<? extends
T> c)
{
try {
ObjectInputStream stream=new
ObjectInputStream(in);
T obj=c.cast(stream.readObject());
return obj;
} catch(IOException e) {
throw new IOError(e);
} catch(ClassNotFoundException e) {
throw new IOError(e);
}
}
}
HashMap<T1,ArrayList<T2>>
public class Roles
{
public Roles()
{
contributers=new
HashMap<Contributer,ArrayList<Role>>();
}
public void addRole(Contributer contributer,Role role)
{
ArrayList<Role> r=contributers.get(contributer);
if(r==null) {
r=new ArrayList<Role>();
} else r=contributers.get(contributer);
r.add(role);
contributers.put(contributer,r);
}
public ArrayList<Role> getRoles(Contributer contributer)
{
return contributers.get(contributer);
}
public void save()
{
FileUtils.serialize("contributers",contributers);
}
public void load()
{
contributers=FileUtils.deserialize("contributers",contributers.getClass());
}
public void reset()
{
contributers=new
HashMap<Contributer,ArrayList<Role>>();
}
public String toString()
{
String s=new String();
s+=contributers;
return s;
}
private HashMap<Contributer,ArrayList<Role>> contributers;
}
When I run it against the following test code I get the output below:
public class RolesTest extends TestCase
{
public void setUp()
{
mc=new MockContributer();
mr=new MockRole();
mrl=new ArrayList<MockRole>();
mcl=new ArrayList<MockContributer>();
mrl.add(mr);
mcl.add(mc);
}
public void testAdd() {
Roles roles=new Roles();
roles.addRole(mc,mr);
System.out.println(roles.getRoles(mc));
roles.save();
roles.reset();
roles.load();
System.out.println(roles.getRoles(mc));
}
private MockContributer mc;
private MockRole mr;
private ArrayList<MockContributer> mcl;
private ArrayList<MockRole> mrl;
}
Output:
[Name: mock role]
null
For reference here are the MockContributer, MockRole, Contributer,
Role, FileUtils and IOUtils (which FIleUtils.[de]serialize calls on):
public class MockContributer extends Contributer
{
public MockContributer()
{
super("aryeh");
}
private static final long serialVersionUID=0;
}
public class MockRole extends Role
{
public MockRole()
{
super("mock role");
}
private static final long serialVersionUID=0;
}
public abstract class Contributer implements Serializable
{
public Contributer(String id)
{
this.id=id;
}
public String getId()
{
return id;
}
public String toString()
{
String s=new String();
s+="authorId: "+id;
return s;
}
private String id;
}
public abstract class Role implements Serializable
{
public Role(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public String toString()
{
String s=new String();
s+="Name: "+name;
return s;
}
private static final long serialVersionUID=0;
private String name;
}
public class FileUtils
{
public static void serialize(String fname,Serializable obj)
{
FileOutputStream out=null;
try {
out=new FileOutputStream(fname);
} catch(FileNotFoundException e) {
throw new FileError(e);
}
IOUtils.serialize(out,obj);
try {
out.close();
} catch(IOException e) {
throw new FileError(e);
}
}
public static <T extends Serializable> T deserialize(String
fname,Class<? extends T> c)
{
FileInputStream in=null;
T obj=null;
try {
in=new FileInputStream(fname);
} catch(FileNotFoundException e) {
throw new FileError(e);
}
obj=IOUtils.deserialize(in,c);
try {
in.close();
} catch(IOException e) {
throw new FileError(e);
}
return obj;
}
}
public class IOUtils
{
public static void serialize(OutputStream out,Serializable obj)
{
try {
ObjectOutputStream stream=new
ObjectOutputStream(out);
stream.writeObject(obj);
} catch(IOException e) {
throw new IOError(e);
}
}
public static <T> T deserialize(InputStream in,Class<? extends
T> c)
{
try {
ObjectInputStream stream=new
ObjectInputStream(in);
T obj=c.cast(stream.readObject());
return obj;
} catch(IOException e) {
throw new IOError(e);
} catch(ClassNotFoundException e) {
throw new IOError(e);
}
}
}