Changeset 170:6effb7db0936 in finroc_plugins_data_types-java


Ignore:
Timestamp:
23.02.2019 11:33:02 (3 weeks ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Tags:
tip
Message:

Adapts/aligns quantity type adapter to/with changes in rrlib_si_units

File:
1 edited

Legend:

Unmodified
Added
Removed
  • QuantityTypeAdapter.java

    r155 r170  
    3939 * Adapts rrlib::si_units::tQuantity template types to CoreNumbers 
    4040 */ 
    41 public class QuantityTypeAdapter extends RemoteTypeAdapter { 
     41public class QuantityTypeAdapter extends RemoteTypeAdapter.Default { 
    4242 
    4343    public static final QuantityTypeAdapter INSTANCE = new QuantityTypeAdapter(); 
    4444 
    4545    private QuantityTypeAdapter() { 
    46         super(10); 
     46        super(); 
    4747    } 
    4848 
     
    6464    }; 
    6565 
     66    static Type getElementaryType(String elementaryTypeName, int typeSize) throws Exception { 
     67        for (int i = 0; i <= Type.DOUBLE.ordinal(); i++) { 
     68            if (elementaryTypeName.equalsIgnoreCase(Type.values()[i].toString())) { 
     69                return Type.values()[i]; 
     70            } 
     71        } 
     72        if (elementaryTypeName.contains("Angle")) { 
     73            if ((typeSize == 4 || typeSize == 8)) { 
     74                return typeSize == 4 ? Type.FLOAT : Type.DOUBLE; 
     75            } else if (typeSize == 0) { 
     76                // legacy Finroc runtime 
     77                return elementaryTypeName.contains("float") ? Type.FLOAT : Type.DOUBLE; 
     78            } 
     79        } 
     80        throw new Exception("Elementary type '" + elementaryTypeName + "' not found"); 
     81    } 
     82 
    6683    @Override 
    6784    public boolean handlesType(RemoteType remoteType, Info adapterInfo) { 
     
    7592            if (arguments.length == 2) { 
    7693                try { 
    77                     adapterInfo.customAdapterData1 = SIUnit.getInstance(arguments[0]); 
    78                     adapterInfo.customAdapterData2 = (arguments[1].trim().equalsIgnoreCase("float")) ? Float.class : Double.class; 
     94                    adapterInfo.customAdapterData2 = SIUnit.getInstance(arguments[0]); 
     95                    adapterInfo.customAdapterData1 = getElementaryType(arguments[1].trim(), remoteType.getSize()); 
    7996                    adapterInfo.localType = CoreNumber.TYPE; 
    8097                    adapterInfo.networkEncoding = Serialization.DataEncoding.BINARY; 
     
    88105            } else if (remoteType.getName().endsWith("rrlib.math.Angle<double, rrlib.math.angle.Radian, rrlib.math.angle.NoWrap>>")) { // TODO: this hard-coded string is a workaround and not really nice 
    89106                adapterInfo.customAdapterData1 = SIUnit.RAD_PER_SECOND; 
    90                 adapterInfo.customAdapterData2 = Double.class; 
     107                adapterInfo.customAdapterData2 = Type.DOUBLE; 
    91108                adapterInfo.localType = CoreNumber.TYPE; 
    92109                adapterInfo.networkEncoding = Serialization.DataEncoding.BINARY; 
     
    98115                if (LOOKUP[i].equals(typeName)) { 
    99116                    try { 
    100                         adapterInfo.customAdapterData1 = SIUnit.getInstance(LOOKUP[i + 1]); 
    101                         adapterInfo.customAdapterData2 = Double.class; 
     117                        adapterInfo.customAdapterData2 = SIUnit.getInstance(LOOKUP[i + 1]); 
     118                        adapterInfo.customAdapterData1 = Type.DOUBLE; 
     119                        adapterInfo.localType = CoreNumber.TYPE; 
     120                        adapterInfo.networkEncoding = Serialization.DataEncoding.BINARY; 
     121                        return true; 
     122                    } catch (Exception e) { 
     123                        Log.log(LogLevel.WARNING, "Cannot find suitable SI unit for predefined string '" + LOOKUP[i + 1] + "': ", e); 
     124                    } 
     125                } else if (typeName.startsWith(LOOKUP[i] + "<")) { 
     126                    try { 
     127                        adapterInfo.customAdapterData2 = SIUnit.getInstance(LOOKUP[i + 1]); 
     128                        adapterInfo.customAdapterData1 = getElementaryType(typeName.substring(LOOKUP[i].length() + 1, typeName.length() - 1), remoteType.getSize()); 
    102129                        adapterInfo.localType = CoreNumber.TYPE; 
    103130                        adapterInfo.networkEncoding = Serialization.DataEncoding.BINARY; 
     
    115142    @Override 
    116143    public void deserialize(BinaryInputStream stream, GenericObject object, RemoteType type, Info adapterInfo) throws Exception { 
    117         ((CoreNumber)object.getData()).setValue(adapterInfo.customAdapterData2 == Float.class ? stream.readFloat() : stream.readDouble(), (SIUnit)adapterInfo.customAdapterData1); 
     144        super.deserialize(stream, object, type, adapterInfo); 
     145        ((CoreNumber)object.getData()).setUnit((SIUnit)adapterInfo.customAdapterData2); 
    118146    } 
    119147 
    120148    @Override 
    121149    public void serialize(BinaryOutputStream stream, GenericObject object, RemoteType type, Info adapterInfo) { 
    122         SIUnit targetUnit = (SIUnit)adapterInfo.customAdapterData1; 
    123         double value = ((CoreNumber)object.getData()).doubleValue(); 
     150        SIUnit targetUnit = (SIUnit)adapterInfo.customAdapterData2; 
    124151        Unit sourceUnit = ((CoreNumber)object.getData()).getUnit(); 
    125152        if (sourceUnit != null && sourceUnit != targetUnit) { 
    126153            if (sourceUnit.convertibleTo(targetUnit)) { 
    127                 value = sourceUnit.convertTo(value, targetUnit); 
     154                CoreNumber temp = new CoreNumber(); 
     155                temp.copyFrom((CoreNumber)object.getData()); 
     156                temp.setValue(sourceUnit.convertTo(temp.doubleValue(), targetUnit)); 
     157                super.serialize(stream, object, type, adapterInfo); 
     158                return; 
    128159            } else { 
    129160                Log.log(LogLevel.WARNING, "Cannot convert unit " + sourceUnit.toString() + " to target unit " + targetUnit.toString() + ". Forwarding unmodified value."); 
    130161            } 
    131162        } 
    132         if (adapterInfo.customAdapterData2 == Float.class) { 
    133             stream.writeFloat((float)value); 
    134         } else { 
    135             stream.writeDouble(value); 
    136         } 
     163        super.serialize(stream, object, type, adapterInfo); 
    137164    } 
    138165} 
Note: See TracChangeset for help on using the changeset viewer.