Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored "Cloud#type" #508

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DropboxCloudContentRepositoryFactory(Context context) {

@Override
public boolean supports(Cloud cloud) {
return cloud.type() == DROPBOX;
return cloud.getType() == DROPBOX;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public OnedriveCloudContentRepositoryFactory(Context context) {

@Override
public boolean supports(Cloud cloud) {
return cloud.type() == ONEDRIVE;
return cloud.getType() == ONEDRIVE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public PCloudContentRepositoryFactory(Context context) {

@Override
public boolean supports(Cloud cloud) {
return cloud.type() == PCLOUD;
return cloud.getType() == PCLOUD;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public GoogleDriveCloudContentRepositoryFactory(Context context, GoogleDriveIdCa

@Override
public boolean supports(Cloud cloud) {
return cloud.type() == CloudType.GOOGLE_DRIVE;
return cloud.getType() == CloudType.GOOGLE_DRIVE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.cryptomator.domain.Cloud;
import org.cryptomator.domain.CloudType;
import org.cryptomator.domain.Vault;
import org.jetbrains.annotations.NotNull;

public class CryptoCloud implements Cloud {

Expand All @@ -17,8 +18,9 @@ public Long id() {
return null;
}

@NotNull
@Override
public CloudType type() {
public CloudType getType() {
return CloudType.CRYPTO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CryptoCloudContentRepositoryFactory(Lazy<CloudContentRepository> cloudCon

@Override
public boolean supports(Cloud cloud) {
return cloud.type() == CRYPTO;
return cloud.getType() == CRYPTO;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public LocalStorageContentRepositoryFactory(Context context, MimeTypes mimeTypes

@Override
public boolean supports(Cloud cloud) {
return cloud.type() == LOCAL;
return cloud.getType() == LOCAL;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public S3CloudContentRepositoryFactory(Context context, SharedPreferencesHandler

@Override
public boolean supports(Cloud cloud) {
return cloud.type() == S3;
return cloud.getType() == S3;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class WebDavCloudContentRepositoryFactory implements CloudContentReposito

@Override
public boolean supports(Cloud cloud) {
return cloud.type() == WEBDAV;
return cloud.getType() == WEBDAV;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public Cloud fromEntity(CloudEntity entity) {
public CloudEntity toEntity(Cloud domainObject) {
CloudEntity result = new CloudEntity();
result.setId(domainObject.id());
result.setType(domainObject.type().name());
switch (domainObject.type()) {
result.setType(domainObject.getType().name());
switch (domainObject.getType()) {
case DROPBOX:
result.setAccessToken(((DropboxCloud) domainObject).accessToken());
result.setUsername(((DropboxCloud) domainObject).username());
Expand Down Expand Up @@ -126,7 +126,7 @@ public CloudEntity toEntity(Cloud domainObject) {
result.setWebdavCertificate(((WebDavCloud) domainObject).certificate());
break;
default:
throw new IllegalStateException("Unhandled enum constant " + domainObject.type());
throw new IllegalStateException("Unhandled enum constant " + domainObject.getType());
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public List<Cloud> clouds(CloudType cloudType) throws BackendException {
List<Cloud> allClouds = mapper.fromEntities(database.loadAll(CloudEntity.class));

for (Cloud cloud : allClouds) {
if (cloud.type().equals(cloudType)) {
if (cloud.getType().equals(cloudType)) {
cloudsFromType.add(cloud);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ internal class MasterkeyCryptoCloudProviderTest {
fun testUnlockVault() {
val cloudType: CloudType = mock()

whenever(cloud.type()).thenReturn(cloudType)
whenever(cloud.type).thenReturn(cloudType)
whenever(vault.cloud).thenReturn(cloud)
whenever(vault.cloudType).thenReturn(cloudType)
whenever(vault.format).thenReturn(8)
Expand All @@ -183,7 +183,7 @@ internal class MasterkeyCryptoCloudProviderTest {
fun testUnlockLegacyVault() {
val cloudType: CloudType = mock()

whenever(cloud.type()).thenReturn(cloudType)
whenever(cloud.type).thenReturn(cloudType)
whenever(vault.cloud).thenReturn(cloud)
whenever(vault.cloudType).thenReturn(cloudType)
whenever(vault.format).thenReturn(7)
Expand Down Expand Up @@ -217,7 +217,7 @@ internal class MasterkeyCryptoCloudProviderTest {
fun tesChangePassword(legacy: Boolean) {
val cloudType: CloudType = mock()

whenever(cloud.type()).thenReturn(cloudType)
whenever(cloud.type).thenReturn(cloudType)
whenever(vault.cloud).thenReturn(cloud)
whenever(vault.cloudType).thenReturn(cloudType)
whenever(vault.format).thenReturn(7)
Expand Down Expand Up @@ -334,7 +334,7 @@ internal class MasterkeyCryptoCloudProviderTest {
private fun testVaultPasswordVault(masterkeyContent: String, unverifiedVaultConfig: Optional<UnverifiedVaultConfig>, password: String): Boolean {
val cloudType: CloudType = mock()

whenever(cloud.type()).thenReturn(cloudType)
whenever(cloud.type).thenReturn(cloudType)
whenever(vault.cloud).thenReturn(cloud)
whenever(vault.cloudType).thenReturn(cloudType)
whenever(vault.format).thenReturn(7)
Expand Down
2 changes: 1 addition & 1 deletion domain/src/main/java/org/cryptomator/domain/Cloud.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.io.Serializable
interface Cloud : Serializable {

fun id(): Long?
fun type(): CloudType?
val type: CloudType
fun configurationMatches(cloud: Cloud?): Boolean
fun persistent(): Boolean
fun requiresNetwork(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public String username() {
return username;
}

@NotNull
@Override
public CloudType type() {
public CloudType getType() {
return CloudType.DROPBOX;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public Long id() {
return id;
}

@NotNull
@Override
public CloudType type() {
public CloudType getType() {
return CloudType.GOOGLE_DRIVE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public String rootUri() {
return rootUri;
}

@NotNull
@Override
public CloudType type() {
public CloudType getType() {
return CloudType.LOCAL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public String username() {
return username;
}

@NotNull
@Override
public CloudType type() {
public CloudType getType() {
return CloudType.ONEDRIVE;
}

Expand Down
3 changes: 2 additions & 1 deletion domain/src/main/java/org/cryptomator/domain/PCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public String username() {
return username;
}

@NotNull
@Override
public CloudType type() {
public CloudType getType() {
return CloudType.PCLOUD;
}

Expand Down
3 changes: 2 additions & 1 deletion domain/src/main/java/org/cryptomator/domain/S3Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public String displayName() {
return displayName;
}

@NotNull
@Override
public CloudType type() {
public CloudType getType() {
return CloudType.S3;
}

Expand Down
6 changes: 3 additions & 3 deletions domain/src/main/java/org/cryptomator/domain/Vault.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Builder withCloud(Cloud cloud) {
this.cloud = cloud;

if (cloud != null) {
this.cloudType = cloud.type();
this.cloudType = cloud.getType();
}

return this;
Expand All @@ -168,7 +168,7 @@ public Builder withCloud(Cloud cloud) {
public Builder withCloudType(CloudType cloudType) {
this.cloudType = cloudType;

if (cloud != null && cloud.type() != cloudType) {
if (cloud != null && cloud.getType() != cloudType) {
throw new IllegalStateException("Cloud type must match cloud");
}

Expand All @@ -179,7 +179,7 @@ public Builder withNamePathAndCloudFrom(CloudFolder vaultFolder) {
this.name = vaultFolder.getName();
this.path = vaultFolder.getPath();
this.cloud = vaultFolder.getCloud();
this.cloudType = cloud.type();
this.cloudType = cloud.getType();
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion domain/src/main/java/org/cryptomator/domain/WebDavCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ private boolean configurationMatches(WebDavCloud cloud) {
return url.equals(cloud.url) && username.equals(cloud.username);
}

@NotNull
@Override
public CloudType type() {
public CloudType getType() {
return CloudType.WEBDAV;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void execute() throws BackendException {
}

private boolean cloudExists(Cloud cloud) throws BackendException {
for (Cloud storedCloud : cloudRepository.clouds(cloud.type())) {
for (Cloud storedCloud : cloudRepository.clouds(cloud.getType())) {
if (cloud.id() != null && cloud.id().equals(storedCloud.id())) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ private Cloud cloudWithUsernameAndAccessTokenRemoved(Cloud cloud) {
.withAccessToken(null) //
.build();
}
throw new IllegalStateException("Logout not supported for cloud with type " + cloud.type());
throw new IllegalStateException("Logout not supported for cloud with type " + cloud.getType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CloudFolderModel(cloudFolder: CloudFolder) : CloudNodeModel<CloudFolder>(c
get() = true

fun vault(): VaultModel? {
return if (toCloudNode().cloud?.type() == CloudType.CRYPTO) {
return if (toCloudNode().cloud?.type == CloudType.CRYPTO) {
VaultModel((toCloudNode().cloud as CryptoCloud).vault)
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CloudModelMapper @Inject constructor() : ModelMapper<CloudModel, Cloud>()
}

override fun toModel(domainObject: Cloud): CloudModel {
return when (domainObject.type()?.let { CloudTypeModel.valueOf(it) }) {
return when (domainObject.type.let { CloudTypeModel.valueOf(it) }) {
CloudTypeModel.DROPBOX -> DropboxCloudModel(domainObject)
CloudTypeModel.GOOGLE_DRIVE -> GoogleDriveCloudModel(domainObject)
CloudTypeModel.LOCAL -> LocalStorageModel(domainObject)
Expand All @@ -31,7 +31,6 @@ class CloudModelMapper @Inject constructor() : ModelMapper<CloudModel, Cloud>()
CloudTypeModel.S3 -> S3CloudModel(domainObject)
CloudTypeModel.CRYPTO -> CryptoCloudModel(domainObject)
CloudTypeModel.WEBDAV -> WebDavCloudModel(domainObject)
null -> throw IllegalStateException("The type of the object shouldn't be null")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ class AuthenticateCloudPresenter @Inject constructor( //
}

private fun updateUsernameOf(cloud: Cloud, username: String): Cloud {
return when (cloud.type()) {
return when (cloud.type) {
CloudType.DROPBOX -> DropboxCloud.aCopyOf(cloud as DropboxCloud).withUsername(username).build()
CloudType.ONEDRIVE -> OnedriveCloud.aCopyOf(cloud as OnedriveCloud).withUsername(username).build()
else -> throw IllegalStateException("Cloud " + cloud.type() + " is not supported")
else -> throw IllegalStateException("Cloud " + cloud.type + " is not supported")
Comment on lines +117 to +120
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add missing cases to the when expression

The when expression only handles DROPBOX and ONEDRIVE cases, but other cloud types like GOOGLE_DRIVE, WEBDAV, S3, PCLOUD, and LOCAL could also be passed to this method. This could lead to runtime exceptions.

Consider adding all possible cases or documenting why only these types are handled:

 return when (cloud.type) {
     CloudType.DROPBOX -> DropboxCloud.aCopyOf(cloud as DropboxCloud).withUsername(username).build()
     CloudType.ONEDRIVE -> OnedriveCloud.aCopyOf(cloud as OnedriveCloud).withUsername(username).build()
+    CloudType.GOOGLE_DRIVE -> GoogleDriveCloud.aCopyOf(cloud as GoogleDriveCloud).withUsername(username).build()
+    CloudType.WEBDAV -> WebDavCloud.aCopyOf(cloud as WebDavCloud).withUsername(username).build()
+    CloudType.S3 -> S3Cloud.aCopyOf(cloud as S3Cloud).withUsername(username).build()
+    CloudType.PCLOUD -> PCloud.aCopyOf(cloud as PCloud).withUsername(username).build()
+    CloudType.LOCAL -> LocalStorage.aCopyOf(cloud as LocalStorage).withUsername(username).build()
     else -> throw IllegalStateException("Cloud " + cloud.type + " is not supported")
 }

Committable suggestion skipped: line range outside the PR's diff.

}
}

Expand Down Expand Up @@ -336,7 +336,7 @@ class AuthenticateCloudPresenter @Inject constructor( //

fun prepareForSavingPCloud(cloud: PCloud) {
getCloudsUseCase //
.withCloudType(cloud.type()) //
.withCloudType(cloud.type) //
.run(object : DefaultResultHandler<List<Cloud>>() {
override fun onSuccess(clouds: List<Cloud>) {
clouds.firstOrNull {
Expand Down
Loading