Skip to content

Commit

Permalink
Update to FixedMapSchema
Browse files Browse the repository at this point in the history
Each key in the map can be associated to a different type of value
  • Loading branch information
sylvainhalle committed Sep 7, 2023
1 parent 8ebe718 commit 6a2b404
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
52 changes: 41 additions & 11 deletions Source/Buffy/src/ca/uqac/lif/azrael/buffy/FixedMapSchema.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Azrael, a serializer for Java objects
Copyright (C) 2016-2022 Sylvain Hallé
Copyright (C) 2016-2023 Sylvain Hallé
Laboratoire d'informatique formelle
Université du Québec à Chicoutimi, Canada
Expand Down Expand Up @@ -31,21 +31,41 @@ public class FixedMapSchema implements Schema
{
/*@ non_null @*/ protected final List<String> m_keys;

/*@ non_null @*/ protected final Schema m_valueType;

public FixedMapSchema(/*@ non_null @*/ Schema value_type, /*@ non_null @*/ List<String> keys)
/*@ non_null @*/ protected final Map<String,Schema> m_valueTypes;
public FixedMapSchema(/*@ non_null @*/ List<String> keys)
{
super();
m_keys = keys;
Collections.sort(m_keys);
m_valueType = value_type;
Collections.sort(keys);
m_valueTypes = new HashMap<String,Schema>(keys.size());
}

public FixedMapSchema(/*@ non_null @*/ String ... keys)
{
this(Arrays.asList(keys));
}

public FixedMapSchema(/*@ non_null @*/ Schema value_type, /*@ non_null @*/ List<String> keys)
{
this(keys);
for (String k : keys)
{
m_valueTypes.put(k, value_type);
}
}

public FixedMapSchema(/*@ non_null @*/ Schema value_type, /*@ non_null @*/ String ... keys)
{
this(value_type, Arrays.asList(keys));
}

public FixedMapSchema setSchema(String key, Schema value_type)
{
m_valueTypes.put(key, value_type);
return this;
}

@Override
public String toString()
{
Expand All @@ -57,10 +77,14 @@ public String toString()
{
out.append(",");
}
out.append(m_keys.get(i));
String k = m_keys.get(i);
if (!m_valueTypes.containsKey(k))
{
out.append(k + "->?");
}
out.append(k + "->" + m_valueTypes.get(k).toString());
}
out.append("]->");
out.append(m_valueType.toString());
out.append("]");
out.append(")");
return out.toString();
}
Expand Down Expand Up @@ -96,7 +120,12 @@ public BitSequence print(Object o) throws PrintException
else
{
s.add(true);
s.addAll(m_valueType.print(bo));
if (!m_valueTypes.containsKey(k))
{
throw new PrintException("No schema defined for key " + k);
}
Schema sch = m_valueTypes.get(k);
s.addAll(sch.print(bo));
}
}
return s;
Expand All @@ -116,7 +145,8 @@ public Map<String,?> read(Object o) throws ReadException
BitSequence has_entry = s.truncatePrefix(1);
if (has_entry.get(0))
{
Object o2 = m_valueType.read(s);
Schema sch = m_valueTypes.get(k);
Object o2 = sch.read(s);
map.put(k, o2);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public BitSequence print(Object o) throws PrintException
}
FixedMapSchema s = (FixedMapSchema) o;
BitSequence bs = s_listSchema.print(s.m_keys);
bs.addAll(ReflectiveSchema.this.print(s.m_valueType));
bs.addAll(ReflectiveSchema.this.print(s.m_valueTypes));
return bs;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public Map<String,?> read(Object ob) throws ReadException
BitSequence has_entry = s.truncatePrefix(1);
if (has_entry.get(0))
{
Object o = m_valueType.read(s);
Schema value_type = m_valueTypes.get(k);
Object o = value_type.read(s);
map.put(k, ((Number) o).intValue());
}
else
Expand Down Expand Up @@ -140,7 +141,8 @@ public Map<String,?> read(Object ob) throws ReadException
BitSequence has_entry = s.truncatePrefix(1);
if (has_entry.get(0))
{
Object o = m_valueType.read(s);
Schema value_type = m_valueTypes.get(k);
Object o = value_type.read(s);
map.put(k, (String) o);
}
else
Expand Down

0 comments on commit 6a2b404

Please sign in to comment.