List of usage examples for org.apache.commons.collections4 Transformer transform
O transform(I input);
From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformerTest.java
@Test public void invokeMethod() { String testString = "HelloWorld"; Transformer transformer = new MethodInvokingTransformer(charAtMethod, argumentResolver); Object transform = transformer.transform(testString); assertNotNull(transform);/*from w w w . j a v a 2 s. c om*/ assertTrue(transform instanceof Character); Character charAt1 = (Character) transform; assertEquals('e', charAt1.charValue()); }
From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformerTest.java
@Test public void invokeStaticMethodWithNullTarget() throws SecurityException, NoSuchMethodException { InvocationArgumentsResolver invocationArgumentsResolver = createNiceMock(InvocationArgumentsResolver.class); Method formatMethod = String.class.getDeclaredMethod("format", String.class, Object[].class); expect(invocationArgumentsResolver.getArguments(String.class, formatMethod)) .andReturn(new Object[] { "Hello %s", "World" }); replay(invocationArgumentsResolver); Transformer transformer = new MethodInvokingTransformer(formatMethod, invocationArgumentsResolver); Object transform = transformer.transform(null); assertNotNull(transform);//from w w w.j a v a 2 s. c o m assertEquals("Hello World", transform); }
From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformerTest.java
@Test public void invokeStaticMethodWithClassTarget() throws SecurityException, NoSuchMethodException { InvocationArgumentsResolver invocationArgumentsResolver = createNiceMock(InvocationArgumentsResolver.class); Method formatMethod = String.class.getDeclaredMethod("format", String.class, Object[].class); expect(invocationArgumentsResolver.getArguments(String.class, formatMethod)) .andReturn(new Object[] { "Hello %s", "World" }); replay(invocationArgumentsResolver); Transformer transformer = new MethodInvokingTransformer(formatMethod, invocationArgumentsResolver); Object transform = transformer.transform(String.class); assertNotNull(transform);//from w w w . ja v a 2s . c o m assertEquals("Hello World", transform); }
From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformerTest.java
@Test(expected = IllegalArgumentException.class) public void transformWrongType() { Transformer transformer = new MethodInvokingTransformer(charAtMethod, argumentResolver); Character valueOf = Character.valueOf('c'); transformer.transform(valueOf); }
From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformerTest.java
@Test(expected = IllegalStateException.class) public void invokeInstanceMethodThrowsUnknownException() { String testString = "HelloWorld"; Method toStringMethod = MethodUtils.getAccessibleMethod(String.class, "toString", new Class<?>[0]); Transformer transformer = new MethodInvokingTransformer(toStringMethod) { @Override/*from w ww . ja va 2s . c o m*/ protected Object invokeInstanceMethod(Object targetObject) throws Exception { throw new IOException(); } }; transformer.transform(testString); }
From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformerTest.java
@Test(expected = IllegalArgumentException.class) public void wrongArguments() { Transformer transformer = new MethodInvokingTransformer(charAtMethod, new InvocationArgumentsResolver() { public Object[] getArguments(Object invokedObject, Method invokedMethod) { return new Object[] { "A" }; }//from w ww .j a v a 2 s .c o m }); transformer.transform("Test"); }
From source file:com.link_intersystems.lang.reflect.Class2.java
private String[] transform(Object[] objects, Transformer objectTransformer) { String[] toStringList = new String[objects.length]; for (int i = 0; i < objects.length; i++) { Object object = objects[i]; Object transformedObject = objectTransformer.transform(object); toStringList[i] = transformedObject.toString(); }// w w w. ja va 2 s .co m return toStringList; }
From source file:org.icelib.beans.ObjectMapping.java
@SuppressWarnings({ "unchecked", "restriction", "rawtypes" }) public static <V> V value(Object value, Class<V> targetClass, Object... constructorArgs) { Transformer<Object, V> t = (Transformer<Object, V>) objectConverters .get(new ClassKey(value.getClass(), targetClass)); if (t != null) { return (V) (t.transform(value)); }//from w w w . ja v a 2s .c o m if (value instanceof Number) { if (targetClass.equals(Long.class) || targetClass.equals(long.class)) { return (V) ((Long) (((Number) value).longValue())); } else if (targetClass.equals(Double.class) || targetClass.equals(double.class)) { return (V) ((Double) (((Number) value).doubleValue())); } else if (targetClass.equals(Integer.class) || targetClass.equals(int.class)) { return (V) ((Integer) (((Number) value).intValue())); } else if (targetClass.equals(Short.class) || targetClass.equals(short.class)) { return (V) ((Short) (((Number) value).shortValue())); } else if (targetClass.equals(Float.class) || targetClass.equals(float.class)) { return (V) ((Float) (((Number) value).floatValue())); } else if (targetClass.equals(Byte.class) || targetClass.equals(byte.class)) { return (V) ((Byte) (((Number) value).byteValue())); } else if (targetClass.equals(Boolean.class) || targetClass.equals(boolean.class)) { return (V) ((Boolean) (((Number) value).intValue() != 0)); } else if (targetClass.equals(String.class)) { return (V) (value.toString()); } else { throw new IllegalArgumentException( String.format("Cannot convert number %s to %s", value, targetClass)); } } else if (value instanceof Boolean) { return (V) (((Boolean) value)); } else if (value instanceof String) { if (targetClass.equals(Long.class)) { return (V) ((Long) Long.parseLong((String) value)); } else if (targetClass.equals(Double.class)) { return (V) ((Double) Double.parseDouble((String) value)); } else if (targetClass.equals(Integer.class)) { return (V) ((Integer) Integer.parseInt((String) value)); } else if (targetClass.equals(Short.class)) { return (V) ((Short) Short.parseShort((String) value)); } else if (targetClass.equals(Float.class)) { return (V) ((Float) Float.parseFloat((String) value)); } else if (targetClass.equals(Byte.class)) { return (V) ((Byte) Byte.parseByte((String) value)); } else if (targetClass.equals(Boolean.class)) { return (V) ((Boolean) Boolean.parseBoolean((String) value)); } else if (targetClass.equals(String.class)) { return (V) (value.toString()); } else if (targetClass.isEnum()) { for (V ev : targetClass.getEnumConstants()) { if (ev.toString().toLowerCase().equalsIgnoreCase(value.toString())) { return ev; } } throw new IllegalArgumentException(String.format("Unknown enum %s in %s", value, targetClass)); } else { // Maybe the target has a constructor that takes a string try { Constructor<V> con = targetClass.getConstructor(String.class); return con.newInstance((String) value); } catch (NoSuchMethodException nsme) { // That's it, give up throw new IllegalArgumentException( String.format("Cannot convert string %s to %s", value, targetClass)); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } } } else if (value instanceof Map) { /* * This covers ScriptObjectMirror from Nashorn which is a list AND a * map describing an object or an array. It also covers ordinary map * and lists */ if (isScriptNativeArray(value)) { Collection<?> c = ((jdk.nashorn.api.scripting.ScriptObjectMirror) value).values(); if (c instanceof List && MappedList.class.isAssignableFrom(targetClass)) { try { V v = getTargetInstance(value, targetClass, constructorArgs); for (Object o : ((List) c)) { ((List) v).add(o); } return v; } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format( "No zero-length constructor for class %s and no valid constructor args provided.", targetClass)); } catch (InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(String.format("Could not construct %s", targetClass), e); } } else if ((List.class.isAssignableFrom(targetClass) && c instanceof List) || (Collection.class.isAssignableFrom(targetClass) && c instanceof Collection)) { return (V) c; } else if (Set.class.isAssignableFrom(targetClass)) { return (V) new LinkedHashSet<Object>(c); } else if (Collection.class.isAssignableFrom(targetClass)) { return (V) new ArrayList<Object>(c); } else if (targetClass.isArray()) { return (V) c.toArray((Object[]) Array.newInstance(targetClass.getComponentType(), c.size())); } } else if (value instanceof List && List.class.isAssignableFrom(targetClass)) { return (V) ((List<?>) value); } else if (!isScriptNativeObject(value) && Map.class.isAssignableFrom(targetClass)) { return (V) ((Map<?, ?>) value); } else { try { V v = getTargetInstance(value, targetClass, constructorArgs); ObjectMapper<?> m = new ObjectMapper<>(v); try { m.map((Map<String, Object>) value); } catch (Exception e) { throw new RuntimeException(String.format("Failed to map %s.", targetClass), e); } return v; } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format( "No zero-length constructor for class %s and no valid constructor args provided.", targetClass)); } catch (InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(String.format("Could not construct %s", targetClass), e); } } } else if (targetClass.isAssignableFrom(value.getClass())) { return (V) value; } else if (value instanceof Collection && List.class.isAssignableFrom(targetClass)) { try { V v = getTargetInstance(value, targetClass, constructorArgs); ((List) v).addAll((Collection) value); return v; } catch (InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(String.format("Could not construct %s", targetClass), e); } catch (NoSuchMethodException nse) { } } throw new IllegalArgumentException( String.format("Cannot convert %s (%s) to %s", value, value.getClass(), targetClass)); }
From source file:org.molasdin.wbase.collections.list.ExtendedListDecorator.java
@SuppressWarnings("unchecked") public void sort(final String property, Order order) { if (decorated().isEmpty()) { return;/*from www . j a va 2s. c o m*/ } T item = decorated().get(0); Comparator<T> tmp = null; if (ReflectionHelper.hasFunction(property, item)) { final Transformer<T, Object> transformer = new Transformer<T, Object>() { @Override public Object transform(T o) { return ReflectionHelper.functionValue(property, o); } }; Object val = transformer.transform(item); final boolean isNatural = ReflectionHelper.supportsNaturalOrdering(val); tmp = new Comparator<T>() { @Override public int compare(T o1, T o2) { Object firstResult = transformer.transform(o1); Object secondResult = transformer.transform(o2); if (isNatural) { Comparable f = (Comparable) firstResult; Comparable s = (Comparable) secondResult; return f.compareTo(s); } String f = ConvertUtils.convert(firstResult); String s = ConvertUtils.convert(secondResult); return f.compareTo(s); } }; } else if (PropertyUtils.isReadable(item, property)) { tmp = new BeanComparator(property); } else { throw new RuntimeException("No property"); } Collections.sort(decorated(), tmp); }
From source file:org.rippleosi.common.service.AbstractSCCISService.java
protected <T> T findData(String patientId, Transformer transformer) { ResponseEntity<String> response; response = queryByHttpGet(patientId); Document xml = null;//from w ww .j av a 2 s .c o m if (response.getStatusCode() == HttpStatus.OK) { try { xml = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new StringReader(response.getBody()))); xml.normalize(); } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } transformer.transform(xml); } return (T) transformer.transform(xml); }