extensions/net.sf.basedb.xfiles/trunk/src/net/sf/basedb/xfiles/sftp/SftpConnectionParametersComparator.java

Code
Comments
Other
Rev Date Author Line
2571 13 Aug 14 nicklas 1 /**
2571 13 Aug 14 nicklas 2   $Id$
2571 13 Aug 14 nicklas 3
2571 13 Aug 14 nicklas 4   Copyright (C) 2014 Nicklas Nordborg
2571 13 Aug 14 nicklas 5
2571 13 Aug 14 nicklas 6   This file is part of BASE - BioArray Software Environment.
2571 13 Aug 14 nicklas 7   Available at http://base.thep.lu.se/
2571 13 Aug 14 nicklas 8
2571 13 Aug 14 nicklas 9   BASE is free software; you can redistribute it and/or
2571 13 Aug 14 nicklas 10   modify it under the terms of the GNU General Public License
2571 13 Aug 14 nicklas 11   as published by the Free Software Foundation; either version 3
2571 13 Aug 14 nicklas 12   of the License, or (at your option) any later version.
2571 13 Aug 14 nicklas 13
2571 13 Aug 14 nicklas 14   BASE is distributed in the hope that it will be useful,
2571 13 Aug 14 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
2571 13 Aug 14 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2571 13 Aug 14 nicklas 17   GNU General Public License for more details.
2571 13 Aug 14 nicklas 18
2571 13 Aug 14 nicklas 19   You should have received a copy of the GNU General Public License
2571 13 Aug 14 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
2571 13 Aug 14 nicklas 21 */
2571 13 Aug 14 nicklas 22 package net.sf.basedb.xfiles.sftp;
2571 13 Aug 14 nicklas 23
2571 13 Aug 14 nicklas 24 import java.util.Comparator;
2571 13 Aug 14 nicklas 25
2572 13 Aug 14 nicklas 26 import net.sf.basedb.xfiles.ConnectionInfo;
2571 13 Aug 14 nicklas 27
2571 13 Aug 14 nicklas 28 /**
2571 13 Aug 14 nicklas 29   Comparator implementation that compare two sets of connection
2571 13 Aug 14 nicklas 30   parameters for the SSH protocol. The parameters are considered
2572 13 Aug 14 nicklas 31   equal if the connect to the same host with the same username and
2572 13 Aug 14 nicklas 32   password.
2571 13 Aug 14 nicklas 33
2571 13 Aug 14 nicklas 34   @since 1.1
2571 13 Aug 14 nicklas 35 */
2571 13 Aug 14 nicklas 36 public class SftpConnectionParametersComparator 
2572 13 Aug 14 nicklas 37   implements Comparator<ConnectionInfo>
2571 13 Aug 14 nicklas 38 {
2571 13 Aug 14 nicklas 39
2571 13 Aug 14 nicklas 40   @Override
2572 13 Aug 14 nicklas 41   public int compare(ConnectionInfo cp1, ConnectionInfo cp2)
2571 13 Aug 14 nicklas 42   {
2572 13 Aug 14 nicklas 43     int result = compare(cp1.uri.getHost(), cp2.uri.getHost());
2571 13 Aug 14 nicklas 44     if (result == 0)
2571 13 Aug 14 nicklas 45     {
2572 13 Aug 14 nicklas 46       result = cp2.uri.getPort() - cp1.uri.getPort();
2571 13 Aug 14 nicklas 47     }
2572 13 Aug 14 nicklas 48     if (result == 0)
2572 13 Aug 14 nicklas 49     {
2572 13 Aug 14 nicklas 50       result = compare(cp1.parameters.getUsername(), cp2.parameters.getUsername());
2572 13 Aug 14 nicklas 51     }
2572 13 Aug 14 nicklas 52     if (result == 0)
2572 13 Aug 14 nicklas 53     {
2572 13 Aug 14 nicklas 54       result = compare(cp1.parameters.getPassword(), cp2.parameters.getPassword());
2572 13 Aug 14 nicklas 55     }
2571 13 Aug 14 nicklas 56     return result;
2571 13 Aug 14 nicklas 57   }
2571 13 Aug 14 nicklas 58
2571 13 Aug 14 nicklas 59   
2571 13 Aug 14 nicklas 60   private int compare(String s1, String s2)
2571 13 Aug 14 nicklas 61   {
2571 13 Aug 14 nicklas 62     if (s1 == s2) return 0;
2571 13 Aug 14 nicklas 63     if (s1 == null) return 1;
2571 13 Aug 14 nicklas 64     if (s2 == null) return -1;
2571 13 Aug 14 nicklas 65     return s1.compareTo(s2);
2571 13 Aug 14 nicklas 66   }
2571 13 Aug 14 nicklas 67 }