Android Open Source - Jello Expression






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   ww  w  . j av a  2 s. c  o  m*/
import java.util.ArrayList;

public class Expression {
  public static final int OPERATOR_GREATER = 0;
  public static final int OPERATOR_LOWER = 1;
  public static final int OPERATOR_GREATER_EQUAL = 2;
  public static final int OPERATOR_LOWER_EQUAL = 3;
  public static final int OPERATOR_EQUAL = 4;
  public static final int OPERATOR_NOT_EQUAL = 5;
  public static final int OPERATOR_IN = 7;
  public static final int OPERATOR_OR = 8;
  public static final int OPERATOR_AND = 9;

  public Expression(Object left, int operator, Object right) {
    this.left = left;
    this.operator = operator;
    this.right = right;
  }

  @SuppressWarnings("unchecked")
  public boolean evaluate(Storable storable) {
    Object leftValue = left, rightValue = right;
    if (leftValue instanceof String && ((String) leftValue).startsWith("."))
      leftValue = storable.getDbFieldValue(((String) leftValue).substring(1));

    if (rightValue instanceof String && ((String) rightValue).startsWith("."))
      rightValue = storable.getDbFieldValue(((String) rightValue).substring(1));

    if (leftValue instanceof Expression)
      leftValue = ((Expression) leftValue).evaluate(storable);

    if (rightValue instanceof Expression)
      rightValue = ((Expression) rightValue).evaluate(storable);

    switch (operator) {
    case OPERATOR_EQUAL:
      return leftValue.equals(rightValue);
    case OPERATOR_NOT_EQUAL:
      return !leftValue.equals(rightValue);
    case OPERATOR_AND:
      return (Boolean) leftValue && (Boolean) rightValue;
    case OPERATOR_OR:
      return (Boolean) leftValue || (Boolean) rightValue;
    case OPERATOR_GREATER:
      if (leftValue instanceof Double)
        return (Double) leftValue > (Double) rightValue;
      if (leftValue instanceof Float)
        return (Float) leftValue > (Float) rightValue;
      if (leftValue instanceof Integer)
        return (Integer) leftValue > (Integer) rightValue;
      if (leftValue instanceof Long)
        return (Long) leftValue > (Long) rightValue;
      if (leftValue instanceof Short)
        return (Short) leftValue > (Short) rightValue;

      throw new RuntimeException("Unknown datatype");
    case OPERATOR_GREATER_EQUAL:
      if (leftValue instanceof Double)
        return (Double) leftValue >= (Double) rightValue;
      if (leftValue instanceof Float)
        return (Float) leftValue >= (Float) rightValue;
      if (leftValue instanceof Integer)
        return (Integer) leftValue >= (Integer) rightValue;
      if (leftValue instanceof Long)
        return (Long) leftValue >= (Long) rightValue;
      if (leftValue instanceof Short)
        return (Short) leftValue >= (Short) rightValue;
      throw new RuntimeException("Unknown datatype");
    case OPERATOR_LOWER:
      if (leftValue instanceof Double)
        return (Double) leftValue < (Double) rightValue;
      if (leftValue instanceof Float)
        return (Float) leftValue < (Float) rightValue;
      if (leftValue instanceof Integer)
        return (Integer) leftValue < (Integer) rightValue;
      if (leftValue instanceof Long)
        return (Long) leftValue < (Long) rightValue;
      if (leftValue instanceof Short)
        return (Short) leftValue < (Short) rightValue;
      throw new RuntimeException("Unknown datatype");
    case OPERATOR_LOWER_EQUAL:
      if (leftValue instanceof Double)
        return (Double) leftValue <= (Double) rightValue;
      if (leftValue instanceof Float)
        return (Float) leftValue <= (Float) rightValue;
      if (leftValue instanceof Integer)
        return (Integer) leftValue <= (Integer) rightValue;
      if (leftValue instanceof Long)
        return (Long) leftValue <= (Long) rightValue;
      if (leftValue instanceof Short)
        return (Short) leftValue <= (Short) rightValue;
      throw new RuntimeException("Unknown datatype");
    case OPERATOR_IN:
      ArrayList<Storable> storables = ((StorableCollection<Storable>) rightValue)
          .toArrayList();
      int l = storables.size();
      int id = (Integer) leftValue;
      for (int i = 0; i < l; i++)
        if (storables.get(i).getId() == id)
          return true;
      return false;
    }

    return false;
  }

  int operator;
  Object left;
  Object right;
}




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