Android Open Source - Jello Storable






From Project

Back to project page Jello.

License

The source code is released under:

Apache License

If you think the Android project Jello listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.atteo.jello;
//from   w  w  w.  j  a v a2s. co m
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import android.os.Bundle;
import android.util.Pool;

import com.atteo.jello.associations.BelongsTo;
import com.atteo.jello.associations.DatabaseField;
import com.atteo.jello.schema.Schema;
import com.atteo.jello.schema.StorableWriter;
import com.atteo.jello.transaction.TransactionManager;
import com.google.inject.Inject;
import com.google.inject.name.Named;

abstract public class Storable {
  protected Schema schema = null;
  protected String klassName = null;
  protected Class<? extends Storable> thisClass = null;
  static protected FieldComparator comparator;

  protected Field[] dbFields = null;
  protected Field[] belongsToFields = null;

  @Inject
  static protected StorableWriter storableWriter;
  @Inject
  static protected TransactionManager transactionManager;
  @Inject
  static protected Pool<Record> recordPool;
  @Inject
  static protected @Named("maxRecordPages")
  int maxRecordPages;
  @Inject
  static protected @Named("pageSize")
  short pageSize;

  protected Record record;

  private boolean isInDatabase;

  static private byte bundleData[];

  protected Storable() {
    initClass();
  }

  protected Storable(int id) {
    initClass();
    setId(id);
  }

  private void initClass() {
    if (comparator == null)
      comparator = new FieldComparator();

    thisClass = this.getClass();

    if (dbFields == null)
      dbFields = StorableInfo.getDbFields(thisClass, this);

    if (belongsToFields == null)
      belongsToFields = StorableInfo.getBelongsToFields(thisClass, this);

    if (schema == null)
      schema = StorableInfo.getClassSchema(thisClass, this);

    record = recordPool.acquire();
    klassName = thisClass.getCanonicalName();
    if (bundleData == null)
      bundleData = new byte[maxRecordPages * pageSize];
  }

  public String getClassName() {
    return klassName;
  }

  public int getId() {
    return record.getId();
  }

  public Record getRecord() {
    return record;
  }

  public Schema getSchema() {
    return schema;
  }

  public Class<? extends Storable> getStorableClass() {
    return thisClass;
  }

  @SuppressWarnings("unchecked")
  public <T> T load() {
    if (transactionManager.performFindTransaction(this)) {
      isInDatabase = true;
      return (T) this;
    } else {
      isInDatabase = false;
      return null;
    }

  }

  public void save() {
    if (!beforeSave())
      return;

    if (isInDatabase) {
      update();
      afterSave();
    } else {
      insert();
      afterSave();
    }
    isInDatabase = true;

    afterSave();
  }

  private void insert() {
    transactionManager.performInsertTransaction(this);
  }

  private void update() {
    transactionManager.performInsertTransaction(this);
  }

  public void remove() {
    transactionManager.performDeleteTransaction(this);
  }

  public void setId(final int id) {
    record.setId(id);
  }

  public void setRecord(final Record record) {
    this.record = record;
  }

  Schema createClassSchema() {
    final Schema schema = new Schema();

    int l = dbFields.length;
    int b = belongsToFields.length;

    schema.fields = new int[l + b];
    schema.names = new String[l + b];

    for (int i = 0; i < l; i++) {
      schema.fields[i] = Schema.getFieldType(dbFields[i].getType());
      schema.names[i] = dbFields[i].getName();
    }

    for (int i = 0; i < b; i++) {
      schema.fields[i + l] = Schema.FIELD_STORABLE;
      schema.names[i + l] = belongsToFields[i].getName();
    }

    return schema;
  }

  Field[] extractDbFields() {
    final ArrayList<Field> fields = new ArrayList<Field>();
    Class<?> k = thisClass;
    Field field;
    while (!k.equals(Object.class)) {
      final Field[] declaredFields = k.getDeclaredFields();
      for (int i = declaredFields.length - 1; i >= 0; i--) {
        field = declaredFields[i];
        if (field.isAnnotationPresent(DatabaseField.class)) {
          field.setAccessible(true);
          fields.add(field);
        }
      }
      k = k.getSuperclass();
    }
    Field[] f = fields.toArray(new Field[fields.size()]);
    Arrays.sort(f, comparator);

    return f;
  }

  Object getDbFieldValue(String name) {
    Field f = getDbField(name);
    try {
      return f.get(this);
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }

    return null;
  }

  Field[] extractBelongsToFields() {
    final ArrayList<Field> fields = new ArrayList<Field>();
    Class<?> k = thisClass;
    Field field;
    while (!k.equals(Object.class)) {
      final Field[] declaredFields = k.getDeclaredFields();
      for (int i = declaredFields.length - 1; i >= 0; i--) {
        field = declaredFields[i];
        if (field.isAnnotationPresent(BelongsTo.class)) {
          field.setAccessible(true);
          fields.add(field);
        }
      }
      k = k.getSuperclass();
    }
    Field[] f = fields.toArray(new Field[fields.size()]);
    Arrays.sort(f, comparator);

    return f;
  }

  private Field findField(Field[] fields, String name) {
    int len = fields.length;
    int left = 0;
    int right = len;

    int r;

    while ((r = fields[(right + left) / 2].getName().compareTo(name)) != 0) {
      if (r < 0)
        left = (right + left) / 2 + 1;
      else
        right = (right + left) / 2 - 1;

      if (right < left)
        return null;
    }

    return fields[(right + left) / 2];

  }

  public Field getDbField(String name) {
    Field f = findField(dbFields, name);
    if (f == null)
      f = findField(belongsToFields, name);
    return f;
  }

  @Override
  protected void finalize() {
    recordPool.release(record);
  }

  private class FieldComparator implements Comparator<Field> {
    public int compare(final Field field1, final Field field2) {
      return field1.getName().compareTo(field2.getName());
    }
  }

  public boolean loadBundle(final Bundle bundle) {
    if (!beforeLoadBundle())
      return false;
    if (bundle == null)
      throw new NullPointerException("Provided bundle is null");

    bundleData = bundle.getByteArray("content");

    if (bundleData == null)
      return false;

    storableWriter.readStorable(bundleData, this, schema);

    afterLoadBundle();
    return true;
  }

  public Bundle toBundle() {
    if (!beforeSaveToBundle())
      return null;
    final Bundle result = new Bundle();
    storableWriter.writeStorable(bundleData, this, schema);
    result.putByteArray("content", bundleData);

    afterSaveToBundle();
    return result;
  }

  protected void afterDelete() {

  }

  protected void afterLoad() {

  }

  protected void afterLoadBundle() {

  }

  protected void afterSave() {

  }

  protected void afterSaveToBundle() {

  }

  protected boolean beforeDelete() {
    return true;
  }

  protected boolean beforeLoad() {
    return true;
  }

  protected boolean beforeLoadBundle() {
    return true;
  }

  protected boolean beforeSave() {
    return true;
  }

  protected boolean beforeSaveToBundle() {
    return true;
  }
}




Java Source Code List

android.util.FinitePool.java
android.util.Pool.java
android.util.PoolableManager.java
android.util.Poolable.java
android.util.Pools.java
android.util.SynchronizedPool.java
com.atteo.jello.DatabaseFile.java
com.atteo.jello.Expression.java
com.atteo.jello.JelloModule.java
com.atteo.jello.Jello.java
com.atteo.jello.PageUsage.java
com.atteo.jello.RecordPoolableManager.java
com.atteo.jello.Record.java
com.atteo.jello.StorableCollection.java
com.atteo.jello.StorableFactory.java
com.atteo.jello.StorableInfo.java
com.atteo.jello.Storable.java
com.atteo.jello.associations.BelongsTo.java
com.atteo.jello.associations.DatabaseField.java
com.atteo.jello.associations.HasMany.java
com.atteo.jello.index.BTree.java
com.atteo.jello.index.IndexFactory.java
com.atteo.jello.index.IndexModule.java
com.atteo.jello.index.Index.java
com.atteo.jello.index.PagePoolProxy.java
com.atteo.jello.klass.KlassManager.java
com.atteo.jello.klass.SimpleKlassManager.java
com.atteo.jello.schema.SchemaManagerFactory.java
com.atteo.jello.schema.SchemaManager.java
com.atteo.jello.schema.SchemaModule.java
com.atteo.jello.schema.Schema.java
com.atteo.jello.schema.SimpleSchemaManager.java
com.atteo.jello.schema.StorableWriter.java
com.atteo.jello.schema.VanillaStorableWriter.java
com.atteo.jello.space.AppendOnlyCacheNative.java
com.atteo.jello.space.AppendOnlyCache.java
com.atteo.jello.space.AppendOnly.java
com.atteo.jello.space.Hybrid.java
com.atteo.jello.space.NextFitHistogramNative.java
com.atteo.jello.space.NextFitHistogram.java
com.atteo.jello.space.NextFit.java
com.atteo.jello.space.SpaceManagerNative.java
com.atteo.jello.space.SpaceManagerPolicy.java
com.atteo.jello.space.SpaceManager.java
com.atteo.jello.space.SpaceModule.java
com.atteo.jello.space.VanillaHistogram.java
com.atteo.jello.store.HeaderPage.java
com.atteo.jello.store.ListPage.java
com.atteo.jello.store.PagePoolableManager.java
com.atteo.jello.store.PageSizeProvider.java
com.atteo.jello.store.Page.java
com.atteo.jello.store.PagedFileNative.java
com.atteo.jello.store.PagedFileRAF.java
com.atteo.jello.store.PagedFile.java
com.atteo.jello.store.StoreModule.java
com.atteo.jello.transaction.LockManager.java
com.atteo.jello.transaction.SimpleLockManager.java
com.atteo.jello.transaction.SimpleTransactionManager.java
com.atteo.jello.transaction.TransactionManager.java
com.atteo.jello.transaction.TransactionModule.java