/** * checks if an action is usable * * @param p_action action object * @return boolean usable flag */ private static boolean actionusable(final IAction p_action) { if ((p_action.name() == null) || (p_action.name().isEmpty()) || (p_action.name().get(0).trim().isEmpty())) { LOGGER.warning(CCommon.languagestring(CCommon.class, "actionnameempty")); return false; } if (!Character.isLetter(p_action.name().get(0).charAt(0))) { LOGGER.warning(CCommon.languagestring(CCommon.class, "actionletter", p_action)); return false; } if (!Character.isLowerCase(p_action.name().get(0).charAt(0))) { LOGGER.warning(CCommon.languagestring(CCommon.class, "actionlowercase", p_action)); return false; } if (p_action.minimalArgumentNumber() < 0) { LOGGER.warning(CCommon.languagestring(CCommon.class, "actionargumentsnumber", p_action)); return false; } return true; }
/** * returns actions by a class * * @note class must be an inheritance of the IAgent interface * @param p_class class list * @return action stream */ @SuppressWarnings("unchecked") public static Stream<IAction> actionsFromAgentClass(final Class<?>... p_class) { return p_class == null || p_class.length == 0 ? Stream.of() : Arrays.stream(p_class) .parallel() .filter(IAgent.class::isAssignableFrom) .flatMap(i -> CCommon.methods(i, i)) .map( i -> { try { return (IAction) new CMethodAction(i); } catch (final IllegalAccessException l_exception) { LOGGER.warning( CCommon.languagestring(CCommon.class, "actioninstantiate", i, l_exception)); return null; } }) // action can be instantiate .filter(Objects::nonNull) // check usable action name .filter(CCommon::actionusable); }
/** * creates a compression stream * * @param p_datastream data-counting stream * @return compression output stream * @throws IOException throws on any io error */ public final OutputStream get(final DataOutputStream p_datastream) throws IOException { switch (this) { case BZIP: return new BZip2CompressorOutputStream(p_datastream); case GZIP: return new GzipCompressorOutputStream(p_datastream); case DEFLATE: return new DeflateCompressorOutputStream(p_datastream); case PACK200: return new Pack200CompressorOutputStream(p_datastream); case XZ: return new XZCompressorOutputStream(p_datastream); default: throw new CIllegalStateException(CCommon.languagestring(this, "unknown", this)); } }
/** * get all classes within an Java package as action * * @param p_package full-qualified package name or empty for default package * @return action stream */ @SuppressWarnings("unchecked") public static Stream<IAction> actionsFromPackage(final String... p_package) { return ((p_package == null) || (p_package.length == 0) ? Stream.of(MessageFormat.format("{0}.{1}", PACKAGEROOT, "action.buildin")) : Arrays.stream(p_package)) .flatMap( j -> { try { return ClassPath.from(Thread.currentThread().getContextClassLoader()) .getTopLevelClassesRecursive(j) .parallelStream() .map(ClassPath.ClassInfo::load) .filter(i -> !Modifier.isAbstract(i.getModifiers())) .filter(i -> !Modifier.isInterface(i.getModifiers())) .filter(i -> Modifier.isPublic(i.getModifiers())) .filter(IAction.class::isAssignableFrom) .map( i -> { try { return (IAction) i.newInstance(); } catch (final IllegalAccessException | InstantiationException l_exception) { LOGGER.warning( CCommon.languagestring( CCommon.class, "actioninstantiate", i, l_exception)); return null; } }) // action can be instantiate .filter(Objects::nonNull) // check usable action name .filter(CCommon::actionusable); } catch (final IOException l_exception) { throw new UncheckedIOException(l_exception); } }); }