Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactoring.
  • Loading branch information
nursultanturdaliev committed Feb 2, 2016
commit 341ed657d3c2d6a044b9c9f9be207d3cfe4723a1
2 changes: 1 addition & 1 deletion library/src/main/java/com/orm/SugarContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private SugarContext(Context context) {
this.entitiesMap = Collections.synchronizedMap(new WeakHashMap<Object, Long>());
}

public static SugarContext getSugarContext() {
public static SugarContext getInstance() {
if (instance == null) {
throw new NullPointerException("SugarContext has not been initialized properly. Call SugarContext.init(Context) in your Application.onCreate() method and SugarContext.terminate() in your Application.onTerminate() method.");
}
Expand Down
19 changes: 9 additions & 10 deletions library/src/main/java/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
import java.util.Map;
import java.util.NoSuchElementException;

import static com.orm.SugarContext.getSugarContext;

public class SugarRecord {

public static final String SUGAR = "Sugar";
private Long id = null;

private static SQLiteDatabase getSugarDataBase() {
return getSugarContext().getSugarDb().getDB();
return SugarContext.getInstance().getSugarDb().getDB();
}

public static <T> int deleteAll(Class<T> type) {
Expand Down Expand Up @@ -126,7 +125,7 @@ public static <T> int deleteInTx(Collection<T> objects) {
public static <T> List<T> listAll(Class<T> type) {
return find(type, null, null, null, null, null);
}

public static <T> List<T> listAll(Class<T> type, String orderBy) {
return find(type, null, null, null, orderBy, null);
}
Expand Down Expand Up @@ -210,7 +209,7 @@ public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type){
try {
while (cursor.moveToNext()) {
entity = type.getDeclaredConstructor().newInstance();
inflate(cursor, entity, getSugarContext().getEntitiesMap());
inflate(cursor, entity, SugarContext.getInstance().getEntitiesMap());
result.add(entity);
}
} catch (Exception e) {
Expand Down Expand Up @@ -261,7 +260,7 @@ public static long save(Object object) {
}

static long save(SQLiteDatabase db, Object object) {
Map<Object, Long> entitiesMap = getSugarContext().getEntitiesMap();
Map<Object, Long> entitiesMap = SugarContext.getInstance().getEntitiesMap();
List<Field> columns = ReflectionUtil.getTableFields(object.getClass());
ContentValues values = new ContentValues(columns.size());
Field idField = null;
Expand All @@ -284,7 +283,7 @@ static long save(SQLiteDatabase db, Object object) {
if (idField != null) {
idField.setAccessible(true);
try {
idField.set(object, new Long(id));
idField.set(object, id);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Expand All @@ -305,7 +304,7 @@ public static long update(Object object) {
}

static long update(SQLiteDatabase db, Object object) {
Map<Object, Long> entitiesMap = getSugarContext().getEntitiesMap();
Map<Object, Long> entitiesMap = SugarContext.getInstance().getEntitiesMap();
List<Field> columns = ReflectionUtil.getTableFields(object.getClass());
ContentValues values = new ContentValues(columns.size());

Expand Down Expand Up @@ -381,7 +380,7 @@ public boolean delete() {
return false;
}
}

public static boolean delete(Object object) {
Class<?> type = object.getClass();
if (type.isAnnotationPresent(Table.class)) {
Expand Down Expand Up @@ -422,7 +421,7 @@ public long update() {

@SuppressWarnings("unchecked")
void inflate(Cursor cursor) {
inflate(cursor, this, getSugarContext().getEntitiesMap());
inflate(cursor, this, SugarContext.getInstance().getEntitiesMap());
}

public Long getId() {
Expand Down Expand Up @@ -460,7 +459,7 @@ public E next() {

try {
entity = type.getDeclaredConstructor().newInstance();
inflate(cursor, entity, getSugarContext().getEntitiesMap());
inflate(cursor, entity, SugarContext.getInstance().getEntitiesMap());
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/orm/SugarTransactionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class SugarTransactionHelper {

public static void doInTransaction(SugarTransactionHelper.Callback callback) {
SQLiteDatabase database = SugarContext.getSugarContext().getSugarDb().getDB();
SQLiteDatabase database = SugarContext.getInstance().getSugarDb().getDB();
database.beginTransaction();

try {
Expand Down