Skip to content

Commit

Permalink
devonfw#750: Created method implementation for each enum constant
Browse files Browse the repository at this point in the history
Removed switch and created a method implementation for each enum constant instead and made it abstract
  • Loading branch information
leonrohne27 committed Nov 14, 2024
1 parent 2b473ff commit eb9c1e0
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions cli/src/main/java/com/devonfw/tools/ide/context/GitUrlSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,45 @@ public enum GitUrlSyntax {
/**
* The DEFAULT Git URL syntax
*/
DEFAULT(""),
DEFAULT("") {
@Override
public GitUrl format(GitUrl gitUrl) {
return gitUrl; // No conversion for DEFAULT
}
},
/**
* The SSH Git URL syntax (e.g., [email protected]:user/repo.git).
*/
SSH("git@"),
SSH("git@") {
@Override
public GitUrl format(GitUrl gitUrl) {
String url = gitUrl.url();
if (url.startsWith(HTTPS.prefix)) {
int index = url.indexOf("/", HTTPS.prefix.length());
if (index > 0) {
url = SSH.prefix + url.substring(HTTPS.prefix.length(), index) + ":" + url.substring(index + 1);
}
}
return new GitUrl(url, gitUrl.branch());
}
},

/**
* The HTTPS Git URL syntax (e.g., https://github.com/user/repo.git).
*/
HTTPS("https://");
HTTPS("https://") {
@Override
public GitUrl format(GitUrl gitUrl) {
String url = gitUrl.url();
if (url.startsWith(SSH.prefix)) {
int index = url.indexOf(":");
if (index > 0) {
url = HTTPS.prefix + url.substring(SSH.prefix.length(), index) + "/" + url.substring(index + 1);
}
}
return new GitUrl(url, gitUrl.branch());
}
};

private final String prefix;

Expand All @@ -40,39 +69,7 @@ public enum GitUrlSyntax {
* @return the formatted {@link GitUrl} according to this syntax.
* @throws IllegalArgumentException if the protocol is not supported.
*/
public GitUrl format(GitUrl gitUrl) {
if (this == DEFAULT) {
return gitUrl;
}
String url = gitUrl.url();

// Prevent conversion for domains in the no-conversion list
if (isDomainWithNoConversion(url.toLowerCase())) {
return gitUrl;
}

switch (this) {
case SSH -> {
if (url.startsWith(HTTPS.prefix)) {
int index = url.indexOf("/", HTTPS.prefix.length());
if (index > 0) {
url = SSH.prefix + url.substring(HTTPS.prefix.length(), index) + ":" + url.substring(index + 1);
}
}
}
case HTTPS -> {
if (url.startsWith(SSH.prefix)) {
int index = url.indexOf(":");
if (index > 0) {
url = HTTPS.prefix + url.substring(SSH.prefix.length(), index) + "/" + url.substring(index + 1);
}
}
}
default -> throw new IllegalArgumentException("Unsupported protocol: " + this);
}

return new GitUrl(url, gitUrl.branch());
}
public abstract GitUrl format(GitUrl gitUrl);

private boolean isDomainWithNoConversion(String url) {

Expand Down

0 comments on commit eb9c1e0

Please sign in to comment.