CPD Results

The following document contains the results of PMD's CPD 6.46.0.

Duplications

File Line
org/djutils/data/serialization/DoubleScalarSerializer.java 23
org/djutils/data/serialization/FloatScalarSerializer.java 25
public class DoubleScalarSerializer<U extends Unit<U>, S extends AbstractDoubleScalar<U, S>> implements TextSerializer<S>
{
    /** cache of the retrieved valueOf(String) methods for scalars based on the stored string. */
    private static Map<String, Method> valueOfMethodCache = new LinkedHashMap<>();

    /** cache of the retrieved unit instances based on the unit string. */
    private static Map<String, Unit<?>> unitCache = new LinkedHashMap<>();

    /**
     * Serialize an Scalar value to text in such a way that it can be deserialized with the corresponding deserializer.
     * @param value Object; the scalar to serialize
     * @return String; a string representation of the value that can later be deserialized
     */
    @SuppressWarnings("unchecked")
    @Override
    public String serialize(final S value, final String unitString)
    {
        if (value == null)
        {
            return null;
        }

        String key = value.getClass().getSimpleName() + "_" + unitString;
        Unit<?> unit = unitCache.get(key);
        if (unit == null)
        {
            unit = value.getDisplayUnit().getQuantity().of(unitString);
            unitCache.put(key, unit);
        }
        return String.valueOf(value.getInUnit((U) unit));
    }

    /**
     * Deserialize a String to the correct Scalar value. The method caches the valueOf(String) method for repeated use.
     * @param text String; the text to deserialize
     * @return S; the reconstructed scalar
     */
    @SuppressWarnings("unchecked")
    @Override
    public S deserialize(final Class<S> type, final String text, final String unit)
    {
        if (text == null || text.isEmpty())
        {
            return null;
        }
        try
        {
            Method valueOfMethod = valueOfMethodCache.get(type.getName());
            if (valueOfMethod == null)
            {
                valueOfMethod = type.getDeclaredMethod("valueOf", String.class);
                valueOfMethodCache.put(type.getName(), valueOfMethod);
            }
            return (S) valueOfMethod.invoke(null, text + unit);
        }
        catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
                | SecurityException exception)
        {
            throw new RuntimeException(exception);
        }
    }

}
File Line
org/djutils/data/json/JsonData.java 247
org/djutils/data/xml/XmlData.java 264
Table table;
            Consumer<Object[]> unserializableTable;
            if (tableProperties[2].equals(ListTable.class.getName()))
            {
                ListTable listTable = new ListTable(tableProperties[0], tableProperties[1], columns);
                table = listTable;
                unserializableTable = (
                        data
                ) -> listTable.addRow(data);
            }
            else
            {
                // fallback
                ListTable listTable = new ListTable(tableProperties[0], tableProperties[1], columns);
                table = listTable;
                unserializableTable = (
                        data
                ) -> listTable.addRow(data);
            }

            // obtain the serializers
            TextSerializer<?>[] serializers = new TextSerializer[table.getNumberOfColumns()];
            for (int i = 0; i < table.getNumberOfColumns(); i++)
            {
                serializers[i] = TextSerializer.resolve(columns.get(i).getValueType());
            }
File Line
org/djutils/data/json/JsonData.java 222
org/djutils/data/xml/XmlData.java 240
if (Integer.valueOf(columnProperties[0]).intValue() != index)
                {
                    throw new IOException("column nr not ok");
                }
                String type = columnProperties[3];
                Class<?> valueClass = Primitive.forName(type);
                if (valueClass == null)
                {
                    try
                    {
                        valueClass = Class.forName(type);
                    }
                    catch (ClassNotFoundException exception)
                    {
                        throw new IOException("Could not find class " + type, exception);
                    }
                }
                Column<?> column = new Column<>(columnProperties[1], columnProperties[2], valueClass, columnProperties[4]);