Package net.jcores.jre.utils.internal

Examples of net.jcores.jre.utils.internal.Options


            public void f() {
                try {
                    queue.add(Async.QEntry(f.f()));
                    queue.close();
                } catch (Exception e) {
                    final Options options$ = Options.$(CommonCore.this, options);
                    options$.failure(f, e, "async:exception", "General exception invoking async function.");
                    queue.close();
                }
            }
        }, 0, options);
View Full Code Here


     * ).
     * @since 1.0
     * @return This async object.
     */
    public Async<T> onNext(final F1<T, Void> f, Option... options) {
        final Options options$ = Options.$(this.commonCore, options);
        final KillSwitch killswitch = options$.killswitch();

        $.sys.oneTime(new F0() {
            @Override
            public void f() {
                while (true) {
View Full Code Here

     */
    @SupportsOption(options = { KillSwitch.class })
    public <R> Async<R> async(final F1<T, R> f, Option... options) {
        final Queue<R> queue = Async.Queue();
        final Async<R> async = new Async<R>(this.commonCore, queue);
        final Options options$ = Options.$(this.commonCore, options);
        final KillSwitch killswitch = options$.killswitch();

        this.commonCore.sys.oneTime(new F0() {
            @Override
            public void f() {
                try {
                    // Now process all elements
                    for (T t : CoreObject.this) {
                        // Check if we should terminate.
                        if(killswitch != null && killswitch.terminated())  return;
                       
                        // Otherwise process next element.
                        try {
                            queue.add(Async.QEntry(f.f(t)));
                        } catch (Exception e) {
                            CoreObject.this.commonCore.report(MessageType.EXCEPTION, "Exception when processing " + t + " ... " + e.getMessage());
                            options$.failure(t, e, "for/f", "Unknown exception when processing element");
                        }
                    }
                } finally {
                    // When we're done, close the queue.
                    queue.close();
View Full Code Here

     *
     * @since 1.0
     * @param manifest The manifest (can be null).
     */
    protected void pack(Manifest manifest, Option... options) {
        final Options options$ = Options.$($, options);
        final ClassLoader systemloader = ClassLoader.getSystemClassLoader().getParent();
        final boolean debug = options$.debug();
        URLClassLoader loader = $(getClass().getClassLoader()).get(URLClassLoader.class, null);

        // Check the current loader
        if (loader == null) {
            System.err.println("Unable to get the classpath for this script. Cannot pack. Sorry.");
View Full Code Here

                final String classname = x.getCanonicalName().replaceAll("\\.", "/") + ".class";
                final ClassLoader classloader = x.getClassLoader();

                // For internal object this usually does not work
                if (classloader == null) {
                    final Options options$ = Options.$(cc, options);
                    options$.failure(x, null, "bytecode:none", "Unable to find bytecode.");
                    return null;
                }

                return Streams.getByteData(classloader.getResourceAsStream(classname));
            }
View Full Code Here

     */
    @SuppressWarnings("unchecked")
    public CoreObject<T> spawn(final Option... options) {
        // Process each element we might have enclosed.
        final CommonCore cc = this.commonCore;
        final Options options$ = Options.$(this.commonCore, options);
        final Object[] args = options$.args();

        return map(new F1<Class<T>, T>() {
            @Override
            public T f(Class<T> x) {
                // Get the class we operate on
                if (x == null) return null;
                Class<T> toSpawn = x;

                // TODO: Selection of implementor could need some improvement
                if (x.isInterface()) {
                    toSpawn = (Class<T>) CoreClass.this.manager.getImplementors(x)[0];
                }

                // Quick pass for most common option
                if (args == null || args.length == 0) {
                    try {
                        toSpawn.newInstance();
                    } catch (InstantiationException e) {
                        options$.failure(x, e, "spawn:instanceexception", "Unable to create a new instance.");
                    } catch (IllegalAccessException e) {
                        options$.failure(x, e, "spawn:illegalaccess", "Unable to access type.");
                    }
                }

                // Get constructor types ...
                Class<?>[] types = new CoreObject<Object>(cc, args).map(new F1<Object, Class<?>>() {
                    public Class<?> f(Object xx) {
                        return xx.getClass();
                    }
                }).array(Class.class);

                try {
                    Constructor<T> constructor = null;

                    // Get constructor from cache ... (try to)
                    synchronized (CoreClass.this.constructors) {
                        constructor = CoreClass.this.constructors.get(types);

                        // Put a new constructor if it wasn't cached before
                        if (constructor == null) {
                            try {
                                constructor = toSpawn.getDeclaredConstructor(types);
                            } catch (NoSuchMethodException e) {
                                // We catch this exception in here, as sometimes we fail to obtain the
                                // proper constructor with the method above. In that case, we try to get
                                // the closest match
                                Constructor<?>[] declaredConstructors = toSpawn.getDeclaredConstructors();
                                for (Constructor<?> ccc : declaredConstructors) {
                                    // Check if the constructor matches
                                    Class<?>[] parameterTypes = ccc.getParameterTypes();
                                    if (parameterTypes.length != types.length) continue;

                                    boolean mismatch = false;

                                    // Check if each parameter is assignable
                                    for (int i = 0; i < types.length; i++) {
                                        if (!parameterTypes[i].isAssignableFrom(types[i]))
                                            mismatch = true;
                                    }

                                    // In case any parameter mismatched, we can't use this constructor
                                    if (mismatch) continue;

                                    constructor = (Constructor<T>) ccc;
                                }
                            }
                            // If we don't have any constructor at this point, we are in trouble
                            if (constructor == null)
                                throw new NoSuchMethodException("No constructor found.");

                            CoreClass.this.constructors.put(types, constructor);
                        }
                    }

                    return constructor.newInstance(args);

                    // NOTE: We do not swallow all execptions silently, becasue spawn() is a bit
                    // special and we cannot return anything that would still be usable.
                } catch (SecurityException e) {
                    options$.failure(x, e, "spawn:security", "Security exception when trying to spawn.");
                } catch (NoSuchMethodException e) {
                    options$.failure(x, e, "spawn:nomethod", "Method not found.");
                } catch (IllegalArgumentException e) {
                    options$.failure(x, e, "spawn:illegalargs", "Illegal passed arguments.");
                } catch (InstantiationException e) {
                    options$.failure(x, e, "spawn:instanceexception:2", "Cannot instantiate.");
                } catch (IllegalAccessException e) {
                    options$.failure(x, e, "spawn:illegalaccess:2", "Unable to access type (2).");
                } catch (InvocationTargetException e) {
                    options$.failure(x, e, "spawn:invocation", "Unable to invoke target.");
                }

                // TODO Make sure to only use weak references, so that we don't run out of memory
                // and prevent
                // garbage colleciton.
View Full Code Here

     * @param options Accepts a {@link Hash} options for the method to use.
     * @since 1.0
     * @return A CoreString containing the generated hashes.
     */
    public CoreString hash(Option... options) {
        final Options options$ = Options.$(this.commonCore, options);
        final String method = $(options).get(Hash.class, Hash.MD5).getMethod();

        return new CoreString(this.commonCore, map(new F1<ByteBuffer, String>() {
            public String f(final ByteBuffer x) {
                return Bytes.generateHash(x, method, options$);
View Full Code Here

     */
    public File tempfile(Option... options) {
        try {
            return File.createTempFile("jcores.", ".tmp");
        } catch (IOException e) {
            final Options options$ = Options.$(this.commonCore, options);
            options$.failure(null, e, "tempfile:create", "Unable to create temp file.");
        }

        return new File("/tmp/jcores.failedtmp." + System.nanoTime() + ".tmp");
    }
View Full Code Here

    public File tempdir(Option... options) {
        final File ffile = new File(tempfile(options).getAbsoluteFile() + ".dir/");
       
        // Report if we failed
        if (!ffile.mkdirs()) {
            final Options options$ = Options.$(this.commonCore, options);
            options$.failure(null, null, "tempdir:create", "Unable to create temp dir.");
        }
       
        return ffile;
    }
View Full Code Here

     * @param delay The delay at which the function will be executed.
     * @param options May accept a {@link KillSwitch}.
     */
    @SupportsOption(options = { KillSwitch.class })
    public void manyTimes(final F0 f0, final long delay, final Option... options) {
        final Options options$ = Options.$(this.commonCore, options);
        final KillSwitch killswitch = options$.killswitch();
        final Future<?> submit = this.commonCore.executor().getExecutor().submit(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        f0.f();
                        sleep(delay, options);
                    } catch (Exception e) {
                        options$.failure(f0, e, "manytimes:run", "Exception while executing f().");
                    }

                    // Check if we should terminate
                    if (killswitch != null && killswitch.terminated()) return;
                }
View Full Code Here

TOP

Related Classes of net.jcores.jre.utils.internal.Options

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.