Curson

Additional

Language
Java
Version
1.0.2 (May 10, 2016)
Created
Jan 10, 2016
Updated
May 10, 2016 (Retired)
Owner
Daiki Kajiwara (dkajiwara)
Contributor
Daiki Kajiwara (dkajiwara)
1
Activity
Badge
Generate
Download
Source code

Advertisement

Curson

Cursor binding which uses annotation processing to generate boilerplate code for you.

  • Eliminate getColumnIndex calls by using @CursorRow on fields.

How to use

Create Entity class

You have to create a entity class like below.

class Item {
    @CursorRow("_id"/*CURSOR_COLUMN_NAME*/)
    int id;
    @CursorRow("MEMO")
    String memo;
    @CursorRow("DATE")
    long date;
}

CursorRow annotations supports

  • byte[]
  • int / Integer
  • double / Double
  • float / Float
  • short / Short
  • String

Convert to entity/cursor

To Entity

public Item getItem() {
    Cursor cursor = context.getContentResolver().query(
            ExContentProvider.Contract.MEMO_URI,
            null,
            MemoColumns.MEMO_ID + " = ?",
            new String[]{dataSnapshot.getKey()}, null);
    return Curson.fromCursor(cursor, Item.class, 0);
}

public List<Item> getItems() {
    Cursor cursor = context.getContentResolver().query(
            ExContentProvider.Contract.MEMO_URI,
            null, null, null, null);
    return Curson.fromCursor(cursor, Item.class);
}

To Cursor

public Cursor toCursor() {
    Item item = new Item();
    item.id = 1;
    item.memo = "Hello Curson!!";
    item.date = 1L;
    return Curson.toCursor(item, Item.class);
}

public Cursor toCursor() {
    List<Item> items = new ArrayList<>();
    Item item = new Item();
    item.id = 1;
    item.memo = "Hello Curson!!";
    item.date = 1L;
    items.add(item);
    return Curson.toCursor(items, Item.class);
}

Installation

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'com.github.dkajiwara:curson:1.0.2'
    apt 'com.github.dkajiwara:curson-compiler:1.0.2'
}

License

The MIT License (MIT)

Copyright (c) 2016 daiki kajiwara

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.