001package org.intellimate.izou.events;
002
003import org.intellimate.izou.identification.Identification;
004import org.intellimate.izou.resource.ListResourceMinimalImpl;
005import org.intellimate.izou.resource.ListResourceProvider;
006import org.intellimate.izou.resource.ResourceModel;
007
008import java.util.ArrayList;
009import java.util.HashMap;
010import java.util.List;
011import java.util.function.Consumer;
012
013/**
014 * this is a minimal implementation of an Event. Do not use this outside Izou! It will change between Versions!
015 * @author LeanderK
016 * @version 1.0
017 */
018public class EventMinimalImpl implements EventModel<EventMinimalImpl> {
019    private final String type;
020    private final Identification source;
021    private final List<String> descriptors;
022    private final ListResourceProvider listResourceContainer;
023    private final Consumer<EventLifeCycle> callback;
024    private final EventBehaviourControllerImpl eventBehaviourController;
025
026    public EventMinimalImpl(String type, Identification source, List<String> descriptors) {
027        this.type = type;
028        this.source = source;
029        this.descriptors = descriptors;
030        this.listResourceContainer = new ListResourceMinimalImpl();
031        callback = eventLifeCycle ->{};
032        eventBehaviourController = new EventBehaviourControllerImpl();
033    }
034
035    public EventMinimalImpl(String type, Identification source, List<String> descriptors, Consumer<EventLifeCycle> callback) {
036        this.type = type;
037        this.source = source;
038        this.descriptors = descriptors;
039        this.listResourceContainer = new ListResourceMinimalImpl();
040        this.callback = callback;
041        eventBehaviourController = new EventBehaviourControllerImpl();
042    }
043
044    /**
045     * The type of the Event.
046     * It describes the Type of the Event.
047     *
048     * @return A String containing an ID
049     */
050    @Override
051    public String getType() {
052        return type;
053    }
054
055    /**
056     * returns the Source of the Event, e.g. the object who fired it.
057     * @return an identifiable
058     */
059    @Override
060    public Identification getSource() {
061        return source;
062    }
063
064    /**
065     * returns all the Resources the Event currently has
066     * @return an instance of ListResourceContainer
067     */
068    @Override
069    public ListResourceProvider getListResourceContainer() {
070        return listResourceContainer;
071    }
072
073    /**
074     * adds a Resource to the Container
075     * @param resource an instance of the resource to add
076     * @return the resulting Event (which is the same instance)
077     */
078    @Override
079    public EventMinimalImpl addResource(ResourceModel resource) {
080        listResourceContainer.addResource(resource);
081        return this;
082    }
083
084    /**
085     * adds a List of Resources to the Container
086     * @param resources a list containing all the resources
087     */
088    @Override
089    public EventMinimalImpl addResources(List<ResourceModel> resources) {
090        listResourceContainer.addResource(resources);
091        return this;
092    }
093
094    /**
095     * returns a List containing all the Descriptors.
096     * @return a List containing the Descriptors
097     */
098    @Override
099    public List<String> getDescriptors() {
100        return descriptors;
101    }
102
103    /**
104     * returns a List containing all the Descriptors and the type.
105     * @return a List containing the Descriptors
106     */
107    @Override
108    public List<String> getAllInformations() {
109        ArrayList<String> strings = new ArrayList<>(descriptors);
110        strings.add(type);
111        return strings;
112    }
113
114    /**
115     * returns whether the event contains the specific descriptor.
116     * this method also checks whether it matches the type.
117     *
118     * @param descriptor a String with the ID of the Descriptor
119     * @return boolean when the Event contains the descriptor, false when not.
120     */
121    @Override
122    public boolean containsDescriptor(String descriptor) {
123        return descriptors.contains(descriptor) || type.equals(descriptor);
124    }
125
126    @Override
127    public boolean equals(Object o) {
128        if (this == o) return true;
129        if (!(o instanceof EventMinimalImpl)) return false;
130
131        EventMinimalImpl that = (EventMinimalImpl) o;
132
133        if (type != null ? !type.equals(that.type) : that.type != null) return false;
134        if (source != null ? !source.equals(that.source) : that.source != null) return false;
135        return !(descriptors != null ? !descriptors.equals(that.descriptors) : that.descriptors != null);
136
137    }
138
139    @Override
140    public int hashCode() {
141        int result = type != null ? type.hashCode() : 0;
142        result = 31 * result + (source != null ? source.hashCode() : 0);
143        result = 31 * result + (descriptors != null ? descriptors.hashCode() : 0);
144        return result;
145    }
146
147    /**
148     * returns the associated EventBehaviourController
149     *
150     * @return an instance of EventBehaviourController
151     */
152    @Override
153    public EventBehaviourControllerModel getEventBehaviourController() {
154        return eventBehaviourController;
155
156    }
157
158    /**
159     * this method gets called when the different lifecycle-stages got reached.
160     * It is not blocking!
161     *
162     * @param eventLifeCycle the lifecycle reached.
163     */
164    @Override
165    public void lifecycleCallback(EventLifeCycle eventLifeCycle) {
166        callback.accept(eventLifeCycle);
167    }
168
169    /**
170     * An ID must always be unique.
171     * A Class like Activator or OutputPlugin can just provide their .class.getCanonicalName()
172     * If you have to implement this interface multiple times, just concatenate unique Strings to
173     * .class.getCanonicalName()
174     *
175     * @return A String containing an ID
176     */
177    @Override
178    public String getID() {
179        return type;
180    }
181
182    private class EventBehaviourControllerImpl implements EventBehaviourControllerModel {
183
184        /**
185         * generates the data to control the Event
186         * <p>
187         * The Identifications with the highest Integer get the priority.
188         * </p>
189         *
190         * @param identifications the Identifications of the OutputPlugins
191         * @return a HashMap, where the keys represent the associated Behaviour and the values the Identification;
192         */
193        @Override
194        public HashMap<Integer, List<Identification>> getOutputPluginBehaviour(List<Identification> identifications) {
195            HashMap<Integer, List<Identification>> hashMap = new HashMap<>();
196            hashMap.put(0, identifications);
197            return hashMap;
198        }
199    }
200}