1

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.

nulldev
  • 555
  • 6
  • 16

2 Answers2

1

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.

Stuart Campbell
  • 1,141
  • 11
  • 13
  • You just made me realize that the fields I've declared as ArrayLists don't change much anyways so by turning them into arrays and adding a custom column for them, this technically solves my problem. Thanks for the help! – nulldev Jan 27 '16 at 15:15
0

Found a shorter and easiest way.
As I dont want to change List to Array in my POJO

public class ListStringColumn implements RushColumn<List<String>> {
  @Override
  public String sqlColumnType() {
    return "varchar(255)";
  }
  @Override
  public String serialize(List<String> object, RushStringSanitizer stringSanitizer) {
    return stringSanitizer.sanitize(new Gson().toJson(object.toArray()));
  }
  @Override
  public List<String> deserialize(String value) {
    return new Gson().fromJson(value, new TypeToken<List<String>>(){}.getType());
  }
  @Override
  public Class[] classesColumnSupports() {
    return new Class[]{List.class};
  }
}
Harry Cotte
  • 90
  • 2
  • 14