Skip to content

Commit

Permalink
Code Cleanup (ctco#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorGursky authored Jul 14, 2020
1 parent cf9a880 commit e301222
Show file tree
Hide file tree
Showing 77 changed files with 858 additions and 230 deletions.
2 changes: 1 addition & 1 deletion cukes-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>lv.ctco.cukes</groupId>
<artifactId>cukes</artifactId>
<version>1.0.6-SNAPSHOT</version>
<version>1.0.8-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ public class RandomGeneratorFacadeImpl implements RandomGeneratorFacade {
private static final int CAPITAL_CHAR_BOUNDS = 'Z' - 'A';
private static final int CHAR_BOUNDS = 'z' - 'a';
private static final int NUMBER_BOUNDS = '9' - '0';
private Map<Character, Supplier<Integer>> randomGenerators = new HashMap<>();
private SecureRandom random = new SecureRandom();
private final Map<Character, Supplier<Integer>> randomGenerators = new HashMap<>();
private final SecureRandom random = new SecureRandom();

public RandomGeneratorFacadeImpl() {
randomGenerators.put('A', () -> 'A' + random.nextInt(CAPITAL_CHAR_BOUNDS));
randomGenerators.put('a', () -> 'a' + random.nextInt(CHAR_BOUNDS));
randomGenerators.put('0', () -> '0' + random.nextInt(NUMBER_BOUNDS));
}


@Override
public String byPattern(String pattern) {
if (StringUtils.isEmpty(pattern))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void beforeAllTests() {
for (CukesPlugin cukesPlugin : pluginSet) {
cukesPlugin.beforeAllTests();
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> afterAllTests()));
Runtime.getRuntime().addShutdownHook(new Thread(this::afterAllTests));
}

public void beforeScenario() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

public abstract class BaseContextHandler {

public static final String GROUP_PATTERN = "\\{\\(([\\w\\.-]+)\\)\\}";
public static final String GROUP_PATTERN = "\\{\\(([\\w.-]+)\\)}";

protected List<String> extractGroups(String pattern) {
List<String> allMatches = new ArrayList<String>();
List<String> allMatches = new ArrayList<>();
if (pattern == null) {
return allMatches;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package lv.ctco.cukes.core.internal.context;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import lv.ctco.cukes.core.CukesOptions;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public class ContextInflater extends BaseContextHandler {
Expand All @@ -13,7 +13,7 @@ public class ContextInflater extends BaseContextHandler {
GlobalWorldFacade world;

public String inflate(String input) {
Set<String> groups = new HashSet<String>(extractGroups(input));
Set<String> groups = new HashSet<>(extractGroups(input));
boolean inflatingEnabled = world.getBoolean(CukesOptions.CONTEXT_INFLATING_ENABLED, true);
if (inflatingEnabled) {
return inflateGroups(input, groups);
Expand All @@ -26,7 +26,7 @@ String inflateGroups(String input, Set<String> groups) {
for (String key : groups) {
Optional<String> $value = world.get(key);
if ($value.isPresent()) {
result = result.replaceAll("\\{\\(" + key + "\\)\\}", $value.get());
result = result.replaceAll("\\{\\(" + key + "\\)}", $value.get());
}
}
return result;
Expand All @@ -35,7 +35,7 @@ String inflateGroups(String input, Set<String> groups) {
String inflateGroupsWithPlaceholders(String input, Set<String> groups) {
String result = input;
for (String key : groups) {
result = result.replaceAll("\\{\\(" + key + "\\)\\}", "{" + key + "}");
result = result.replaceAll("\\{\\(" + key + "\\)}", "{" + key + "}");
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import lv.ctco.cukes.core.CukesRuntimeException;

public class CukesMissingPropertyException extends CukesRuntimeException {

public CukesMissingPropertyException(String key) {
super("Property cukes." + key + " is missing");
}

public CukesMissingPropertyException(String key, String additionalInfo) {
super("Property cukes." + key + " is missing: " + additionalInfo);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package lv.ctco.cukes.core.internal.context;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import lv.ctco.cukes.core.CukesRuntimeException;

import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -65,7 +65,7 @@ public void put(@CaptureContext.Pattern String key, @CaptureContext.Value String
}

public Optional<String> get(String key) {
return Optional.fromNullable(context.get(key));
return Optional.ofNullable(context.get(key));
}

public Set<String> keys() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package lv.ctco.cukes.core.internal.context;

import com.google.common.base.Optional;
import com.google.common.collect.Sets;
import com.google.inject.Inject;

import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class GlobalWorldFacade {

Expand All @@ -17,11 +18,9 @@ public void put(@CaptureContext.Pattern String key, @CaptureContext.Value String
}

public String getOrThrow(String key) {
return world.get(key).or(
() -> {
throw new CukesMissingPropertyException(key);
}
);
return world.get(key).orElseGet(() -> {
throw new CukesMissingPropertyException(key);
});
}

public Optional<String> get(String key) {
Expand All @@ -33,14 +32,12 @@ public boolean getBoolean(String key) {
}

public boolean getBoolean(String key, boolean defaultValue) {
return Boolean.valueOf(get(key, Boolean.toString(defaultValue)));
return Boolean.parseBoolean(get(key, Boolean.toString(defaultValue)));
}

public String get(String key, String defaultValue) {
Optional<String> value = world.get(key);
return value.isPresent()
? value.get()
: defaultValue;
return value.orElse(defaultValue);
}

public void reconstruct() {
Expand All @@ -49,7 +46,10 @@ public void reconstruct() {

public Set<String> getKeysStartingWith(final String headerPrefix) {
Set<String> keys = world.keys();
return Sets.filter(keys, s -> s.startsWith(headerPrefix));
return keys.stream()
.filter(Objects::nonNull)
.filter(s -> s.startsWith(headerPrefix))
.collect(Collectors.toSet());
}

public void remove(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public static List<String> readLines(File file, Charset encoding) throws IOExcep
public static FileInputStream openInputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File \'" + file + "\' exists but is a directory");
throw new IOException("File '" + file + "' exists but is a directory");
} else if (!file.canRead()) {
throw new IOException("File \'" + file + "\' cannot be read");
throw new IOException("File '" + file + "' cannot be read");
} else {
return new FileInputStream(file);
}
} else {
throw new FileNotFoundException("File \'" + file + "\' does not exist");
throw new FileNotFoundException("File '" + file + "' does not exist");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void closeQuietly(InputStream closeable) {
public static List<String> readLines(InputStream inputStream, Charset encoding) throws IOException {
InputStreamReader input = new InputStreamReader(inputStream, encoding == null ? Charset.defaultCharset() : encoding);
BufferedReader reader = toBufferedReader(input);
List<String> list = new ArrayList<String>();
List<String> list = new ArrayList<>();

for (String line = reader.readLine(); line != null; line = reader.readLine()) {
list.add(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,6 @@

public class Strings {

public static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr) {
if (str != null && searchStr != null) {
int len = searchStr.length();
int max = str.length() - len;

for (int i = 0; i <= max; ++i) {
if (regionMatches(str, true, i, searchStr, 0, len)) {
return true;
}
}

return false;
} else {
return false;
}
}

static boolean regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length) {
if (cs instanceof String && substring instanceof String) {
return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
} else {
int index1 = thisStart;
int index2 = start;
int tmpLen = length;

while (tmpLen-- > 0) {
char c1 = cs.charAt(index1++);
char c2 = substring.charAt(index2++);
if (c1 != c2) {
if (!ignoreCase) {
return false;
}

if (Character.toUpperCase(c1) != Character.toUpperCase(c2) && Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
return false;
}
}
}

return true;
}
}

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

public static String escapeRegex(String regex) {
return regex.replace("{", "\\{").replace("}", "\\}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public boolean matches(Object item) {
list = (List<?>) item;
} else if (item instanceof NodeChildrenImpl) { // If XML
List<Node> nodes = ((NodeChildrenImpl) item).list();
List<String> result = new ArrayList<String>(nodes.size());
List<String> result = new ArrayList<>(nodes.size());
for (Node node : nodes) {
result.add(node.value());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ public boolean matches(Object o) {
boolean isString = o instanceof String;
CharSequence item = (isString) ? (CharSequence) o : String.valueOf(o);
if (match) {
if (p.matcher(item).matches()) return true;
return p.matcher(item).matches();
} else {
if (p.matcher(item).find()) return true;
return p.matcher(item).find();
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
}

private Annotation[] getCurrentAndSuperclassAnnotations(Class<?> clazz) {
List<Annotation> annotations = new ArrayList<Annotation>(Arrays.asList(clazz.getAnnotations()));
List<Annotation> annotations = new ArrayList<>(Arrays.asList(clazz.getAnnotations()));
for (Class superclass = clazz.getSuperclass(); superclass != null; superclass = superclass.getSuperclass()) {
annotations.addAll(Arrays.asList(superclass.getAnnotations()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lv.ctco.cukes.core.internal.templating;

import com.google.common.base.Optional;
import java.util.Optional;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import lv.ctco.cukes.core.CukesOptions;
Expand All @@ -24,7 +24,7 @@ private Boolean isBodyTemplatesEnabled() {

public String processBody(String body) {
if (isBodyTemplatesEnabled()) {
final Map<String, String> rythmParams = new HashMap<String, String>();
final Map<String, String> rythmParams = new HashMap<>();
for (String key : world.keys()) {
Optional<String> value = world.get(key);
if (value.isPresent() && body.contains("@" + key)) rythmParams.put(key, value.get());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package lv.ctco.cukes.core.internal.context;

import com.google.common.base.Optional;
import com.google.common.collect.Sets;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -9,6 +8,8 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Mockito.anyString;
Expand All @@ -25,7 +26,7 @@ public class ContextInflaterTest {

@Before
public void setUp() {
doReturn(Optional.absent()).when(world).get(anyString());
doReturn(Optional.empty()).when(world).get(anyString());
}

@Test
Expand Down
7 changes: 3 additions & 4 deletions cukes-documentation-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>cukes</artifactId>
<groupId>lv.ctco.cukes</groupId>
<version>1.0.6-SNAPSHOT</version>
<version>1.0.8-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -48,7 +48,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<version>3.0.0</version>
<executions>
<execution>
<id>create-docs</id>
Expand Down Expand Up @@ -78,7 +78,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.5</version>
<version>1.11.2</version>
<executions>
<execution>
<id>add-docs-to-git</id>
Expand All @@ -89,7 +89,6 @@
<configuration>
<includes>docs/**/*.md</includes>
<pushChanges>false</pushChanges>
<message>Updating documentation</message>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

public enum CukesComponent {


core("General", "lv.ctco.cukes.core", "cukes-core"),
http("HTTP", "lv.ctco.cukes.http.api", "cukes-http"),
graphQL("GraphQL", "lv.ctco.cukes.graphql", "cukes-graphql"),
Expand All @@ -17,9 +16,9 @@ public enum CukesComponent {
public static final Comparator<CukesComponent> comparator = Comparator.comparing(CukesComponent::ordinal);
public static final Comparator<Map.Entry<CukesComponent, ?>> mapKeyComparator = (o1, o2) -> comparator.compare(o1.getKey(), o2.getKey());

private String name;
private String basePackage;
private String moduleName;
private final String name;
private final String basePackage;
private final String moduleName;

CukesComponent(String name, String basePackage, String moduleName) {
this.name = name;
Expand Down
Loading

0 comments on commit e301222

Please sign in to comment.