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