CPD Results

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

Duplications

File Line
org\djutils\data\serialization\DoubleScalarSerializer.java 24
org\djutils\data\serialization\FloatScalarSerializer.java 24
public class DoubleScalarSerializer<U extends Unit<U>, S extends DoubleScalarInterface<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<>();

    /**
     * Serialize a DoubleScalar value to text in such a way that it can be deserialized with the corresponding deserializer. In
     * this case, it serializes the class name of the Scalar, followed by a hash sign, followed by the printed value of the
     * scalar including the unit. So a Length of 12.5 kilometer will return "org.djunits.value.vdouble.scalar.Length#12.5 km".
     * @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 Object value)
    {
        return value.getClass().getName() + "#" + ((S) value).toTextualString();
    }

    /**
     * Deserialize a String to the correct DoubleScalar value in line with the DoubleScalarSerializer.serialize method. In this
     * case, it assumes a string with the class name of the Scalar, followed by a hash sign, followed by the printed value of
     * the scalar including the unit. So, the String "org.djunits.value.vdouble.scalar.Length#12.5 km" will be deserialized into
     * a Length of 12500 m with LengthUnit.KILOMETER as the display unit. 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 String text)
    {
        String[] parts = text.split("#");
        try
        {
            Method valueOfMethod = valueOfMethodCache.get(parts[0]);
            if (valueOfMethod == null)
            {
                Class<?> scalarClass = Class.forName(parts[0]);
                valueOfMethod = scalarClass.getDeclaredMethod("valueOf", String.class);
                valueOfMethodCache.put(parts[0], valueOfMethod);
            }
            return (S) valueOfMethodCache.get(parts[0]).invoke(null, parts[1]);
        }
        catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
                | NoSuchMethodException | SecurityException exception)
        {
            throw new RuntimeException(exception);
        }
    }

}
File Line
org\djutils\data\json\JSONData.java 227
org\djutils\data\xml\XMLData.java 236
                jr.endObject();

                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);
                    }
                }
                @SuppressWarnings({"rawtypes", "unchecked"})
                DataColumn<?> column = new SimpleDataColumn(columnProperties[1], columnProperties[2], valueClass);
                columns.add(column);
                index++;
            }
File Line
org\djutils\data\csv\CSVData.java 133
org\djutils\data\csv\TSVData.java 55
        writeData(writer, metaWriter, dataTable, ',', '"', '\\', "\n");
    }

    /**
     * Write the data from the data table in CSV format.
     * @param filename String; the file name to write the data to
     * @param metaFilename String; the file name to write the metadata to
     * @param dataTable DataTable; the data table to write
     * @throws IOException on I/O error when writing the data
     * @throws TextSerializationException on unknown data type for serialization
     */
    public static void writeData(final String filename, final String metaFilename, final DataTable dataTable)
            throws IOException, TextSerializationException
    {
        FileWriter fw = null;
        FileWriter mfw = null;
        try
        {
            fw = new FileWriter(filename);
            mfw = new FileWriter(metaFilename);
            writeData(fw, mfw, dataTable);
        }
        finally
        {
            if (null != fw)
            {
                fw.close(); // May have already been closed when the CSV writer was closed, but multiple close is harmless
            }
            if (null != mfw)
            {
                mfw.close();
            }
        }
    }

    /**
     * Read the data from the CSV-file into the data table. Use the metadata to reconstruct the data table.
     * @param reader Reader; the reader that can read the data, e.g. from a file
     * @param metaReader Reader; the writer for the metadata
     * @return dataTable the data table reconstructed from the meta data and filled with the data
     * @param separator char; the delimiter to use for separating entries
     * @param quotechar char; the character to use for quoted elements
     * @param escapechar char; the character to use for escaping quotechars or escapechars
     * @param lineEnd String; the line feed terminator to use
     * @throws IOException when the CSV data was not formatted right
     * @throws TextSerializationException on unknown data type for serialization
     */
    public static DataTable readData(final Reader reader, final Reader metaReader, final char separator, final char quotechar,