Package org.nutz.lang.util

Examples of org.nutz.lang.util.Context


   * 保存对象到attribute
   */
  public static void putRequestAttribute(HttpServletRequest req, Object re){
    if (null != re){
      if(re instanceof Context){
        Context context = (Context) re;
        for(String key : context.keys()){
          req.setAttribute(key, context.get(key));
        }
      } else {
        req.setAttribute(ViewProcessor.DEFAULT_ATTRIBUTE, re);
      }
    }
View Full Code Here


  /**
   * 参数
   */
  @Test
  public void test_simple_condition() {
    Context context = Lang.context();
    context.set("a", 10);
    assertEquals(10, El.eval(context, "a"));
    assertEquals(20, El.eval(context, "a + a"));

    context.set("b", "abc");
    assertEquals(25, El.eval(context, "a + 2 +a+ b.length()"));

    String s = "a>5?'GT 5':'LTE 5'";
    assertEquals("GT 5", El.eval(context, s));
    context.set("a", 5);
    assertEquals("LTE 5", El.eval(context, s));

    assertEquals("jk", El.eval("\"j\"+\"k\""));

  }
View Full Code Here

  }

  @Test
  public void context() {
    Context context = Lang.context();
    List<String> list = new ArrayList<String>();
    list.add("jk");
    context.set("a", list);
    assertEquals("jk", El.eval(context, "a.get((1-1))"));
    assertEquals("jk", El.eval(context, "a.get(1-1)"));
    assertEquals("jk", El.eval(context, "a.get(0)"));

    assertTrue((Boolean) El.eval(Lang.context(), "a==null"));
View Full Code Here

  /**
   * 数组测试
   */
  @Test
  public void array() {
    Context context = Lang.context();
    String[] str = new String[]{"a", "b", "c"};
    String[][] bb = new String[][]{{"a", "b"}, {"c", "d"}};
    context.set("a", str);
    context.set("b", bb);
    assertEquals("b", El.eval(context, "a[1]"));
    assertEquals("b", El.eval(context, "a[1].toString()"));
    assertEquals("b", El.eval(context, "a[2-1]"));
    assertEquals("d", El.eval(context, "b[1][1]"));
  }
View Full Code Here

  public void field() {
    class abc {
      @SuppressWarnings("unused")
      public String name = "jk";
    }
    Context context = Lang.context();
    context.set("a", new abc());
    assertEquals("jk", El.eval(context, "a.name"));
    // 这个功能放弃
    // assertFalse((Boolean)El.eval("java.lang.Boolean.FALSE"));
    // assertFalse((Boolean)El.eval("Boolean.FALSE"));
  }
View Full Code Here

  public void speed() {
    SimpleSpeedTest z = new SimpleSpeedTest();
    int num = 4988;
    String elstr = "num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7)-z.abc(i)";
    int i = 5000;
    Context con = Lang.context();
    con.set("num", num);
    con.set("i", i);
    con.set("z", z);
    assertEquals(num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7) - z.abc(i), El.eval(con, elstr));
  }
View Full Code Here

  /**
   * map测试
   */
  @Test
  public void map() {
    Context context = Lang.context();
    context.set("a", Lang.map("{x:10,y:50,txt:'Hello'}"));

    assertEquals(100, El.eval(context, "a.get('x')*10"));
    assertEquals(100, El.eval(context, "a.x*10"));
    assertEquals(100, El.eval(context, "a['x']*10"));
    assertEquals("Hello-40", El.eval(context, "a.get('txt')+(a.get('x')-a.get('y'))"));
View Full Code Here

  /**
   * list测试
   */
  @Test
  public void list() {
    Context context = Lang.context();
    List<String> list = new ArrayList<String>();
    context.set("b", list);
    assertEquals(0, El.eval(context, "b.size()"));
    list.add("");
    assertEquals(1, El.eval(context, "b.size()"));
    El.eval(context, "b.add('Q\nQ')");
    assertEquals(2, El.eval(context, "b.size()"));
View Full Code Here

            * 71, El.eval("1000+100.0*99-(600-3*15)%(((68-9)-3)*2-100)+10000%7*71"));
    assertEquals6.7 - 100 > 39.6 ? true ? 4 + 5 : 6 - 1
                    : !(100 % 3 - 39.0 < 27) ? 8 * 2 - 199 : 100 % 3,
            El.eval("6.7-100>39.6 ? 5==5? 4+5:6-1 : !(100%3-39.0<27) ? 8*2-199: 100%3"));

    Context vars = Lang.context();
    vars.set("i", 100);
    vars.set("pi", 3.14f);
    vars.set("d", -3.9);
    vars.set("b", (byte) 4);
    vars.set("bool", false);
    vars.set("t", "");
    String t = "i * pi + (d * b - 199) / (1 - d * pi) - (2 + 100 - i / pi) % 99 ==i * pi + (d * b - 199) / (1 - d * pi) - (2 + 100 - i / pi) % 99";
    // t =
    // "i * pi + (d * b - 199) / (1 - d * pi) - (2 + 100 - i / pi) % 99";
    assertEquals(true, El.eval(vars, t));

View Full Code Here

  }

  public static void main(String[] args) throws SecurityException, NoSuchMethodException {
    final SimpleSpeedTest z = new SimpleSpeedTest();
    final String elstr = "num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7)-z.abc(i)";
    final Context context = Lang.context("{num:0}");
    context.set("z", z);

    System.out.println("\n" + Strings.dup('=', 100));

    Stopwatch sw = Stopwatch.run(new Atom() {
      public void run() {
        int num = 0;
        for (int i = 0; i < max; i++)
          num = num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7) - z.abc(i);
        System.out.println("Num: " + num);
      }
    });

    System.out.println("\n" + Strings.dup('=', 100));
   
    Stopwatch sw3 = Stopwatch.run(new Atom() {
      public void run() {
        try {
          context.set("num", 0);
          for (int i = 0; i < max; i++)
            context.set("num", El.eval(context.set("i", i), elstr));
          System.out.println("Num: " + context.getInt("num"));
        }
        catch (Exception e) {
          throw Lang.wrapThrow(e);
        }
      }
    });
    System.out.println("\n" + Strings.dup('=', 100));
   
    Stopwatch sw4 = Stopwatch.run(new Atom() {
      public void run() {
        try {
          El el2pre = new El(elstr);
          context.set("num", 0);
          context.set("z", z);
          for (int i = 0; i < max; i++)
            context.set("num", el2pre.eval(context.set("i", i)));
          System.out.println("Num: " + context.getInt("num"));
        }
        catch (Exception e) {
          throw Lang.wrapThrow(e);
        }
      }
    });
    System.out.println("\n" + Strings.dup('=', 100));
   
    Stopwatch sw5 = Stopwatch.run(new Atom() {
      public void run() {
        try {
          El el2pre = new El(elstr);
          context.set("num", 0);
          context.set("z", z);
          for (int i = 0; i < max; i++)
            context.set("num", el2pre.eval(context.set("i", i)));
          System.out.println("Num: " + context.getInt("num"));
        }
        catch (Exception e) {
          throw Lang.wrapThrow(e);
        }
      }
View Full Code Here

TOP

Related Classes of org.nutz.lang.util.Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.