@Override public Object fromString(Class targetClass, String s) { try { if (s == null) { return null; } Object o = (Object) s; return o; } catch (NumberFormatException e) { log.error("Number format exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public final String fromBytes(Class targetClass, byte[] bytes) { try { return bytes != null ? new String(bytes, Constants.CHARSET_UTF8) : null; } catch (UnsupportedEncodingException e) { log.error("Unsupported encoding exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public final byte[] toBytes(Object s) throws PropertyAccessException { try { return s != null ? ((String) s).getBytes(Constants.CHARSET_UTF8) : null; } catch (UnsupportedEncodingException e) { log.error("Unsupported encoding exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public Byte fromString(Class targetClass, String s) { try { if (s == null) { return null; } Byte b = new Byte(s); return b; } catch (NumberFormatException e) { log.error("Number fromat exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public UUID fromString(Class targetClass, String s) throws PropertyAccessException { try { if (s == null) { return null; } return UUID.fromString(s); } catch (Exception e) { log.error("Error occured, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public Double fromString(Class targetClass, String s) { try { if (s == null) { return null; } Double d = new Double(s); return d; } catch (NumberFormatException e) { log.error("Number format exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public Boolean fromString(Class targetClass, String s) { try { if (s == null) { return null; } Boolean b = new Boolean(s); return b; } catch (NumberFormatException e) { log.error("Number format exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public Float fromString(Class targetClass, String s) { try { if (s == null) { return null; } Float f = new Float(s); return f; } catch (NumberFormatException e) { log.error("Number format exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public Short fromString(Class targetClass, String s) { try { if (s == null) { return null; } Short sh = new Short(s); return sh; } catch (NumberFormatException e) { log.error("Number format exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public Integer fromString(Class targetClass, String s) { try { if (s == null) { return null; } Integer i = new Integer(s); return i; } catch (NumberFormatException e) { log .error("Number format exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public Enum fromString(Class targetClass, String string) { if (targetClass != null && string != null) { try { return Enum.valueOf(targetClass, string.trim()); } catch (IllegalArgumentException ex) { log.error("Illegal argument exception, Caused by {}.", ex); throw new PropertyAccessException(ex); } } return null; }
@Override public byte[] toBytes(Object object) { ByteArrayOutputStream baos; try { baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(object); oos.close(); return baos.toByteArray(); } catch (IOException e) { log.error("IO exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public Date fromString(Class targetClass, String s) { try { if (s == null) { return null; } Date d = getDateByPattern(s); return d; } catch (NumberFormatException e) { log.error("Number format exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public byte[] toBytes(Object object) throws PropertyAccessException { try { if (object == null) { return null; } UUID uuid = (UUID) object; return asByteArray(uuid); } catch (Exception e) { log.error("Error occured, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public byte[] toBytes(Object object) { if (object == null) { return null; } String s = toString(object); try { return s.getBytes(Constants.ENCODING); } catch (UnsupportedEncodingException e) { log.error("Unsupported encoding exception, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public Enum fromBytes(Class targetClass, byte[] b) { String s = null; try { if (b == null) { return null; } s = new String(b, Constants.ENCODING); } catch (UnsupportedEncodingException e) { log.error("Unsupported encoding exception, Caused by {}.", e); throw new PropertyAccessException(e); } return fromString(targetClass, s); }
@Override public Date fromBytes(Class targetClass, byte[] b) { try { if (b == null) { return null; } // In case date.getTime() is stored in DB. LongAccessor longAccessor = new LongAccessor(); return new Date(longAccessor.fromBytes(targetClass, b)); } catch (Exception e) { log.error("Error occured, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Override public byte[] toBytes(Object object) { try { if (object == null) { return null; } LongAccessor longAccessor = new LongAccessor(); return longAccessor.toBytes(((Date) object).getTime()); // return DATE_FORMATTER.format(((Date) // date)).getBytes(Constants.ENCODING); } catch (Exception e) { log.error("Error occured, Caused by {}.", e); throw new PropertyAccessException(e); } }
@Test public void testPropertyAccessException() { PropertyAccessException exception = new PropertyAccessException("Error with string"); Assert.assertNotNull(exception); exception = new PropertyAccessException(); Assert.assertNotNull(exception); exception = new PropertyAccessException(new RuntimeException()); Assert.assertNotNull(exception); }
@Override public Point fromString(Class targetClass, String s) { if (s == null) return null; WKTReader reader = new WKTReader(); try { return new Point((com.vividsolutions.jts.geom.Point) reader.read(s)); } catch (ParseException e) { log.error("Parse exception, Caused by {}.", e); throw new PropertyAccessException(e); } }