001package jmri.profile;
002
003import java.io.File;
004import java.io.IOException;
005import javax.annotation.Nonnull;
006
007/**
008 * An empty JMRI application profile. Profiles allow a JMRI application to load
009 * completely separate set of preferences at each launch without relying on host
010 * OS-specific tricks to ensure this happens.
011 * <p>
012 * A NullProfile allows an application using JMRI as a library to set the active
013 * JMRI profile to an identity set by that application, if the use of a standard
014 * JMRI profile is not acceptable.
015 * <p>
016 * This class deliberately overrides all methods of {@link jmri.profile.Profile}
017 * that access the {@code name} and {@code id} fields to remove protections
018 * and restrictions on those fields.
019 *
020 * @author Randall Wood Copyright (C) 2014
021 * @see jmri.profile.ProfileManager#setActiveProfile(jmri.profile.Profile)
022 */
023public class NullProfile extends Profile {
024
025    /**
026     * Create a NullProfile object given just a path to it. The Profile must
027     * exist in storage on the computer. Uses a random identity for the Profile.
028     *
029     * @param path The Profile's directory
030     * @throws java.io.IOException If path is not readable
031     */
032    public NullProfile(@Nonnull File path) throws IOException {
033        super(path, false);
034    }
035
036    /**
037     * Create a NullProfile object given just a path to it. The Profile must
038     * exist in storage on the computer.
039     *
040     * @param path The Profile's directory
041     * @param id   The Profile's id
042     * @throws java.io.IOException If path is not readable
043     */
044    public NullProfile(@Nonnull File path, @Nonnull String id) throws IOException {
045        super(path, id, false);
046    }
047
048    /**
049     * Create a Profile object and a profile in storage. A Profile cannot exist
050     * in storage on the computer at the path given. Since this is a new
051     * profile, the id must match the last element in the path.
052     * <p>
053     * This is the only time the id can be set on a Profile, as the id becomes a
054     * read-only property of the Profile. The {@link ProfileManager} will only
055     * load a single profile with a given id.
056     *
057     * @param name The name of the profile.
058     * @param id   If null, {@link jmri.profile.ProfileManager#createUniqueId()}
059     *             will be used to generate the id.
060     * @param path The path where the profile is stored.
061     * @throws java.io.IOException If path is not readable.
062     * @throws IllegalArgumentException If a profile already exists at or within path
063     */
064    public NullProfile(String name, String id, @Nonnull File path) throws IOException {
065        this(path, (null != id) ? id : ProfileManager.createUniqueId());
066        this.setNameInConstructor(name);
067    }
068
069    @Override
070    public String toString() {
071        return this.getName();
072    }
073
074    @Override
075    public int hashCode() {
076        int hash = 7;
077        hash = 71 * hash + this.getId().hashCode();
078        return hash;
079    }
080
081    @Override
082    public boolean equals(Object obj) {
083        if (obj == null) {
084            return false;
085        }
086        if (getClass() != obj.getClass()) {
087            return false;
088        }
089        final NullProfile other = (NullProfile) obj;
090        return this.getId().equals(other.getId());
091    }
092
093    /**
094     * Test if the profile is complete.
095     *
096     * @return always true for a NullProfile.
097     */
098    @Override
099    public boolean isComplete() {
100        return true;
101    }
102
103    /**
104     * Return the uniqueness portion of the Profile Id.
105     *
106     * @return The complete Id of a NullProfile.
107     */
108    @Override
109    public String getUniqueId() {
110        return this.getId();
111    }
112}