001package org.intellimate.izou.sdk.resource;
002
003import org.intellimate.izou.identification.Identification;
004import org.intellimate.izou.resource.ResourceModel;
005
006import java.util.ArrayList;
007import java.util.List;
008
009/**
010 * An Resource is an object which is used to pass data from one part of the application to another.
011 * Note! This Object is immutable!
012 */
013public class Resource<T> implements ResourceModel<T> {
014    private final String resourceID;
015    private final Identification provider;
016    private final Identification consumer;
017    private final T resource;
018
019    /**
020     * creates a new Resource.
021     * @param resourceID the ID of the Resource
022     */
023    public Resource(String resourceID) {
024        this(resourceID, null, null, null);
025    }
026
027    /**
028     * creates a new Resource.
029     * @param resourceID the ID of the Resource
030     * @param provider the Provider of the Resource
031     */
032    public Resource(String resourceID, Identification provider) {
033        this(resourceID, provider, null, null);
034    }
035
036    /**
037     * creates a new Resource.
038     * @param resourceID the ID of the Resource
039     * @param provider the Provider of the Resource
040     * @param t the resource
041     */
042    public Resource(String resourceID, Identification provider, T t) {
043        this(resourceID, provider, t, null);
044    }
045
046    /**
047     * creates a new Resource.
048     * @param resourceID the ID of the Resource
049     * @param consumer the ID of the Consumer
050     * @param t the resource
051     */
052    public Resource(String resourceID, T t, Identification consumer) {
053        this(resourceID, null, t, consumer);
054    }
055
056
057    /**
058     * creates a new Resource.
059     * @param resourceID the ID of the Resource
060     * @param provider the Provider of the Resource
061     * @param consumer the ID of the Consumer
062     */
063    public Resource(String resourceID, Identification provider, Identification consumer) {
064        this(resourceID, provider, null, consumer);
065    }
066
067    /**
068     * creates a new Resource.
069     * @param resourceID the ID of the Resource
070     * @param provider the Provider of the Resource
071     * @param t the resource
072     * @param consumer the ID of the Consumer
073     */
074    public Resource(String resourceID, Identification provider, T t, Identification consumer) {
075        this.resourceID = resourceID;
076        this.provider = provider;
077        this.resource = t;
078        this.consumer = consumer;
079    }
080
081    /**
082     * returns the associated Resource data if set.
083     * This method is thread-safe.
084     * @return null or resource data
085     */
086    @Override
087    public T getResource() {
088        return resource;
089    }
090
091    /**
092     * sets the Resource data.
093     * <p>
094     * Note! this Object is immutable!
095     * </p>
096     * @param resource the data to set
097     * @return the Resource
098     */
099    public Resource<T> setResource(T resource) {
100        return new Resource<>(resourceID, provider, resource, consumer);
101    }
102
103    /**
104     * returns the ID of the Resource.
105     * This method is thread-safe.
106     * @return a String containing the ID of the resource
107     */
108    @Override
109    public String getResourceID() {
110        return resourceID;
111    }
112
113    /**
114     * returns the provider of the Resource.
115     * This method is thread-safe.
116     * @return an Identification describing the provider of the Resource or null if not set
117     */
118    @Override
119    public Identification getProvider() {
120        return provider;
121    }
122
123    /**
124     * returns whether a provider is set
125     * @return true if this resource has an provider, false if not
126     */
127    @Override
128    public boolean hasProvider() {
129        return provider != null;
130    }
131
132    /**
133     * sets who should or has provided the Resource Object.
134     * <p>
135     * Note! this Object is immutable!
136     * </p>
137     * @param provider an Identification describing the provider of the Resource
138     * @return the Resource
139     */
140    public Resource<T> setProvider(Identification provider) {
141        return new Resource<>(resourceID, provider, resource, consumer);
142    }
143
144    /**
145     * returns the consumer of the object (if set).
146     * @return null or an Identification describing the consumer of the Resource
147     */
148    @Override
149    public Identification getConsumer() {
150        return consumer;
151    }
152
153    /**
154     * sets who should or has consumed the Resource Object.
155     * <p>
156     * Note! this Object is immutable!
157     * </p>
158     * @param consumer an Identification describing the consumer of the Resource
159     * @return new Resource
160     */
161    public Resource<T> setConsumer(Identification consumer) {
162        return new Resource<>(resourceID, provider, resource, consumer);
163    }
164
165    /**
166     * creates a list with this Element in it.
167     * @return a list
168     */
169    public List<Resource> toList() {
170        List<Resource> resourceList = new ArrayList<>();
171        resourceList.add(this);
172        return resourceList;
173    }
174
175    /**
176     * An ID must always be unique.
177     * A Class like Activator or OutputPlugin can just provide their .class.getCanonicalName()
178     * If you have to implement this interface multiple times, just concatenate unique Strings to
179     * .class.getCanonicalName()
180     *
181     * @return A String containing an ID
182     */
183    @Override
184    public String getID() {
185        return resourceID;
186    }
187
188    @Override
189    public String toString() {
190        return "Resource{" +
191                "resourceID='" + resourceID + '\'' +
192                ", provider=" + provider +
193                ", consumer=" + consumer +
194                ", resource=" + resource +
195                '}';
196    }
197}