/** * Creates the patient selection component. */ @Override public IPatientSelector create() { try { return patientSelectorClass.newInstance(); } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }
/** * Returns true if the two arrays intersect (i.e., have at least one element in common). * * @param ary1 The first array. * @param ary2 The second array. * @return True if the two arrays intersect. */ public static boolean intersects(Object[] ary1, Object[] ary2) { return intersects(Arrays.asList(ary1), Arrays.asList(ary2)); }
/** * Returns an iterable of this component's children restricted to the specified type. * * @param <T> Type of children returned by iterable. * @param clazz Restrict to children of this type. * @return Iterable of this component's children. */ public <T extends ElementBase> Iterable<T> getChildren(Class<T> clazz) { return MiscUtil.iterableForType(children, clazz); }
/** * Returns a lambda function (for use in spEL). * * @param instance Object instance that is target of method invocation. * @param methodName Name of the method to invoke. * @param args Optional additional arguments. * @return The lambda function. */ public Function<?, ?> lambda(Object instance, String methodName, Object... args) { int last = args == null ? 0 : args.length; Object[] realArgs = args == null ? new Object[] { null } : Arrays.copyOf(args, last + 1); return arg -> { try { realArgs[last] = arg; return MethodUtils.invokeMethod(instance, methodName, realArgs, MiscUtil.getParameterTypes(realArgs)); } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }; }
iterList.add(new Long(1)); iterList.add("string #2"); Iterator<Integer> iter = MiscUtil.iteratorForType(iterList, Integer.class); assertTrue(iter.hasNext()); assertEquals((Integer) 1, iter.next()); int strCount = 0; for (String ele : MiscUtil.iterableForType(iterList, String.class)) { strCount++; assertTrue(ele.equals("string #" + strCount));
/** * Returns an iterable that produces only collection members of the specified type. * * @param <T> Class of collection elements. * @param <S> Subclass of T to be used by iterable. * @param collection Collection to iterate. * @param type Type of element to return. * @return An iterable. */ public static <T, S extends T> Iterable<S> iterableForType(Collection<T> collection, Class<S> type) { return () -> { return iteratorForType(collection, type); }; }
@Test public void testCast() { List<Object> list = new ArrayList<>(); list.add("item1"); list.add("item2"); list.add("item3"); int i = 0; for (String item : MiscUtil.castList(list, String.class)) { assertEquals("item" + ++i, item); } Set<Object> set = new HashSet<>(); Set<String> set2 = MiscUtil.castCollection(set, String.class); set.add("item1"); set2.add("item2"); set2.add("item3"); assertEquals(3, set.size()); assertEquals(3, set2.size()); }
/** * Casts a list containing elements of class T to a list containing elements of a class S where * S must be assignment-compatible with T. * * @param <T> Class of list elements. * @param <S> Target class for cast. * @param list List to be recast. * @param clazz Class to which to cast list elements. * @return The recast list. */ public static <T, S> List<S> castList(List<T> list, Class<S> clazz) { return castCollection(list, clazz); }
/** * Returns a list iterator that produces only collection elements of the specified type. * * @param <T> Class of collection elements. * @param <S> Subclass of T to be used by iterator. * @param collection Collection to iterate. * @param type Type of element to return. * @return An iterator. */ public static <T, S extends T> Iterator<S> iteratorForType(Collection<T> collection, Class<S> type) { return iteratorForType(collection.iterator(), type); }
private static BaseMenuComponent createMenuOrMenuitem(BaseComponent parent) { try { Class<?> clazz = parent instanceof BaseMenuComponent ? Menuitem.class : Menu.class; return (BaseMenuComponent) clazz.newInstance(); } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }
@Test public void testOverlap() { assertFalse(MiscUtil.intersects(ary1, ary2)); assertTrue(MiscUtil.intersects(ary1, ary3)); assertTrue(MiscUtil.intersects(ary2, ary3)); }
protected <T extends LayoutNode> T getChild(Class<T> clazz) { Iterator<T> iter = MiscUtil.iteratorForType(children, clazz); return iter.hasNext() ? iter.next() : null; }
/** * Returns an iterable of children of the specified type. * * @param <T> The desired type. * @param type The desired type. * @return An iterable of children of the specified type. Never null. */ public final <T extends BaseComponent> Iterable<T> getChildren(Class<T> type) { return MiscUtil.iterableForType(getChildren(), type); }
/** * Create a new instance of the domain class. */ @Override public <T extends BaseResource> T newObject(Class<T> clazz) { try { return clazz.newInstance(); } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }
if (!MiscUtil.intersects(annotation.mode(), activeModes)) { return true;
protected BasePickerComponent(Class<P> itemClass) { try { converter = itemClass.newInstance(); } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }
if (!MiscUtil.intersects(annotation.mode(), activeModes)) { continue;
/** * Returns an array of resources matching the location pattern. * * @param locationPattern The location pattern. Supports classpath references. * @return Array of matching resources. */ public static Resource[] getResources(String locationPattern) { try { return resolver.getResources(locationPattern); } catch (IOException e) { throw MiscUtil.toUnchecked(e); } }
/** * Creates a new instance of a plot of this type. * * @return New plot instance. */ public PlotOptions newInstance() { try { PlotOptions plotOptions = plotClass.newInstance(); plotOptions.type = this; return plotOptions; } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }
/** * Creates a new instance of a plot of this type. * * @return New plot instance. */ public PlotOptions newInstance() { try { PlotOptions plotOptions = plotClass.newInstance(); plotOptions.type = this; return plotOptions; } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }