001package org.intellimate.izou.sdk.output;
002
003import com.google.common.reflect.TypeToken;
004import org.intellimate.izou.events.EventModel;
005import org.intellimate.izou.output.OutputExtensionModel;
006import org.intellimate.izou.sdk.Context;
007import org.intellimate.izou.sdk.util.AddOnModule;
008
009import java.util.ArrayList;
010import java.util.List;
011
012/**
013 * OutputExtension's purpose is to take resourceData and convert it into another data format so that it can be rendered correctly
014 * by the output-plugin. These objects are represented in the form of future objects that are stored in tDoneList
015 * @param <T> the return type
016 * @param <X> the type of the Argument
017 */
018public abstract class OutputExtensionArgument<T, X> extends AddOnModule implements OutputExtensionModel<T, X> {
019
020    /**
021     * the id of the outputPlugin the outputExtension belongs to
022     */
023    private String pluginId;
024    /**
025     * the type argument for the Data you want to return
026     */
027    private final TypeToken<T> returnTypeToken;
028    /**
029     * the type argument for the Data you want to give the OutputExtensions as an argument
030     */
031    private final TypeToken<X> argumentTypeToken;
032
033    /**
034     * a list of all the resource's which the outputExtension would like to receive theoretically to work with
035     */
036    private final List<String> resourceIdWishList;
037
038    /**
039     * creates a new outputExtension with a new id
040     * @param context the context of the addon
041     * @param id the id to be set to the id of outputExtension
042     * @param pluginId the id of the plugin to associate to
043     */
044    public OutputExtensionArgument(Context context, String id, String pluginId) {
045        this(id, context);
046        this.pluginId = pluginId;
047    }
048
049    /**
050     * creates a new outputExtension with a new id
051     * @param id the id to be set to the id of outputExtension
052     * @param context the context of the addon
053     */
054    public OutputExtensionArgument(String id, Context context) {
055        super(context, id);
056        resourceIdWishList = new ArrayList<>();
057        this.returnTypeToken = new TypeToken<T>(getClass()) {};
058        this.argumentTypeToken = new TypeToken<X>(getClass()) {};
059    }
060
061    /**
062     * returns its resourceIdWishList
063     *
064     * @return a List containing the resourceIDs
065     */
066    public List<String> getResourceIdWishList() {
067        return resourceIdWishList;
068    }
069
070    /**
071     * adds id of resource to the wish list
072     *
073     * @param id the id of the resource to be added to the wish list
074     */
075    public void addResourceIdToWishList(String id) {
076        resourceIdWishList.add(id);
077    }
078
079    /**
080     * removes resourceId from the resourceIdWishList
081     *
082     * @param id the id of the resource to be removed
083     */
084    public void removeResourceIdFromWishList(String id) {
085        resourceIdWishList.remove(id);
086    }
087
088    /**
089     * gets the id of the output-plugin the outputExtension belongs to
090     *
091     * @return id of the output-plugin the outputExtension belongs to
092     */
093    @Override
094    public String getPluginId() {
095        return pluginId;
096    }
097
098    /**
099     * sets the ID of the OutputPlugin the OutputExtension belongs to
100     * @param pluginId the ID of the Plugin
101     */
102    public void setPluginId(String pluginId) {
103        this.pluginId = pluginId;
104    }
105
106    /**
107     * returns the ReturnType of the generic
108     *
109     * @return the type of the generic
110     */
111    @Override
112    public TypeToken<T> getReturnType() {
113        return returnTypeToken;
114    }
115
116    /**
117     * returns the Type of the Argument or null if none
118     *
119     * @return the type of the argument
120     */
121    @Override
122    public TypeToken<X> getArgumentType() {
123        return argumentTypeToken;
124    }
125
126    /**
127     * Checks if the outputExtension can execute with the current event
128     *
129     * @param event the event to check against
130     * @return the state of whether the outputExtension can execute with the current event
131     */
132    @Override
133    public boolean canRun(EventModel event) {
134        //noinspection SimplifiableIfStatement
135        if (event != null) {
136            return event.getListResourceContainer().providesResource(getResourceIdWishList());
137        }
138        return false;
139    }
140
141    /**
142     * generates the data for the given Event
143     *
144     * @param event the event to generate for
145     * @param x     the optional argument
146     * @return the result
147     */
148    @Override
149    //more clarity
150    public abstract T generate(EventModel event, X x);
151}
152