001package org.intellimate.izou.resource;
002
003import org.intellimate.izou.identification.Identification;
004
005/**
006 * An Resource is an object which is used to pass data from one part of the application to another.
007 * <p>
008 * This is a minimal internal implementation! It should stay so to ensure that this implementations doesn't constrain
009 * the implementation.
010 * </p> 
011 * Note! This Object is immutable!
012 */
013public class ResourceMinimalImpl<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     * This method is thread-safe.
022     * @param resourceID the ID of the Resource
023     * @param provider the Provider of the Resource
024     * @param t the resource
025     * @param consumer the ID of the Consumer
026     */
027    public ResourceMinimalImpl(String resourceID, Identification provider, T t, Identification consumer) {
028        this.resourceID = resourceID;
029        this.provider = provider;
030        this.resource = t;
031        this.consumer = consumer;
032    }
033
034    /**
035     * returns the associated Resource data if set.
036     * This method is thread-safe.
037     * @return null or resource data
038     */
039    @Override
040    public T getResource() {
041        return resource;
042    }
043
044    /**
045     * sets the Resource data.
046     * <p>
047     * Note! this Object is immutable!
048     * </p>
049     * @param resource the data to set
050     * @return the Resource
051     */
052    public ResourceMinimalImpl<T> setResource(T resource) {
053        return new ResourceMinimalImpl<>(resourceID, provider, resource, consumer);
054    }
055
056    /**
057     * returns the ID of the Resource.
058     * This method is thread-safe.
059     * @return a String containing the ID of the resource
060     */
061    @Override
062    public String getResourceID() {
063        return resourceID;
064    }
065
066    /**
067     * returns the provider of the Resource.
068     * This method is thread-safe.
069     * @return an Identification describing the provider of the Resource or null if not set
070     */
071    @Override
072    public Identification getProvider() {
073        return provider;
074    }
075
076    /**
077     * returns whether a provider is set
078     * @return true if this resource has an provider, false if not
079     */
080    @Override
081    public boolean hasProvider() {
082        return provider != null;
083    }
084
085    /**
086     * returns the consumer of the object (if set).
087     * @return null or an Identification describing the consumer of the Resource
088     */
089    @Override
090    public Identification getConsumer() {
091        return consumer;
092    }
093
094    /**
095     * An ID must always be unique.
096     * A Class like Activator or OutputPlugin can just provide their .class.getCanonicalName()
097     * If you have to implement this interface multiple times, just concatenate unique Strings to
098     * .class.getCanonicalName()
099     *
100     * @return A String containing an ID
101     */
102    @Override
103    public String getID() {
104        return resourceID;
105    }
106}