Android Open Source - bad Bad Converter






From Project

Back to project page bad.

License

The source code is released under:

Copyright (C) 2013 Madis Pink 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 Softwa...

If you think the Android project bad 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.madisp.bad.eval;
/*from   w w w.ja v a2s.c  o  m*/
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: madis
 * Date: 5/4/13
 * Time: 6:41 PM
 */
public class BadConverter {
  public static boolean bool(Object var) {
    var = object(var);
    if (var == null) {
      return false;
    }
    if (var instanceof Boolean) {
      return (Boolean)var;
    }
    if (var instanceof CharSequence) {
      return ((CharSequence) var).length() > 0;
    }
    return true;
  }

  public static String string(Object var) {
    var = object(var);
    return var == null ? null : var.toString();
  }

  public static Object object(Object var) {
    if (var instanceof BadVar) {
      return ((BadVar) var).get();
    }
    return var;
  }

  public static List list(Object var) {
    var = object(var);
    if (var instanceof List) {
      return (List)var;
    } else if (var instanceof Collection) {
      return Collections.list(Collections.enumeration((Collection) var));
    } else if (var instanceof Object[]) {
      return Arrays.asList(var);
    }
    return null;
  }

  public static int integer(Object var) {
    var = object(var);
    if (var instanceof Integer) {
      return (Integer)var;
    } else if (var instanceof String) {
      return Integer.valueOf((String)var);
    }
    return 0;
  }

  public static boolean isCollapsible(Class[] a, Class[] b) {
    if (a.length > b.length+1 || b.length == 0) {
      return false;
    }
    for (int i = 0; i < a.length - 1; i++) {
      if (!isAssignableFrom(a[i], b[i])) {
        return false;
      }
    }
    // is the last type an array type?
    Class last = a[a.length - 1];
    if (!last.isArray()) {
      return false;
    }
    for (int i = a.length; i < b.length; i++) {
      if (!isAssignableFrom(last.getComponentType(), b[i])) {
        return false;
      }
    }
    return true;
  }

  public static Object[] collapse(Class[] dest, Object[] src) {
    if (dest.length == src.length) {
      if (dest.length == 0 || !dest[dest.length-1].isArray()) {
        return src;
      } else if (isAssignableFrom(dest[dest.length-1], src[src.length-1].getClass())) {
        return src;
      }
    }
    Object[] ret = new Object[dest.length];
    Object[] lastArg = (Object[])Array.newInstance(dest[dest.length - 1].getComponentType(), src.length - (dest.length-1));
    for (int i = 0; i < lastArg.length; i++) {
      lastArg[i] = src[dest.length - 1 + i];
    }
    for (int i = 0; i < ret.length - 1; i++) {
      ret[i] = src[i];
    }
    ret[ret.length - 1] = lastArg;
    return ret;
  }

  public static boolean areAssignableFrom(Class[] a, Class[] b) {
    if (a == null || b == null || a.length != b.length) {
      return false;
    }
    for (int i = 0; i < a.length; i++) {
      if (!isAssignableFrom(a[i], b[i])) {
        return false;
      }
    }
    return true;
  }

  @SuppressWarnings("unchecked")
  public static boolean isAssignableFrom(Class a, Class b) {
    // same class
    if (a.equals(b)) {
      return true;
    }
    // byte
    if (a.equals(byte.class) && b.equals(Byte.class)) {
      return true;
    }
    if (b.equals(byte.class) && a.equals(Byte.class)) {
      return true;
    }
    // short
    if (a.equals(short.class) && b.equals(Short.class)) {
      return true;
    }
    if (b.equals(short.class) && a.equals(Short.class)) {
      return true;
    }
    // int
    if (a.equals(int.class) && b.equals(Integer.class)) {
      return true;
    }
    if (b.equals(int.class) && a.equals(Integer.class)) {
      return true;
    }
    // long
    if (a.equals(long.class) && b.equals(Long.class)) {
      return true;
    }
    if (b.equals(long.class) && a.equals(Long.class)) {
      return true;
    }
    // float
    if (a.equals(float.class) && b.equals(Float.class)) {
      return true;
    }
    if (b.equals(float.class) && a.equals(Float.class)) {
      return true;
    }
    // double
    if (a.equals(double.class) && b.equals(Double.class)) {
      return true;
    }
    if (b.equals(double.class) && a.equals(Double.class)) {
      return true;
    }
    // boolean
    if (a.equals(boolean.class) && b.equals(Boolean.class)) {
      return true;
    }
    if (b.equals(boolean.class) && a.equals(Boolean.class)) {
      return true;
    }
    // char
    if (a.equals(char.class) && b.equals(Character.class)) {
      return true;
    }
    if (b.equals(char.class) && a.equals(Character.class)) {
      return true;
    }
    // other, inheritance, etc
    return a.isAssignableFrom(b);
  }
}




Java Source Code List

com.madisp.bad.ConsoleRepl.java
com.madisp.bad.Freezer.java
com.madisp.bad.Persist.java
com.madisp.bad.decor.BadDecorator.java
com.madisp.bad.decor.BaseDecorator.java
com.madisp.bad.decor.CheckableDecorator.java
com.madisp.bad.decor.EditTextDecorator.java
com.madisp.bad.decor.ListViewDecorator.java
com.madisp.bad.decor.TextViewDecorator.java
com.madisp.bad.decor.ViewDecorator.java
com.madisp.bad.decor.WebViewDecorator.java
com.madisp.bad.demo.ListFragment.java
com.madisp.bad.demo.LoginFragment.java
com.madisp.bad.demo.MainActivity.java
com.madisp.bad.demo.PersistFragment.java
com.madisp.bad.demo.ReplFragment.java
com.madisp.bad.demo.ShopFragment.java
com.madisp.bad.demo.TwoWayFragment.java
com.madisp.bad.eval.BadCollections.java
com.madisp.bad.eval.BadConverter.java
com.madisp.bad.eval.BadScope.java
com.madisp.bad.eval.BadVar.java
com.madisp.bad.eval.ScopeWrapper.java
com.madisp.bad.eval.Scope.java
com.madisp.bad.eval.Watcher.java
com.madisp.bad.expr.AndExpression.java
com.madisp.bad.expr.AssignExpression.java
com.madisp.bad.expr.BasableExpression.java
com.madisp.bad.expr.BlockExpression.java
com.madisp.bad.expr.ConstantExpression.java
com.madisp.bad.expr.DivisionExpression.java
com.madisp.bad.expr.ExpressionFactory.java
com.madisp.bad.expr.Expression.java
com.madisp.bad.expr.MethodExpression.java
com.madisp.bad.expr.MinusExpression.java
com.madisp.bad.expr.MultiplyExpression.java
com.madisp.bad.expr.NotExpression.java
com.madisp.bad.expr.OrExpression.java
com.madisp.bad.expr.PlusExpression.java
com.madisp.bad.expr.StatementList.java
com.madisp.bad.expr.UnaryMinusExpression.java
com.madisp.bad.expr.VarExpression.java
com.madisp.bad.lib.BadFragment.java
com.madisp.bad.lib.BadLayoutFactory.java
com.madisp.bad.lib.BadStdLib.java
com.madisp.bad.lib.ui.BadAdapter.java
com.madisp.bad.lib.ui.BadTextWatcher.java
com.madisp.bad.parse.ExprBaseVisitor.java
com.madisp.bad.parse.ExprLexer.java
com.madisp.bad.parse.ExprParser.java
com.madisp.bad.parse.ExprVisitor.java