001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas/
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see https://www.gnu.org/licenses/.
027 */
028
029package edu.wisc.ssec.mcidasv.servermanager;
030
031import static java.util.Objects.requireNonNull;
032
033import edu.wisc.ssec.mcidasv.util.MakeToString;
034
035/**
036 * Simplistic representation of ADDE accounting information. This is an
037 * immutable class.
038 */
039public class AddeAccount {
040
041    /** Username to hand off to the server. */
042    private final String username;
043
044    /** Project number (currently not limited to a numeric value). */
045    private final String project;
046
047    /** 
048     * Builds a new ADDE account object.
049     * 
050     * @param user Username to store. Cannot be {@code null}.
051     * @param proj Project number to store. Cannot be {@code null}.
052     * 
053     * @throws NullPointerException if {@code user} or {@code proj} is
054     * {@code null}.
055     */
056    public AddeAccount(final String user, final String proj) {
057        username = requireNonNull(user, "user cannot be null");
058        project = requireNonNull(proj, "proj cannot be null");
059    }
060
061    /**
062     * Get the username associated with this account.
063     * 
064     * @return {@link #username}
065     */
066    public String getUsername() {
067        return username;
068    }
069
070    /**
071     * Get the project number associated with this account.
072     * 
073     * @return {@link #project}
074     */
075    public String getProject() {
076        return project;
077    }
078
079    /**
080     * Determines whether or not a given object is equivalent to this ADDE
081     * account. Currently the username and project number <b>are</b> case
082     * sensitive, though this is likely to change.
083     * 
084     * @param obj Object to test against.
085     * 
086     * @return Whether or not {@code obj} is equivalent to this ADDE account.
087     */
088    @Override public boolean equals(Object obj) {
089        if (this == obj) {
090            return true;
091        }
092        if (obj == null) {
093            return false;
094        }
095        if (!(obj instanceof AddeAccount)) {
096            return false;
097        }
098        AddeAccount other = (AddeAccount) obj;
099        if (!project.equals(other.project)) {
100            return false;
101        }
102        if (!username.equals(other.username)) {
103            return false;
104        }
105        return true;
106    }
107
108    /**
109     * Computes the hashcode of this ADDE account using the hashcodes of 
110     * {@link #username} and {@link #project}.
111     * 
112     * @return A hash code value for this object.
113     */
114    @Override public int hashCode() {
115        final int prime = 31;
116        int result = 1;
117        result = prime * result + project.hashCode();
118        result = prime * result + username.hashCode();
119        return result;
120    }
121
122    /**
123     * Returns a string representation of this account.
124     *
125     * The formatting of this string is subject to change, but currently looks
126     * like {@code [AddeAccount@HASHCODE: username=..., project=...]}.
127     * 
128     * @return {@link String} representation of this ADDE account.
129     */
130    public String toString() {
131        return MakeToString.fromInstance(this)
132                           .add("username", username)
133                           .add("project", project).toString();
134    }
135
136    /**
137     * Returns a {@literal "human-friendly"} representation of this accounting
138     * object. Currently looks like {@code USER / PROJ}.
139     * 
140     * @return Friendly accounting detail {@code String}.
141     */
142    public String friendlyString() {
143        return username+" / "+project;
144    }
145}