Advertisement
dwhitzzz

toStringOrNull

Mar 9th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. v 1.0
  2.     private static String toStringOrNull(Object o) {
  3.             if(o instanceof String)
  4.                 return (!"".equals(o) && o != null) ? (String)o : "";
  5.             else if(o instanceof Date)
  6.                 return (o != null) ? new SimpleDateFormat("dd-MM-yyyy").format(o) : "";
  7.             else
  8.                 return ( o != null)? o.toString() : "";
  9.     }
  10.  
  11. V 2.10
  12.  
  13. private static String toStringOrNull(Object o) {
  14.         return (o != null) ? o.toString() : "";
  15.     }
  16.  
  17.     private static String toStringOrNull(String o) {
  18.         return (o != null  &&
  19.                 o.length()>0
  20.                 && !"null".equalsIgnoreCase(o))
  21.                 ? (String) o : "";
  22.     }
  23.  
  24.     private static String toStringOrNull(Date o) {
  25.         return (o != null) ? new SimpleDateFormat("dd-MM-yyyy").format(o) : "";
  26.     }
  27.  
  28.     private static String toStringOrNull(Date o, String formatStr) {
  29.         return (o != null) ? new SimpleDateFormat(formatStr).format(o) : "";
  30.     }
  31.  
  32.     private static String toStringOrNull(BigDecimal o, Boolean flagNumDecimale) {
  33.         if (flagNumDecimale){
  34.             if(o != null){
  35.                 if( ( o.doubleValue() % 1) == 0) //se non ha decimali
  36.                     return Integer.valueOf(o.intValue()).toString() + ",00";
  37.                 else{
  38.                     String[] splitter = o.toString().split("\\.");
  39.                     splitter[0].length();
  40.                     if( splitter[1].length() < 2)
  41.                         return o.toString()+"0";
  42.                 }
  43.             }
  44.             else
  45.                 return "0,00";
  46.            
  47. //          return (o != null) ?
  48. //          ( ((((BigDecimal) o).doubleValue() % 1) == 0) ?
  49. //          o.toString() + ",00"
  50. //          : o.toString())
  51. //          : "";
  52.         }
  53.        
  54.         return  o!= null ? o.toString() : "";
  55.        
  56.     }
  57.  
  58.     private static String toStringOrNull(Double o, Boolean flagNumDecimale) {
  59.         if (flagNumDecimale){
  60.             if(o != null){
  61.                 if( ( ( (Double) o).doubleValue() % 1) == 0) //se non ha decimali
  62.                     return Integer.valueOf(o.intValue()).toString() + ",00";
  63.                 else{
  64.                    
  65.                     String[] splitter = o.toString().split("\\.");
  66.                     splitter[0].length();
  67.                     if( splitter[1].length() < 2)
  68.                         return o.toString()+"0";
  69. //                  double x = o - Math.floor(o);
  70. //                  x = x - (int) x * 10;
  71. //                  x = Math.pow(x, 10);
  72. //                  //se ha un decimale aggiungo uno 0
  73. //                  return x <= 9 ? o.toString()+"0" : o.toString();
  74.                 }
  75.             }else
  76.                 return "0,00";
  77.                
  78. //          return (o != null) ? ( ((((Double) o).doubleValue() % 1) == 0) ? Integer
  79. //                  .valueOf(o.intValue()).toString() + ",00"
  80. //                  : o.toString() )
  81. //                  : "";
  82.         }
  83.             return (o != null) ? o.toString() : "";
  84.     }
  85.    
  86.     private static double doubleOrNull(Object o){
  87.         if(o!= null){
  88.             int scale = (int) Math.pow(10, 3);
  89.             return (double) Math.round((Double)o * scale) / scale;
  90.         }
  91.         return (Double)0.00;
  92.     }
  93.    
  94.     private static double doubleOrNull(BigDecimal o){
  95.         if(o!= null){
  96.             int scale = (int) Math.pow(10, 3);
  97.             return (double) Math.round(o.doubleValue() * scale) / scale;
  98.         }
  99.         return (Double)0.00;
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement