// TODO: refactor public static List createRange(Object from, Object to, boolean inclusive) throws Throwable { if (from instanceof Integer && to instanceof Integer) { int ito = (Integer) to; int ifrom = (Integer) from; if (!inclusive) { if (ifrom == ito) { return new EmptyRange((Comparable) from); } if (ifrom > ito) { ito++; } else { ito--; } } return new IntRange(ifrom, ito); } if (!inclusive) { if (compareEqual(from, to)) { return new EmptyRange((Comparable) from); } if (compareGreaterThan(from, to)) { to = invokeMethod0(ScriptBytecodeAdapter.class, to, "next"); } else { to = invokeMethod0(ScriptBytecodeAdapter.class, to, "previous"); } } if (from instanceof Integer && to instanceof Integer) return new IntRange( DefaultTypeTransformation.intUnbox(from), DefaultTypeTransformation.intUnbox(to)); else return new ObjectRange((Comparable) from, (Comparable) to); }
// spread expressions public static Object[] despreadList(Object[] args, Object[] spreads, int[] positions) { List ret = new ArrayList(); int argsPos = 0; int spreadPos = 0; for (int pos = 0; pos < positions.length; pos++) { for (; argsPos < positions[pos]; argsPos++) { ret.add(args[argsPos]); } Object value = spreads[spreadPos]; if (value == null) { ret.add(null); } else if (value instanceof List) { ret.addAll((List) value); } else if (value.getClass().isArray()) { ret.addAll(DefaultTypeTransformation.primitiveArrayToList(value)); } else { throw new IllegalArgumentException( "cannot spread the type " + value.getClass().getName() + " with value " + value); } spreadPos++; } for (; argsPos < args.length; argsPos++) { ret.add(args[argsPos]); } return ret.toArray(); }
public GPathResult find(final Closure closure) { if (DefaultTypeTransformation.castToBoolean(closure.call(new Object[] {this}))) { return this; } else { return new NoChildren(this, "", this.namespaceTagHints); } }
@SuppressWarnings("unchecked") public boolean onPreUpdate(PreUpdateEvent event) { Object entity = event.getEntity(); boolean evict = false; if (preUpdateEventListener != null) { evict = preUpdateEventListener.call(entity); synchronizePersisterState(entity, event.getPersister(), event.getState()); } if (lastUpdatedProperty != null && shouldTimestamp) { Object now = DefaultGroovyMethods.newInstance( lastUpdatedProperty.getType(), new Object[] {System.currentTimeMillis()}); event .getState()[ ArrayUtils.indexOf( event.getPersister().getPropertyNames(), GrailsDomainClassProperty.LAST_UPDATED)] = now; lastUpdatedProperty.setProperty(entity, now); } if (!AbstractSavePersistentMethod.isAutoValidationDisabled(entity) && !DefaultTypeTransformation.castToBoolean( validateMethod.invoke(entity, new Object[] {validateParams}))) { evict = true; if (failOnErrorEnabled) { Errors errors = (Errors) errorsProperty.getProperty(entity); throw new ValidationException( "Validation error whilst flushing entity [" + entity.getClass().getName() + "]", errors); } } return evict; }
/** * Checks if a Config parameter is true or a System property with the same name is true * * @param application * @param propertyName * @return true if the Config parameter is true or the System property with the same name is true */ public static boolean isConfigTrue(GrailsApplication application, String propertyName) { return ((application != null && application.getFlatConfig() != null && DefaultTypeTransformation.castToBoolean( application.getFlatConfig().get(propertyName))) || Boolean.getBoolean(propertyName)); }
public static Integer compareTo(Object left, Object right) { int answer = DefaultTypeTransformation.compareTo(left, right); if (answer == 0) { return ZERO; } else { return answer > 0 ? ONE : MINUS_ONE; } }
// isCase // TODO: set sender class public static boolean isCase(Object switchValue, Object caseExpression) throws Throwable { if (caseExpression == null) { return switchValue == null; } return DefaultTypeTransformation.castToBoolean( invokeMethodN( caseExpression.getClass(), caseExpression, "isCase", new Object[] {switchValue})); }
public void testInject() { Collection<Integer> c = Arrays.asList(2, 4, 5, 20); Number initial = BigDecimal.ZERO; Closure<? extends Number> closure = new Closure<BigDecimal>(c) { BigDecimal doCall(BigDecimal total, Integer next) { return total.add(BigDecimal.ONE.divide(new BigDecimal(next))); } }; assertTrue(DefaultTypeTransformation.compareEqual(BigDecimal.ONE, inject(c, initial, closure))); }
/** * Set the property on the given object to the new value. * * @param object on which to set the property * @param newValue the new value of the property * @throws RuntimeException if the property could not be set */ public void setProperty(Object object, Object newValue) { MetaMethod setter = getSetter(); if (setter == null) { if (field != null && !Modifier.isFinal(field.getModifiers())) { field.setProperty(object, newValue); return; } throw new GroovyRuntimeException("Cannot set read-only property: " + name); } newValue = DefaultTypeTransformation.castToType(newValue, getType()); setter.invoke(object, new Object[] {newValue}); }
/** * Asserts that the given object can be converted into a collection and iterator of the given size */ protected void assertAsCollection(Object collectionObject, int count) { Collection collection = DefaultTypeTransformation.asCollection(collectionObject); assertTrue("Collection is not null", collection != null); assertEquals("Collection size", count, collection.size()); assertIterator("collections iterator", collection.iterator(), count); assertIterator("InvokerHelper.asIterator", InvokerHelper.asIterator(collectionObject), count); assertIterator( "InvokerHelper.asIterator(InvokerHelper.asCollection)", InvokerHelper.asIterator(collection), count); assertIterator( "InvokerHelper.asIterator(InvokerHelper.asIterator)", InvokerHelper.asIterator(InvokerHelper.asIterator(collectionObject)), count); }
/** Asserts the asBoolean method returns the given flag */ protected void assertAsBoolean(boolean expected, Object value) { boolean answer = DefaultTypeTransformation.castToBoolean(value); assertEquals("value: " + value + " asBoolean()", expected, answer); }
public void testCompareTo() { assertTrue(DefaultTypeTransformation.compareEqual("x", new Integer('x'))); }
public static boolean compareEqual(Object left, Object right) { return DefaultTypeTransformation.compareEqual(left, right); }
/** * Provides a hook for type casting of the given object to the required type * * @param type of object to convert the given object to * @param object the object to be converted * @return the original object or a new converted value * @throws Throwable if the type casting fails */ public static Object castToType(Object object, Class type) throws Throwable { return DefaultTypeTransformation.castToType(object, type); }