Skip to content

Commit

Permalink
adding support for GamaColor serialization (see #3288 )
Browse files Browse the repository at this point in the history
  • Loading branch information
lesquoyb committed Feb 11, 2022
1 parent 4862e1b commit 898ae1f
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ummisco.gama.serializer.gamaType.converters.GamaAgentConverterNetwork;
import ummisco.gama.serializer.gamaType.converters.GamaBDIPlanConverter;
import ummisco.gama.serializer.gamaType.converters.GamaBasicTypeConverter;
import ummisco.gama.serializer.gamaType.converters.GamaColorConverter;
import ummisco.gama.serializer.gamaType.converters.GamaFileConverter;
import ummisco.gama.serializer.gamaType.converters.GamaGraphConverter;
import ummisco.gama.serializer.gamaType.converters.GamaListConverter;
Expand Down Expand Up @@ -44,7 +45,7 @@ public abstract class Converters {
*/
private static Converter[] loadConverter(ConverterScope cs)
{
Converter[] converters= new Converter[15];
Converter[] converters= new Converter[16];
converters[0]= new GamaBasicTypeConverter(cs);
converters[1]=new GamaAgentConverter(cs);
converters[2]=new GamaListConverter(cs);
Expand All @@ -53,16 +54,17 @@ private static Converter[] loadConverter(ConverterScope cs)
converters[5]=new GamaMatrixConverter(cs);
converters[6]=new GamaGraphConverter(cs);
converters[7]=new GamaFileConverter(cs);
converters[8]=new GamaColorConverter();

converters[8]=new LogConverter();
converters[9]=new SavedAgentConverter(cs);
converters[9]=new LogConverter();
converters[10]=new SavedAgentConverter(cs);

converters[10]= new GamaPopulationConverter(cs);
converters[11]= new GamaSpeciesConverter(cs);
converters[12]= new ReferenceAgentConverter(cs);
converters[13]= new GamaPathConverter(cs);
converters[11]= new GamaPopulationConverter(cs);
converters[12]= new GamaSpeciesConverter(cs);
converters[13]= new ReferenceAgentConverter(cs);
converters[14]= new GamaPathConverter(cs);

converters[14]= new GamaBDIPlanConverter(cs);
converters[15]= new GamaBDIPlanConverter(cs);

//converters[12]= new ComplexMessageConverter(cs);

Expand Down Expand Up @@ -92,7 +94,7 @@ public static Converter[] converterFactory(ConverterScope cs)
// TODO Remove when possible
private static Converter[] loadConverterNetwork(ConverterScope cs)
{
Converter[] converters= new Converter[14];
Converter[] converters= new Converter[15];
converters[0]= new GamaBasicTypeConverter(cs);
converters[1]=new GamaAgentConverterNetwork(cs);
converters[2]=new GamaListConverterNetwork(cs);
Expand All @@ -101,15 +103,16 @@ private static Converter[] loadConverterNetwork(ConverterScope cs)
converters[5]=new GamaMatrixConverter(cs);
converters[6]=new GamaGraphConverter(cs);
converters[7]=new GamaFileConverter(cs);
converters[8]=new GamaColorConverter();

converters[8]=new LogConverter();
converters[9]=new SavedAgentConverter(cs);
converters[9]=new LogConverter();
converters[10]=new SavedAgentConverter(cs);

converters[10]= new GamaPopulationConverter(cs);
converters[11]= new GamaSpeciesConverter(cs);
converters[12]= new GamaPathConverter(cs);
converters[11]= new GamaPopulationConverter(cs);
converters[12]= new GamaSpeciesConverter(cs);
converters[13]= new GamaPathConverter(cs);

converters[13]= new GamaBDIPlanConverter(cs);
converters[14]= new GamaBDIPlanConverter(cs);


//converters[12]= new ComplexMessageConverter(cs);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ummisco.gama.serializer.gamaType.converters;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import msi.gama.util.GamaColor;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gama.serializer.gamaType.reduced.GamaColorReducer;

@SuppressWarnings("rawtypes")
public class GamaColorConverter implements Converter {

@Override
public boolean canConvert(final Class arg0) {

if (GamaColor.class.equals(arg0)) {
return true;
}

final Class<?>[] allInterface = arg0.getInterfaces();
for (final Class<?> c : allInterface) {
if (c.equals(GamaColor.class))
return true;
}

final Class superClass = arg0.getSuperclass();
if (superClass != null) {
return canConvert(superClass);
}

return false;
}

@Override
public void marshal(Object arg0, HierarchicalStreamWriter arg1, MarshallingContext arg2) {
final GamaColor mc = (GamaColor) arg0;
DEBUG.OUT("ConvertAnother : GamaColor " + mc.getClass());
arg2.convertAnother(new GamaColorReducer(mc));
DEBUG.OUT("END -- ConvertAnother : GamaColor " + mc.getClass());

}

@Override
public Object unmarshal(HierarchicalStreamReader arg0, UnmarshallingContext arg1) {
final GamaColorReducer gcr = (GamaColorReducer) arg1.convertAnother(null, GamaColorReducer.class);
return gcr.constructObject();
}







}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ummisco.gama.serializer.gamaType.reduced;

import msi.gama.util.GamaColor;

public class GamaColorReducer {


public float getR() {
return r;
}


public void setR(float r) {
this.r = r;
}


public float getG() {
return g;
}


public void setG(float g) {
this.g = g;
}


public float getB() {
return b;
}


public void setB(float b) {
this.b = b;
}


public float getA() {
return a;
}


public void setA(float a) {
this.a = a;
}


private float r;
private float g;
private float b;
private float a;


public GamaColorReducer(GamaColor c) {
r = c.red();
g = c.green();
b = c.blue();
a = c.alpha();
}


public Object constructObject() {
return new GamaColor(r/255.0,g/255.0,b/255.0,a/255.0);
}





}

0 comments on commit 898ae1f

Please sign in to comment.