Eric Kok
8 years ago
9 changed files with 225 additions and 55 deletions
@ -0,0 +1,42 @@ |
|||||||
|
package org.transdroid.connect.clients; |
||||||
|
|
||||||
|
import org.transdroid.connect.model.Torrent; |
||||||
|
|
||||||
|
import io.reactivex.Flowable; |
||||||
|
|
||||||
|
/** |
||||||
|
* Wraps an actual client implementation by calling through the appropriate method only if it is supported. This allows the final |
||||||
|
* {@link ClientSpec} API to expose all methods without forcing the individual implementations to implement unsupported featured with a no-op. |
||||||
|
*/ |
||||||
|
final class ClientDelegate implements ClientSpec { |
||||||
|
|
||||||
|
private final Client client; |
||||||
|
private final Object actual; |
||||||
|
|
||||||
|
ClientDelegate(Client client, Object actual) { |
||||||
|
this.client = client; |
||||||
|
this.actual = actual; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Flowable<Torrent> torrents() { |
||||||
|
if (client.supports(Feature.LISTING)) |
||||||
|
return ((Feature.Listing) actual).torrents(); |
||||||
|
throw new UnsupportedFeatureException(client, Feature.LISTING); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Flowable<String> clientVersion() { |
||||||
|
if (client.supports(Feature.VERSION)) |
||||||
|
return ((Feature.Version) actual).clientVersion(); |
||||||
|
throw new UnsupportedFeatureException(client, Feature.VERSION); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Flowable<Torrent> forceStartTorrent() { |
||||||
|
if (client.supports(Feature.FORCE_STARTING)) |
||||||
|
return ((Feature.ForceStarting) actual).forceStartTorrent(); |
||||||
|
throw new UnsupportedFeatureException(client, Feature.FORCE_STARTING); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,13 +1,10 @@ |
|||||||
package org.transdroid.connect.clients; |
package org.transdroid.connect.clients; |
||||||
|
|
||||||
import org.transdroid.connect.model.Torrent; |
public interface ClientSpec extends |
||||||
|
Feature.Version, |
||||||
import io.reactivex.Flowable; |
Feature.Listing, |
||||||
|
Feature.StartingStopping, |
||||||
public interface ClientSpec { |
Feature.ResumingPausing, |
||||||
|
Feature.ForceStarting { |
||||||
Flowable<String> clientVersion(); |
|
||||||
|
|
||||||
Flowable<Torrent> torrents(); |
|
||||||
|
|
||||||
} |
} |
||||||
|
@ -1,11 +1,55 @@ |
|||||||
package org.transdroid.connect.clients; |
package org.transdroid.connect.clients; |
||||||
|
|
||||||
|
import org.transdroid.connect.model.Torrent; |
||||||
|
|
||||||
|
import io.reactivex.Flowable; |
||||||
|
|
||||||
|
/** |
||||||
|
* Available feature enum which can be implemented by clients. Use {@link Client#supports(Feature)} to see if a certain {@link Client} support a |
||||||
|
* {@link Feature}. |
||||||
|
*/ |
||||||
public enum Feature { |
public enum Feature { |
||||||
|
|
||||||
VERSION, |
VERSION(Version.class), |
||||||
STARTING, |
LISTING(Listing.class), |
||||||
STOPPING, |
STARTING_STOPPING(StartingStopping.class), |
||||||
RESUMING, |
RESUMING_PAUSING(ResumingPausing.class), |
||||||
PAUSING |
FORCE_STARTING(ForceStarting.class); |
||||||
|
|
||||||
|
private final Class<?> type; |
||||||
|
|
||||||
|
Feature(Class<?> type) { |
||||||
|
this.type = type; |
||||||
|
} |
||||||
|
|
||||||
|
public Class<?> type() { |
||||||
|
return type; |
||||||
|
} |
||||||
|
|
||||||
|
public interface Version { |
||||||
|
|
||||||
|
Flowable<String> clientVersion(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public interface Listing { |
||||||
|
|
||||||
|
Flowable<Torrent> torrents(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public interface StartingStopping { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public interface ResumingPausing { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public interface ForceStarting { |
||||||
|
|
||||||
|
Flowable<Torrent> forceStartTorrent(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
} |
} |
||||||
|
@ -0,0 +1,22 @@ |
|||||||
|
package org.transdroid.connect.clients; |
||||||
|
|
||||||
|
import sun.reflect.generics.reflectiveObjects.NotImplementedException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Thrown when trying to call into a client method for a feature which the client does not support. |
||||||
|
*/ |
||||||
|
public class UnsupportedFeatureException extends NotImplementedException { |
||||||
|
|
||||||
|
private final Client client; |
||||||
|
private final Feature feature; |
||||||
|
|
||||||
|
UnsupportedFeatureException(Client client, Feature feature) { |
||||||
|
this.client = client; |
||||||
|
this.feature = feature; |
||||||
|
} |
||||||
|
|
||||||
|
public String getMessage() { |
||||||
|
return client.name() + " does not support " + feature.name(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,5 @@ |
|||||||
|
package org.transdroid.connect.clients.transmission; |
||||||
|
|
||||||
|
public final class Transmission { |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue