LapisSea

Untitled

Feb 8th, 2020
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1.     public static Map<String, Object> mapObjectValues(Object o){
  2.         Map<String, Object> map=new LinkedHashMap<>();
  3.         mapObjectValues(o, map::put);
  4.         return map;
  5.     }
  6.    
  7.     @Nullable
  8.     public static String firstToLoverCase(@Nullable String string){
  9.         if(string==null||string.isEmpty()) return string;
  10.        
  11.         char[] c=string.toCharArray();
  12.         c[0]=Character.toLowerCase(c[0]);
  13.         return new String(c);
  14.     }
  15.    
  16.     public static void mapObjectValues(Object o, @NotNull BiConsumer<String, Object> push){
  17.         if(o==null) return;
  18.        
  19.         Class c=o.getClass();
  20.         for(Method m : c.getMethods()){
  21.             if(m.getParameterCount()!=0) continue;
  22.             if(Modifier.isPrivate(m.getModifiers())||Modifier.isProtected(m.getModifiers())||Modifier.isStatic(m.getModifiers())) continue;
  23.             if(m.getReturnType()==Void.TYPE) continue;
  24.            
  25.             String name=m.getName();
  26.             if(name.length()<=4) continue;
  27.             if(m.getDeclaringClass()==Object.class) continue;
  28.            
  29.             int prefix;
  30.            
  31.             if((m.getReturnType()==Boolean.class||m.getReturnType()==boolean.class)&&name.startsWith("is")){
  32.                 prefix=2;
  33.             }else{
  34.                 if(!name.startsWith("get")) continue;
  35.                 prefix=3;
  36.             }
  37.            
  38.             char ch=name.charAt(prefix);
  39.             if(!Character.isAlphabetic(ch)||!Character.isUpperCase(ch)) continue;
  40.             name=firstToLoverCase(name.substring(prefix));
  41.            
  42.             try{
  43.                 m.setAccessible(true);
  44.                 push.accept(name, m.invoke(o));
  45.             }catch(Throwable e){
  46.                 push.accept(name, "<read error>");
  47.             }
  48.         }
  49.        
  50.         for(Field f : c.getFields()){
  51.             if(Modifier.isPrivate(f.getModifiers())||
  52.                Modifier.isProtected(f.getModifiers())||
  53.                Modifier.isStatic(f.getModifiers())||
  54.                Modifier.isTransient(f.getModifiers())) continue;
  55.            
  56.             String name=f.getName();
  57.            
  58.             try{
  59.                 f.setAccessible(true);
  60.                 push.accept(name, f.get(o));
  61.             }catch(ReflectiveOperationException e){
  62.                 e.printStackTrace();
  63.             }
  64.         }
  65.     }
  66.    
  67.     private static void writeRow(Writer output, List<String> names, Function<String, String> getter) throws IOException{
  68.         for(int i=0;i<names.size();i++){
  69.             String value=getter.apply(names.get(i));
  70.            
  71.             if(value.contains(",")){
  72.                 value='"'+value.replace("\"", "\"\"")+'"';
  73.             }
  74.            
  75.             output.write(value);
  76.            
  77.             if(i+1<names.size()){
  78.                 output.write(',');
  79.             }else{
  80.                 output.write('\n');
  81.             }
  82.         }
  83.     }
  84.    
  85.     public static <T> void writeCsv(Writer output, List<T> data) throws IOException{
  86.         List<String> names=new ArrayList<>();
  87.         mapObjectValues(data.get(0), (name, v)->names.add(name));
  88.        
  89.         writeRow(output, names, Function.identity());
  90.        
  91.         for(T t : data){
  92.             Map<String, Object> row=mapObjectValues(t);
  93.             writeRow(output, names, name->{
  94.                 Object val=row.get(name);
  95.                 return val==null?"":val.toString();
  96.             });
  97.         }
  98.     }
Add Comment
Please, Sign In to add comment