Package org.springframework.expression

Examples of org.springframework.expression.TypedValue


    if (key == null) {
      SimpleDateFormat format = new SimpleDateFormat((String)arguments[0]);
      //if first argument is Long, then we assume it's a timestamp in milliseconds type format
      //first argument can also be a Date itself
      if (arguments[1] instanceof Long || arguments[1] instanceof Date) {
        return new TypedValue(format.format(arguments[1]));
      }
      //if the argument is a String, then assume it's a Date represented as a String.
      if (arguments[1] instanceof String) {
        //Assume it's in default formay yyyMMdd
        SimpleDateFormat fromFormat = new SimpleDateFormat(DEFAULT_FORMAT);
        //if the third argument is present, use it as the from date format
        if (arguments.length == 3 && arguments[2] instanceof String)
          fromFormat = new SimpleDateFormat((String)arguments[2]);

          try {
            Date parsedDate = fromFormat.parse((String) arguments[1]);
            return new TypedValue(format.format(parsedDate));
          } catch (ParseException e) {
            throw new AccessException("Unable to format", e);
          }

      }

      return new TypedValue(format.format((Long)arguments[1]));
    }
    throw new AccessException("Unable to format");
  }
View Full Code Here


      buf.append(arguments[i]);
      if (i+1 < arguments.length) {
        buf.append(delimiter);
      }
    }
    return new TypedValue(buf.toString());
  }
View Full Code Here

  public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
    if (arguments[1] instanceof List) {
      try {
        Object[] ranges = ((List<?>) arguments[1]).toArray(new Object[0]);
        int searchIndex = Arrays.binarySearch(ranges, arguments[0]);
        return new TypedValue(ranges[Math.min(searchIndex < 0 ? -searchIndex - 1 : searchIndex, ranges.length - 1)]
            + "_range");
      } catch (Exception e) {
        throw new AccessException("Error finding range", e);
      }
    }
View Full Code Here

  @Override
  public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
    if (arguments[1] instanceof Integer) {
      try {
        Integer buckets = ((Integer)arguments[1]);
        return new TypedValue(Math.abs(arguments[0].hashCode()) % buckets + "_hash");
      } catch (Exception e) {
        throw new AccessException("Error creating hash", e);
      }
    }
    throw new AccessException("Argument " + arguments[1] + " not an Integer");
View Full Code Here

      List<?> list = (List<?>)arguments[1];
      for (Object object : list) {
        if (object instanceof List) {
          List<?> sublist = ((List<?>)object);
          if (sublist.contains(arguments[0])) {
            return new TypedValue(sublist.get(0) + "_list");
          }
        }
      }
    } else {
      throw new AccessException("Argument " + arguments[1] + " not a List");
    }
    // we didn't match anything, return default partition as 'list'
    return new TypedValue("list");
  }
View Full Code Here

      return super.execute(context, target, arguments);
    }
    if (target instanceof Message<?>) {
      Map<?, ?> map = ((Message<?>)target).getHeaders();
      SimpleDateFormat format = new SimpleDateFormat((String)arguments[0]);
      return new TypedValue(format.format(map.get(getKey())));
    }
    throw new AccessException("Unable to format");
  }
View Full Code Here

  @Override
  public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
    if (target instanceof Message) {
      if (PAYLOAD.equals(name)) {
        return new TypedValue(((Message<?>) target).getPayload());
      } else if (HEADERS.equals(name)) {
        return new TypedValue(((Message<?>) target).getHeaders());
      } else {
        return new TypedValue(((Message<?>) target).getHeaders().get(name));
      }
    }
    throw new AccessException("Unable to read " + target + " using " + name);
  }
View Full Code Here

  @Test
  public void testPositiveValues() throws Exception {
    StandardEvaluationContext context = new StandardEvaluationContext();
    HashMethodExecutor executor = new HashMethodExecutor();
    TypedValue value = executor.execute(context, new Object(), 3, 2);
    assertThat((String) value.getValue(), is("1_hash"));
    value = executor.execute(context, new Object(), 4, 2);
    assertThat((String) value.getValue(), is("0_hash"));
    value = executor.execute(context, new Object(), 9, 27);
    assertThat((String) value.getValue(), is("9_hash"));
    value = executor.execute(context, new Object(), 9, 11);
    assertThat((String) value.getValue(), is("9_hash"));
    value = executor.execute(context, new Object(), 11, 22);
    assertThat((String) value.getValue(), is("11_hash"));
    value = executor.execute(context, new Object(), 30, 27);
    assertThat((String) value.getValue(), is("3_hash"));
    value = executor.execute(context, new Object(), 332, 27);
    assertThat((String) value.getValue(), is("8_hash"));
  }
View Full Code Here

  @Test
  public void testNegativeValues() throws Exception {
    StandardEvaluationContext context = new StandardEvaluationContext();
    HashMethodExecutor executor = new HashMethodExecutor();
    TypedValue value = executor.execute(context, new Object(), -3, -2);
    assertThat((String) value.getValue(), is("1_hash"));
  }
View Full Code Here

  @Test
  public void testEqualValues() throws Exception {
    StandardEvaluationContext context = new StandardEvaluationContext();
    HashMethodExecutor executor = new HashMethodExecutor();
    TypedValue value = executor.execute(context, new Object(), -3, -3);
    assertThat((String) value.getValue(), is("0_hash"));
    value = executor.execute(context, new Object(), -1, -1);
    assertThat((String) value.getValue(), is("0_hash"));
    value = executor.execute(context, new Object(), 1, 1);
    assertThat((String) value.getValue(), is("0_hash"));
  }
View Full Code Here

TOP

Related Classes of org.springframework.expression.TypedValue

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.