Search code examples
javaandroidormrushorm

Storing lists of boxed primitives/Strings?


Is there any way to store lists of boxed primitives and Strings in RushORM? Something like ArrayList<String> or ArrayList<Integer>? I know I can wrap the int/String in an object that extends RushObject but I'm hoping I don't have to resort to this.

I've already attempted: @RushList(classType = String.class) but it will not compile as the class type must extend RushObject.


Solution

  • What you want to do is implement your own custom column type for those boxed primitives.

    The only thing you will have to do is switch them to a Array String[] or int[] as you wont be able to get type using ArrayList. Here is a int[] custom column.

    public class IntArrayColumn implements RushColumn<int[]> {
    
        private static final String DELIMITER = ",";
    
        @Override
        public String sqlColumnType() {
            return "varchar(255)";
        }
    
        @Override
        public String serialize(int[] ints, RushStringSanitizer rushStringSanitizer) {
            return rushStringSanitizer.sanitize(join(ints));
        }
    
        @Override
        public int[] deserialize(String s) {
            return split(s);
        }
    
        @Override
        public Class[] classesColumnSupports() {
            return new Class[]{int[].class};
        }
    
        private String join(int[] ints) {
            if (ints.length > 0) {
                StringBuilder sbuild = new StringBuilder();
                for (int i = 0; i < ints.length; i++) {
                    sbuild.append(ints[i]).append(DELIMITER);
                }
                sbuild.delete(sbuild.lastIndexOf(DELIMITER), sbuild.length());
                return sbuild.toString();
            } else {
                return "";
            }
        }
    
        private int[] split(String source) {
            if (source != null) {
                if (source.isEmpty()) {
                    return new int[0];
                }
                String[] items = source.split(DELIMITER);
                int[] ints = new int[items.length];
                for (int i = 0; i < ints.length; i++) {
                    ints[i] = Integer.valueOf(items[i]);
                }
                return ints;
            }
            return null;
        }
    }
    

    Then change the initialize function to look like this.

    AndroidInitializeConfig config = new AndroidInitializeConfig(getApplicationContext());
    config.addRushColumn(new IntArrayColumn());
    RushCore.initialize(config);
    

    The only downfall is you wont be able to query anything is the list.

    Hope that helps.