All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.appium.java_client.events.EventFiringObjectFactory Maven / Gradle / Ivy

There is a newer version: 9.3.0
Show newest version
package io.appium.java_client.events;

import io.appium.java_client.events.api.Listener;
import org.openqa.selenium.WebDriver;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;

public class EventFiringObjectFactory {

    /**
     * This method makes an event firing object.
     *
     * @param t an original {@link Object} that is
     *               supposed to be listenable
     * @param driver an instance of {@link org.openqa.selenium.WebDriver}
     * @param listeners is a collection of {@link io.appium.java_client.events.api.Listener} that
     *                  is supposed to be used for the event firing
     * @param  T
     * @return an {@link Object} that fires events
     */
    @SuppressWarnings("unchecked")
    public static  T getEventFiringObject(T t, WebDriver driver, Collection listeners) {
        final List listenerList = new ArrayList<>();

        for (Listener listener : ServiceLoader.load(
                Listener.class)) {
            listenerList.add(listener);
        }

        listeners.stream().filter(listener -> !listenerList.contains(listener)).forEach(listenerList::add);

        AbstractApplicationContext context = new AnnotationConfigApplicationContext(
                DefaultBeanConfiguration.class);
        return (T) context.getBean(
                DefaultBeanConfiguration.LISTENABLE_OBJECT, t, driver, listenerList, context);
    }

    /**
     * This method makes an event firing object.
     *
     * @param t an original {@link Object} that is
     *               supposed to be listenable
     * @param driver an instance of {@link org.openqa.selenium.WebDriver}
     * @param  T
     * @return an {@link Object} that fires events
     */
    public static  T getEventFiringObject(T t, WebDriver driver) {
        return getEventFiringObject(t, driver, Collections.emptyList());
    }

    /**
     * This method makes an event firing object.
     *
     * @param t an original {@link Object} that is
     *               supposed to be listenable
     * @param driver an instance of {@link org.openqa.selenium.WebDriver}
     * @param listeners is an array of {@link io.appium.java_client.events.api.Listener} that
     *                  is supposed to be used for the event firing
     *
     * @param  T
     * @return an instance of {@link org.openqa.selenium.WebDriver} that fires events
     */
    public static  T getEventFiringObject(T t, WebDriver driver, Listener ... listeners) {
        return getEventFiringObject(t, driver, Arrays.asList(listeners));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy