001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2015
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 http://www.gnu.org/licenses.
027 */
028
029package edu.wisc.ssec.mcidasv.servermanager;
030
031/**
032 * Simplistic representation of ADDE accounting information. This is an
033 * immutable class.
034 */
035public class AddeAccount {
036
037    /** Username to hand off to the server. */
038    private final String username;
039
040    /** Project number (currently not limited to a numeric value). */
041    private final String project;
042
043    /** 
044     * Builds a new ADDE account object.
045     * 
046     * @param user Username to store. Cannot be {@code null}.
047     * @param proj Project number to store. Cannot be {@code null}.
048     * 
049     * @throws NullPointerException if {@code user} or {@code proj} is
050     * {@code null}.
051     */
052    public AddeAccount(final String user, final String proj) {
053        if (user == null) {
054            throw new NullPointerException("user cannot be null");
055        }
056        if (proj == null) {
057            throw new NullPointerException("proj cannot be null");
058        }
059        username = user;
060        project = proj;
061    }
062
063    /**
064     * Get the username associated with this account.
065     * 
066     * @return {@link #username}
067     */
068    public String getUsername() {
069        return username;
070    }
071
072    /**
073     * Get the project number associated with this account.
074     * 
075     * @return {@link #project}
076     */
077    public String getProject() {
078        return project;
079    }
080
081    /**
082     * Determines whether or not a given object is equivalent to this ADDE
083     * account. Currently the username and project number <b>are</b> case
084     * sensitive, though this is likely to change.
085     * 
086     * @param obj Object to test against.
087     * 
088     * @return Whether or not {@code obj} is equivalent to this ADDE account.
089     */
090    @Override public boolean equals(Object obj) {
091        if (this == obj) {
092            return true;
093        }
094        if (obj == null) {
095            return false;
096        }
097        if (!(obj instanceof AddeAccount)) {
098            return false;
099        }
100        AddeAccount other = (AddeAccount) obj;
101        if (!project.equals(other.project)) {
102            return false;
103        }
104        if (!username.equals(other.username)) {
105            return false;
106        }
107        return true;
108    }
109
110    /**
111     * Computes the hashcode of this ADDE account using the hashcodes of 
112     * {@link #username} and {@link #project}.
113     * 
114     * @return A hash code value for this object.
115     */
116    @Override public int hashCode() {
117        final int prime = 31;
118        int result = 1;
119        result = prime * result + project.hashCode();
120        result = prime * result + username.hashCode();
121        return result;
122    }
123
124    /**
125     * Returns a string representation of this account. The formatting of
126     * this string is subject to change, but currently looks like:<br/>
127     * <pre>[AddeAccount@HASHCODE: username=..., project=...]</pre>
128     * 
129     * @return {@link String} representation of this ADDE account.
130     */
131    public String toString() {
132        return String.format("[AddeAccount@%x: username=%s, project=%s]", hashCode(), username, project);
133    }
134
135    /**
136     * Returns a {@literal "human-friendly"} representation of this accounting
137     * object. Currently looks like {@code USER / PROJ}.
138     * 
139     * @return Friendly accounting detail {@code String}.
140     */
141    public String friendlyString() {
142        return username+" / "+project;
143    }
144}