Skip to content

Commit

Permalink
reduced repeated statements in MethodSubnegotiators
Browse files Browse the repository at this point in the history
  • Loading branch information
jh3nd3rs0n committed Oct 14, 2023
1 parent 9049373 commit f290fc4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -196,6 +197,25 @@ public MethodEncapsulation subnegotiate(
}

}

private static final class MethodSubnegotiators {

private final Map<Method, MethodSubnegotiator> methodSubnegotiatorsMap;

public MethodSubnegotiators() {
this.methodSubnegotiatorsMap =
new HashMap<Method, MethodSubnegotiator>();
}

public void add(final MethodSubnegotiator value) {
this.methodSubnegotiatorsMap.put(value.getMethod(), value);
}

public Map<Method, MethodSubnegotiator> toMap() {
return Collections.unmodifiableMap(this.methodSubnegotiatorsMap);
}

}

private static final class NoAcceptableMethodsMethodSubnegotiator
extends MethodSubnegotiator {
Expand Down Expand Up @@ -269,39 +289,26 @@ public MethodEncapsulation subnegotiate(

}

private static final Map<Method, MethodSubnegotiator> METHOD_SUBNEGOTIATOR_MAP;
private static final Map<Method, MethodSubnegotiator> METHOD_SUBNEGOTIATORS_MAP;

static {
METHOD_SUBNEGOTIATOR_MAP = new HashMap<Method, MethodSubnegotiator>();
MethodSubnegotiator gssapiMethodSubnegotiator =
new GssapiMethodSubnegotiator();
METHOD_SUBNEGOTIATOR_MAP.put(
gssapiMethodSubnegotiator.getMethod(),
gssapiMethodSubnegotiator);
MethodSubnegotiator noAcceptableMethodsMethodSubnegotiator =
new NoAcceptableMethodsMethodSubnegotiator();
METHOD_SUBNEGOTIATOR_MAP.put(
noAcceptableMethodsMethodSubnegotiator.getMethod(),
noAcceptableMethodsMethodSubnegotiator);
MethodSubnegotiator noAuthenticationRequiredMethodSubnegotiator =
new NoAuthenticationRequiredMethodSubnegotiator();
METHOD_SUBNEGOTIATOR_MAP.put(
noAuthenticationRequiredMethodSubnegotiator.getMethod(),
noAuthenticationRequiredMethodSubnegotiator);
MethodSubnegotiator usernamePasswordMethodSubnegotiator =
new UsernamePasswordMethodSubnegotiator();
METHOD_SUBNEGOTIATOR_MAP.put(
usernamePasswordMethodSubnegotiator.getMethod(),
usernamePasswordMethodSubnegotiator);
MethodSubnegotiators methodSubnegotiators = new MethodSubnegotiators();
methodSubnegotiators.add(new GssapiMethodSubnegotiator());
methodSubnegotiators.add(new NoAcceptableMethodsMethodSubnegotiator());
methodSubnegotiators.add(
new NoAuthenticationRequiredMethodSubnegotiator());
methodSubnegotiators.add(new UsernamePasswordMethodSubnegotiator());
METHOD_SUBNEGOTIATORS_MAP = new HashMap<Method, MethodSubnegotiator>(
methodSubnegotiators.toMap());
}

public static MethodSubnegotiator getInstance(final Method meth) {
MethodSubnegotiator methodSubnegotiator = METHOD_SUBNEGOTIATOR_MAP.get(
MethodSubnegotiator methodSubnegotiator = METHOD_SUBNEGOTIATORS_MAP.get(
meth);
if (methodSubnegotiator != null) {
return methodSubnegotiator;
}
String str = METHOD_SUBNEGOTIATOR_MAP.keySet().stream()
String str = METHOD_SUBNEGOTIATORS_MAP.keySet().stream()
.map(Method::toString)
.collect(Collectors.joining(", "));
throw new IllegalArgumentException(String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -206,7 +207,26 @@ public MethodSubnegotiationResults subnegotiate(
}

}


private static final class MethodSubnegotiators {

private final Map<Method, MethodSubnegotiator> methodSubnegotiatorsMap;

public MethodSubnegotiators() {
this.methodSubnegotiatorsMap =
new HashMap<Method, MethodSubnegotiator>();
}

public void add(final MethodSubnegotiator value) {
this.methodSubnegotiatorsMap.put(value.getMethod(), value);
}

public Map<Method, MethodSubnegotiator> toMap() {
return Collections.unmodifiableMap(this.methodSubnegotiatorsMap);
}

}

private static final class NoAcceptableMethodsMethodSubnegotiator
extends MethodSubnegotiator {

Expand Down Expand Up @@ -307,39 +327,26 @@ public MethodSubnegotiationResults subnegotiate(

}

private static final Map<Method, MethodSubnegotiator> METHOD_SUBNEGOTIATOR_MAP;
private static final Map<Method, MethodSubnegotiator> METHOD_SUBNEGOTIATORS_MAP;

static {
METHOD_SUBNEGOTIATOR_MAP = new HashMap<Method, MethodSubnegotiator>();
MethodSubnegotiator gssapiMethodSubnegotiator =
new GssapiMethodSubnegotiator();
METHOD_SUBNEGOTIATOR_MAP.put(
gssapiMethodSubnegotiator.getMethod(),
gssapiMethodSubnegotiator);
MethodSubnegotiator noAcceptableMethodsMethodSubnegotiator =
new NoAcceptableMethodsMethodSubnegotiator();
METHOD_SUBNEGOTIATOR_MAP.put(
noAcceptableMethodsMethodSubnegotiator.getMethod(),
noAcceptableMethodsMethodSubnegotiator);
MethodSubnegotiator noAuthenticationRequiredMethodSubnegotiator =
new NoAuthenticationRequiredMethodSubnegotiator();
METHOD_SUBNEGOTIATOR_MAP.put(
noAuthenticationRequiredMethodSubnegotiator.getMethod(),
noAuthenticationRequiredMethodSubnegotiator);
MethodSubnegotiator usernamePasswordMethodSubnegotiator =
new UsernamePasswordMethodSubnegotiator();
METHOD_SUBNEGOTIATOR_MAP.put(
usernamePasswordMethodSubnegotiator.getMethod(),
usernamePasswordMethodSubnegotiator);
MethodSubnegotiators methodSubnegotiators = new MethodSubnegotiators();
methodSubnegotiators.add(new GssapiMethodSubnegotiator());
methodSubnegotiators.add(new NoAcceptableMethodsMethodSubnegotiator());
methodSubnegotiators.add(
new NoAuthenticationRequiredMethodSubnegotiator());
methodSubnegotiators.add(new UsernamePasswordMethodSubnegotiator());
METHOD_SUBNEGOTIATORS_MAP = new HashMap<Method, MethodSubnegotiator>(
methodSubnegotiators.toMap());
}

public static MethodSubnegotiator getInstance(final Method meth) {
MethodSubnegotiator methodSubnegotiator = METHOD_SUBNEGOTIATOR_MAP.get(
MethodSubnegotiator methodSubnegotiator = METHOD_SUBNEGOTIATORS_MAP.get(
meth);
if (methodSubnegotiator != null) {
return methodSubnegotiator;
}
String str = METHOD_SUBNEGOTIATOR_MAP.keySet().stream()
String str = METHOD_SUBNEGOTIATORS_MAP.keySet().stream()
.map(Method::toString)
.collect(Collectors.joining(", "));
throw new IllegalArgumentException(String.format(
Expand Down

0 comments on commit f290fc4

Please sign in to comment.