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

Refactor : Removed Some Code smells to increase Readability and Maintainability #39

Open
wants to merge 3 commits into
base: master
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
6 changes: 4 additions & 2 deletions src/main/java/org/burningwave/core/Criteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,15 @@ <E, C extends Simple<E, C>> Predicate<E> consumeLogicalOperator (
Function<Predicate<E>,
Predicate<E>> logicalOperator
) {
int methodIndex = 10;
int messageIndex = 11;
return Optional.ofNullable(logicalOperator).map(logOp -> {
return logicalOperator.apply(input);
}).orElseGet(() ->
org.burningwave.core.assembler.StaticComponentContainer.Driver.throwException(
"A call to and/or method is necessary before calling {} at {}",
Thread.currentThread().getStackTrace()[10].getMethodName(),
Thread.currentThread().getStackTrace()[11]
Thread.currentThread().getStackTrace()[methodIndex].getMethodName(),
Thread.currentThread().getStackTrace()[messageIndex]
)
);
}
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/org/burningwave/core/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.burningwave.core;

public class StringUtils {

public static String capitalizeFirstCharacter(String value) {
return Character.toString(value.charAt(0)).toUpperCase()
+ value.substring(1, value.length());
}

public static boolean isBlank(String str) {
int strLen;
if ((str == null) || ((strLen = str.length()) == 0)) {
return true;
}
for (int i = 0; i < strLen; ++i) {
if (!(Character.isWhitespace(str.charAt(i)))) {
return false;
}
}
return true;
}

public static boolean isNotBlank(String str) {
return (!(isBlank(str)));
}

public static boolean isEmpty(String str) {
return ((str == null) || (str.length() == 0));
}

public static boolean isNotEmpty(String str) {
return (!(isEmpty(str)));
}

public static boolean contains(String str, char searchChar) {
if (isEmpty(str)) {
return false;
}
return (str.indexOf(searchChar) >= 0);
}

public static boolean areEquals(String string1, String string2) {
return (isEmpty(string1) && isEmpty(string2)) ||
(isNotEmpty(string1) && isNotEmpty(string2) && string1.equals(string2));
}
}
45 changes: 2 additions & 43 deletions src/main/java/org/burningwave/core/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
package org.burningwave.core;

import static org.burningwave.core.StringUtils.isEmpty;
import static org.burningwave.core.assembler.StaticComponentContainer.ManagedLoggerRepository;

import java.net.URLDecoder;
Expand Down Expand Up @@ -58,45 +59,6 @@ public static Strings create() {
return new Strings();
}

public String capitalizeFirstCharacter(String value) {
return Character.toString(value.charAt(0)).toUpperCase()
+ value.substring(1, value.length());
}

public boolean isBlank(String str) {
int strLen;
if ((str == null) || ((strLen = str.length()) == 0)) {
return true;
}
for (int i = 0; i < strLen; ++i) {
if (!(Character.isWhitespace(str.charAt(i)))) {
return false;
}
}
return true;
}

public boolean isNotBlank(String str) {
return (!(isBlank(str)));
}


public boolean isEmpty(String str) {
return ((str == null) || (str.length() == 0));
}

public boolean isNotEmpty(String str) {
return (!(isEmpty(str)));
}


public boolean contains(String str, char searchChar) {
if (isEmpty(str)) {
return false;
}
return (str.indexOf(searchChar) >= 0);
}


public String strip(String str, String stripChars) {
if (isEmpty(str)) {
Expand Down Expand Up @@ -390,10 +352,7 @@ public String removeInitialPathElements(String path, String... toRemove) {
}


public boolean areEquals(String string1, String string2) {
return (isEmpty(string1) && isEmpty(string2)) ||
(isNotEmpty(string1) && isNotEmpty(string2) && string1.equals(string2));
}


public String placeHolderToRegEx(String value) {
return value.replace("$", "\\$").replace(".", "\\.").replace("{", "\\{").replace("}", "\\}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public static AnnotationSourceGenerator create(Class<?> cls) {
return annotation;
}

Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {

@Override
public Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {
Collection<TypeDeclarationSourceGenerator> types = new ArrayList<>();
Optional.ofNullable(body).ifPresent(hirearchyElements -> {
types.addAll(body.getTypeDeclarations());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -99,6 +100,11 @@ public BodySourceGenerator addCode(String... elements) {
public String make() {
return element;
}

@Override
public Collection<TypeDeclarationSourceGenerator> getTypeDeclarations(){
return Collections.emptyList();
}
});
}
return this;
Expand Down Expand Up @@ -127,33 +133,13 @@ public BodySourceGenerator addAllElements(Collection<? extends SourceGenerator>
return this;
}

Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {
@Override
public Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {
Collection<TypeDeclarationSourceGenerator> types = new ArrayList<>();
Optional.ofNullable(usedTypes).ifPresent(usedTypes -> types.addAll(usedTypes));
Optional.ofNullable(bodyGenerators).ifPresent(bodyGenerators -> {
for (SourceGenerator generator : bodyGenerators) {
if (generator instanceof AnnotationSourceGenerator) {
types.addAll(((AnnotationSourceGenerator)generator).getTypeDeclarations());
}
if (generator instanceof BodySourceGenerator) {
types.addAll(((BodySourceGenerator)generator).getTypeDeclarations());
}
if (generator instanceof ClassSourceGenerator) {
types.addAll(((ClassSourceGenerator)generator).getTypeDeclarations());
}
if (generator instanceof FunctionSourceGenerator) {
types.addAll(((FunctionSourceGenerator)generator).getTypeDeclarations());
}
if (generator instanceof GenericSourceGenerator) {
types.addAll(((GenericSourceGenerator)generator).getTypeDeclarations());
}
if (generator instanceof TypeDeclarationSourceGenerator) {
types.addAll(((TypeDeclarationSourceGenerator)generator).getTypeDeclarations());
}
if (generator instanceof VariableSourceGenerator) {
types.addAll(((VariableSourceGenerator)generator).getTypeDeclarations());
}

types.addAll(generator.getTypeDeclarations());
}
});
return types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.burningwave.core.StringUtils;
import org.burningwave.core.io.FileSystemItem;

public interface ClassPathHelper {
Expand Down Expand Up @@ -132,7 +133,7 @@ public static AndAddToClassLoaderConfig create(
if (classRepositories == null) {
throw new IllegalArgumentException("No class repository has been provided");
}
if (Strings.isEmpty(nameOfTheClassToBeLoaded)) {
if (StringUtils.isEmpty(nameOfTheClassToBeLoaded)) {
throw new IllegalArgumentException("No class name to be found has been provided");
}
return new AndAddToClassLoaderConfig(classLoader, classRepositories, nameOfTheClassToBeLoaded);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ private ClassSourceGenerator(String classType, TypeDeclarationSourceGenerator ty
this.typeDeclaration = typeDeclaration;
}

TypeDeclarationSourceGenerator getTypeDeclaration() {

public TypeDeclarationSourceGenerator getTypeDeclaration() {
return typeDeclaration;
}

Expand Down Expand Up @@ -326,7 +327,8 @@ String getOuterCode() {
).orElseGet(() -> null);
}

Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {
@Override
public Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {
Collection<TypeDeclarationSourceGenerator> types = typeDeclaration.getTypeDeclarations();
Optional.ofNullable(staticInitializer).ifPresent(stcInit -> {
types.addAll(stcInit.getTypeDeclarations());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import static org.burningwave.core.assembler.StaticComponentContainer.ClassLoaders;
import static org.burningwave.core.assembler.StaticComponentContainer.Constructors;
import static org.burningwave.core.assembler.StaticComponentContainer.IterableObjectHelper;
import static org.burningwave.core.assembler.StaticComponentContainer.Strings;

import java.util.Collection;
import java.util.Map;
Expand All @@ -41,6 +40,7 @@

import org.burningwave.core.Component;
import org.burningwave.core.Executable;
import org.burningwave.core.StringUtils;
import org.burningwave.core.io.FileSystemItem;
import org.burningwave.core.io.PathHelper;
import org.burningwave.core.iterable.IterableObjectHelper.ResolveConfig;
Expand Down Expand Up @@ -124,9 +124,9 @@ public <E extends ExecuteConfig<E>, T> T execute(ExecuteConfig.ForProperties con
Configuration.Key.PROPERTIES_FILE_SUPPLIER_SIMPLE_NAME_SUFFIX,
Configuration.Key.PROPERTIES_FILE_EXECUTOR_SIMPLE_NAME_SUFFIX
);
if (Strings.isNotEmpty(executorName)) {
if (StringUtils.isNotEmpty(executorName)) {
config.setName(executorName);
} else if (Strings.isNotEmpty(executorSimpleName)) {
} else if (StringUtils.isNotEmpty(executorSimpleName)) {
config.setSimpleName(executorSimpleName);
}
String code = IterableObjectHelper.resolveStringValue(
Expand All @@ -139,7 +139,7 @@ public <E extends ExecuteConfig<E>, T> T execute(ExecuteConfig.ForProperties con
if (config.isIndentCodeActive()) {
code = code.replaceAll(";{2,}", ";");
for (String codeLine : code.split(";")) {
if (Strings.isNotEmpty(codeLine)) {
if (StringUtils.isNotEmpty(codeLine)) {
body.addCodeLine(codeLine + ";");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ Boolean setFieldByDirectAccess(Object target, String pathSegment, Object value)
Field field = Fields.findOneAndMakeItAccessible(target.getClass(), matcher.group(1));
Fields.setDirect(target, field, value);
} else {
if (target.getClass().isArray() || target instanceof Map || target instanceof Collection) {
boolean isArray = target.getClass().isArray();
boolean isMapOrCollection = target instanceof Map || target instanceof Collection;
if ( isArray || isMapOrCollection) {
setInIndexedField(target, matcher.group(2), value);
} else {
Field field = Fields.findOneAndMakeItAccessible(target.getClass(), matcher.group(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ private String getParametersCode() {
}
return paramsCode + ")";
}

Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {
@Override
public Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {
Collection<TypeDeclarationSourceGenerator> types = new ArrayList<>();
Optional.ofNullable(usedTypes).ifPresent(usedTypes -> {
types.addAll(usedTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,44 @@ private Method retrieveMethod(Class<?> targetClass, String methodName, Class<?>.
@Override
public <F> F getOrCreate(Executable executable) {
if (executable instanceof Method) {
Method targetMethod = (Method)executable;
if (targetMethod.getParameterTypes().length == 0 && targetMethod.getReturnType() == void.class) {
return getOrCreateBindedRunnable(targetMethod);
} else if ((targetMethod.getReturnType() == boolean.class || targetMethod.getReturnType() == Boolean.class) &&
(targetMethod.getParameterTypes().length > 0 || (targetMethod.getParameterTypes().length == 0 && !Modifier.isStatic(targetMethod.getModifiers())))
) {
return getOrCreateBindedPredicate(targetMethod);
} else if (targetMethod.getParameterTypes().length == 0 && targetMethod.getReturnType() != void.class) {
return getOrCreateBindedSupplier(targetMethod);
} else if (targetMethod.getParameterTypes().length > 0 && targetMethod.getReturnType() == void.class) {
return getOrCreateBindedConsumer(targetMethod);
} else if (targetMethod.getParameterTypes().length > 0 && targetMethod.getReturnType() != void.class) {
return getOrCreateBindedFunction(targetMethod);
}
return getOrCreateMethod(executable);
} else if (executable instanceof Constructor) {
Constructor<?> targetConstructor = (Constructor<?>)executable;
if (targetConstructor.getParameterTypes().length == 0) {
return getOrCreateBindedSupplier(targetConstructor);
} else {
return getOrCreateBindedFunction(targetConstructor);
}
return getOrCreateConstructor(executable);
}
return null;
}

public <F> F getOrCreateConstructor(Executable executable){
Constructor<?> targetConstructor = (Constructor<?>)executable;

if (targetConstructor.getParameterTypes().length == 0) {
return getOrCreateBindedSupplier(targetConstructor);
}

return getOrCreateBindedFunction(targetConstructor);
}

public <F> F getOrCreateMethod(Executable executable){
Method targetMethod = (Method)executable;

boolean isBoolean = (targetMethod.getReturnType() == boolean.class || targetMethod.getReturnType() == Boolean.class);
boolean hasParameters = targetMethod.getParameterTypes().length > 0;
boolean hasNoParametersAndNoStaticModifiers = targetMethod.getParameterTypes().length == 0 && !Modifier.isStatic(targetMethod.getModifiers());


if (targetMethod.getParameterTypes().length == 0 && targetMethod.getReturnType() == void.class) {
return getOrCreateBindedRunnable(targetMethod);
} else if ( isBoolean && ( hasParameters || (hasNoParametersAndNoStaticModifiers))) {
return getOrCreateBindedPredicate(targetMethod);
} else if (targetMethod.getParameterTypes().length == 0 && targetMethod.getReturnType() != void.class) {
return getOrCreateBindedSupplier(targetMethod);
} else if (targetMethod.getParameterTypes().length > 0 && targetMethod.getReturnType() == void.class) {
return getOrCreateBindedConsumer(targetMethod);
} else if (targetMethod.getParameterTypes().length > 0 && targetMethod.getReturnType() != void.class) {
return getOrCreateBindedFunction(targetMethod);
}
return null;
}
<F> F getOrCreateBindedRunnable(Executable executable) {
return (F) Cache.bindedFunctionalInterfaces.getOrUploadIfAbsent(
Classes.getClassLoader(executable.getDeclaringClass()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public GenericSourceGenerator parentOf(TypeDeclarationSourceGenerator hirearchyE
return this;
}

Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {
@Override
public Collection<TypeDeclarationSourceGenerator> getTypeDeclarations() {
Collection<TypeDeclarationSourceGenerator> types = new ArrayList<>();
Optional.ofNullable(annotations).ifPresent(annotations -> {
for (AnnotationSourceGenerator annotation : annotations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

import org.burningwave.core.Closeable;
import org.burningwave.core.Component;
import org.burningwave.core.StringUtils;
import org.burningwave.core.classes.ClassPathHelper.Compute;
import org.burningwave.core.classes.JavaMemoryCompiler.Compilation.Config;
import org.burningwave.core.concurrent.QueuedTaskExecutor.ProducerTask;
Expand Down Expand Up @@ -340,7 +341,7 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
}
}
}
if (Strings.isNotEmpty(packageName)) {
if (StringUtils.isNotEmpty(packageName)) {
try {
fsObjects = context.findForPackageName(packageName);
} catch (Exception exc) {
Expand Down Expand Up @@ -561,7 +562,7 @@ static Context create(
}

void addToClassPath(String path) {
if (Strings.isNotBlank(path)) {
if (StringUtils.isNotBlank(path)) {
if (blackListedClassPaths.contains(path)) {
ManagedLoggerRepository.logWarn(getClass()::getName, "Could not add {} to class path because it is black listed", path);
return;
Expand Down
Loading