All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.terrestris.shoguncore.converter.PropertyValueConverter Maven / Gradle / Ivy

There is a newer version: 5.3.13
Show newest version
/**
 *
 */
package de.terrestris.shoguncore.converter;

import org.apache.commons.lang3.math.NumberUtils;

import javax.persistence.AttributeConverter;

/**
 * This converter can be used for the values of the type Map.
 * 

* The values of the map will be converted to strings to persist them in a * string column in the database. A string value coming from the database will * be converted to the best matching Java type. *

* Currently {@link String}, {@link Long}, {@link Double} and {@link Boolean} * values are supported. * * @author Nils Bühner */ public class PropertyValueConverter implements AttributeConverter { /** * Converts an arbitrary object to it's string representation, that will be * stored in the database. */ @Override public String convertToDatabaseColumn(Object attribute) { return attribute.toString(); } /** * Converts a string value from the database to the best matching java * primitive type. */ @Override public Object convertToEntityAttribute(String dbData) { if ("true".equalsIgnoreCase(dbData)) { return true; } else if ("false".equalsIgnoreCase(dbData)) { return false; } else if (NumberUtils.isParsable(dbData)) { if (NumberUtils.isDigits(dbData) || (dbData.startsWith("-") && NumberUtils.isDigits(dbData.substring(1)))) { return Long.parseLong(dbData); } else { return Double.parseDouble(dbData); } } return dbData; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy