001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.network;
020
021import icy.preferences.XMLPreferences;
022import icy.util.StringUtil;
023
024/**
025 * @author Stephane
026 */
027public class AuthenticationInfo
028{
029    private static final String ID_LOGIN = "login";
030    private static final String ID_PASSWORD = "password";
031    private static final String ID_ENABLED = "enabled";
032
033    private String login;
034    private String password;
035    private boolean enabled;
036
037    public AuthenticationInfo(String login, String password, boolean enabled)
038    {
039        super();
040
041        this.login = login;
042        this.password = password;
043        this.enabled = enabled;
044    }
045
046    public AuthenticationInfo(XMLPreferences node)
047    {
048        this("", "", false);
049
050        load(node);
051    }
052
053    /**
054     * @return the login
055     */
056    public String getLogin()
057    {
058        return login;
059    }
060
061    /**
062     * @param login
063     *        the login to set
064     */
065    public void setLogin(String login)
066    {
067        this.login = login;
068    }
069
070    /**
071     * @return the password
072     */
073    public String getPassword()
074    {
075        return password;
076    }
077
078    /**
079     * @param password
080     *        the password to set
081     */
082    public void setPassword(String password)
083    {
084        this.password = password;
085    }
086
087    /**
088     * @return the enabled
089     */
090    public boolean isEnabled()
091    {
092        return enabled;
093    }
094
095    /**
096     * @param enabled
097     *        the enabled to set
098     */
099    public void setEnabled(boolean enabled)
100    {
101        this.enabled = enabled;
102    }
103
104    public void save(XMLPreferences node)
105    {
106        if (node != null)
107        {
108            node.put(ID_LOGIN, login);
109            node.put(ID_PASSWORD, password);
110            node.putBoolean(ID_ENABLED, enabled);
111        }
112    }
113
114    public void load(XMLPreferences node)
115    {
116        if (node != null)
117        {
118            login = node.get(ID_LOGIN, "");
119            password = node.get(ID_PASSWORD, "");
120            enabled = node.getBoolean(ID_ENABLED, false);
121        }
122    }
123
124    @Override
125    public int hashCode()
126    {
127        return login.hashCode() ^ password.hashCode();
128    }
129
130    @Override
131    public boolean equals(Object obj)
132    {
133        if (obj instanceof AuthenticationInfo)
134        {
135            final AuthenticationInfo auth = (AuthenticationInfo) obj;
136
137            return StringUtil.equals(auth.login, login) && StringUtil.equals(auth.password, password);
138        }
139
140        return super.equals(obj);
141    }
142}