Skip to content

Commit

Permalink
Implement comparable to find bookmark in collection.
Browse files Browse the repository at this point in the history
Former-commit-id: 0a72f034d0d3020bfc43a229f426957d12733816
  • Loading branch information
dkocher committed Sep 19, 2013
1 parent e4c4b29 commit 15c0e5e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
14 changes: 14 additions & 0 deletions source/ch/cyberduck/core/AbstractHostCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,18 @@ protected void load(Collection<Host> c) {
this.addAll(c);
this.collectionLoaded();
}

/**
* Search using comparator
*
* @param bookmark Bookmark to find that matches comparison
*/
public boolean find(final Host bookmark) {
for(Host h : this) {
if(h.compareTo(bookmark) == 0) {
return true;
}
}
return false;
}
}
19 changes: 18 additions & 1 deletion source/ch/cyberduck/core/Host.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* @version $Id$
*/
public final class Host implements Serializable {
public final class Host implements Serializable, Comparable<Host> {
private static final Logger log = Logger.getLogger(Host.class);

/**
Expand Down Expand Up @@ -685,6 +685,23 @@ public int hashCode() {
return this.getUuid().hashCode();
}

@Override
public int compareTo(final Host other) {
if(port != other.port) {
return -1;
}
if(credentials != null ? !credentials.equals(other.credentials) : other.credentials != null) {
return -1;
}
if(hostname != null ? !hostname.equals(other.hostname) : other.hostname != null) {
return -1;
}
if(protocol != null ? !protocol.equals(other.protocol) : other.protocol != null) {
return -1;
}
return 0;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Host{");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.log4j.Logger;

import java.text.MessageFormat;
import java.util.Iterator;

/**
* @version $Id$
Expand Down Expand Up @@ -72,7 +73,7 @@ public void load() {
log.info(String.format("Checksum changed for bookmarks file at %s", file.getAbsolute()));
}
// Should filter existing bookmarks
// this.parse(file);
this.parse(file);
}
}
}
Expand Down Expand Up @@ -145,4 +146,19 @@ public boolean add(final Host bookmark) {
}
return super.add(bookmark);
}

/**
* Remove all that are contained within the collection passed
*/
public void filter(final AbstractHostCollection bookmarks) {
for(Iterator<Host> iter = this.iterator(); iter.hasNext(); ) {
final Host i = iter.next();
if(bookmarks.find(i)) {
if(log.isInfoEnabled()) {
log.info(String.format("Remove %s from import as we found it in bookmarks", i));
}
iter.remove();
}
}
}
}

0 comments on commit 15c0e5e

Please sign in to comment.