SHARE
TWEET

Untitled

a guest Jul 17th, 2017 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class XmlDecimalAdapter extends XmlAdapter< String, BigDecimal >
  2. {
  3.  
  4.    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  5.  
  6.    @Override
  7.    public String marshal( BigDecimal value ) throws Exception
  8.    {
  9.       final String  stringRepresentation = value.toString();
  10.  
  11.       if( value.compareTo( BigDecimal.ZERO ) < 0 ){
  12.          String result = "-00000000000000";
  13.          return result.substring( 0, result.length() - stringRepresentation.length() + 1 ) + stringRepresentation.substring( 1 );
  14.       }
  15.       String result = "000000000000000";
  16.       return result.substring( 0, result.length() - stringRepresentation.length() ) + stringRepresentation;
  17.    }
  18.  
  19.    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  20.  
  21.    @Override
  22.    public BigDecimal unmarshal( String value ) throws Exception
  23.    {
  24.       if( value.equals( "000000000000000" ) ){
  25.          return BigDecimal.ZERO;
  26.       }  
  27.       if( value.startsWith( "-")  ){
  28.          return new BigDecimal( value.replaceFirst( "^-0*", "-" ) );        
  29.       }
  30.       return new BigDecimal( value.replaceFirst( "^0*", "" ) );        
  31.    }
  32. }
RAW Paste Data
Top