Skip to content

Commit

Permalink
#36. Generate Not Matching String
Browse files Browse the repository at this point in the history
Refactoring.
Logging really is not needed clean up to make library smaller.
  • Loading branch information
curious-odd-man committed Aug 25, 2020
1 parent aa55980 commit c87b6e1
Show file tree
Hide file tree
Showing 19 changed files with 16 additions and 138 deletions.
27 changes: 0 additions & 27 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
<!-- Dependencies versions -->
<junit.version>4.12</junit.version>
<jmh.version>1.21</jmh.version>
<slf4j.version>1.7.29</slf4j.version>
<log4j.version>2.13.3</log4j.version>

<!-- Plugins versions -->
<compiler.plugin.version>3.8.1</compiler.plugin.version>
Expand Down Expand Up @@ -257,38 +255,13 @@
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<!-- Test scope below -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,14 @@
/* **************************************************************************/

import com.github.curiousoddman.rgxgen.generator.visitors.NodeVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class Choice implements Node {

private static final Logger LOGGER = LoggerFactory.getLogger(Choice.class);

private final Node[] aNodes;

public Choice(Node... nodes) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Creating from {} ", Arrays.asList(nodes));
}
aNodes = nodes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@
/* **************************************************************************/

import com.github.curiousoddman.rgxgen.generator.visitors.NodeVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FinalSymbol implements Node {

private static final Logger LOGGER = LoggerFactory.getLogger(FinalSymbol.class);

private final String aValue;

public FinalSymbol(String value) {
LOGGER.trace("Creating '{}'", value);
aValue = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@
/* **************************************************************************/

import com.github.curiousoddman.rgxgen.generator.visitors.NodeVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Group implements Node {
private static final Logger LOGGER = LoggerFactory.getLogger(Group.class);

private final Node aNode;
private final int aGroupIndex;

public Group(int index, Node node) {
LOGGER.trace("Crating idx = '{}' from '{}'", index, node);
aNode = node;
aGroupIndex = index;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@
/* **************************************************************************/

import com.github.curiousoddman.rgxgen.generator.visitors.NodeVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GroupRef implements Node {

private static final Logger LOGGER = LoggerFactory.getLogger(GroupRef.class);

private final int aIndex;

public GroupRef(int index) {
LOGGER.trace("Crating idx = '{}'", index);
aIndex = index;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@
/* **************************************************************************/

import com.github.curiousoddman.rgxgen.generator.visitors.NodeVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Pattern;

public class NotSymbol implements Node {

private static final Logger LOGGER = LoggerFactory.getLogger(NotSymbol.class);

private final Pattern aSubPattern;

public NotSymbol(String pattern) {
LOGGER.trace("Crating '{}'", pattern);
aSubPattern = Pattern.compile(pattern);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@
/* **************************************************************************/

import com.github.curiousoddman.rgxgen.generator.visitors.NodeVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Repeat implements Node {

private static final Logger LOGGER = LoggerFactory.getLogger(Repeat.class);

private final Node aNode;
private final int aMin;
private final int aMax;
Expand All @@ -37,7 +32,6 @@ public Repeat(Node node, int times) {
}

public Repeat(Node node, int min, int max) {
LOGGER.trace("Crating ({} to {}) '{}'", min, max, node);
aNode = node;
aMin = min;
aMax = max;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@
/* **************************************************************************/

import com.github.curiousoddman.rgxgen.generator.visitors.NodeVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class Sequence implements Node {

private static final Logger LOGGER = LoggerFactory.getLogger(Sequence.class);

private final Node[] aNodes;

public Sequence(Node... nodes) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Creating from {} ", Arrays.asList(nodes));
}
aNodes = nodes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import com.github.curiousoddman.rgxgen.generator.visitors.NodeVisitor;
import com.github.curiousoddman.rgxgen.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -29,9 +27,6 @@
* Generate Any printable character.
*/
public class SymbolSet implements Node {

private static final Logger LOGGER = LoggerFactory.getLogger(SymbolSet.class);

private static final String[] ALL_SYMBOLS = new String[127 - 32];

public static String[] getAllSymbols() {
Expand All @@ -46,10 +41,6 @@ public static String[] getAllSymbols() {
ALL_SYMBOLS[i - SPACE_ASCII_CODE] = Character.valueOf((char) i)
.toString();
}

if (LOGGER.isTraceEnabled()) {
LOGGER.trace("All symbols {} ", Arrays.asList(ALL_SYMBOLS));
}
}

/**
Expand Down Expand Up @@ -114,7 +105,7 @@ public SymbolSet(String[] symbols, TYPE type) {
this(Collections.emptyList(), symbols, type);
}

public SymbolSet(List<SymbolRange> symbolRanges, TYPE type) {
public SymbolSet(Collection<SymbolRange> symbolRanges, TYPE type) {
this(symbolRanges, Util.ZERO_LENGTH_STRING_ARRAY, type);
}

Expand All @@ -125,11 +116,7 @@ public SymbolSet(List<SymbolRange> symbolRanges, TYPE type) {
* @param symbols symbols to include/exclude
* @param type POSITIVE - include, NEGATIVE - exclude
*/
public SymbolSet(List<SymbolRange> symbolRanges, String[] symbols, TYPE type) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Creating [positive = {}] from {} and {}", type, symbolRanges, Arrays.asList(symbols));
}

public SymbolSet(Collection<SymbolRange> symbolRanges, String[] symbols, TYPE type) {
List<String> initial = type == TYPE.NEGATIVE
? new ArrayList<>(Arrays.asList(ALL_SYMBOLS)) // First we need to add all, later we remove unnecessary
: new ArrayList<>(ALL_SYMBOLS.length); // Most probably it will be enough.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.github.curiousoddman.rgxgen.iterators.ReferenceIterator;
import com.github.curiousoddman.rgxgen.iterators.StringIterator;
import com.github.curiousoddman.rgxgen.iterators.suppliers.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -30,9 +28,6 @@
import java.util.function.Supplier;

public class UniqueGenerationVisitor implements NodeVisitor {

private static final Logger LOGGER = LoggerFactory.getLogger(UniqueGenerationVisitor.class);

private final List<Supplier<StringIterator>> aIterators = new ArrayList<>();
private final Map<Integer, List<ReferenceIterator>> aReferenceIteratorMap;
private final Map<Integer, StringIterator> aGroupIterators;
Expand All @@ -42,7 +37,6 @@ public UniqueGenerationVisitor() {
}

public UniqueGenerationVisitor(Map<Integer, List<ReferenceIterator>> referenceIteratorMap, Map<Integer, StringIterator> groupIterators) {
LOGGER.trace("Creating\n\trefs: {}\n\tgrps: {}", referenceIteratorMap, groupIterators);
aReferenceIteratorMap = referenceIteratorMap;
aGroupIterators = groupIterators;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
limitations under the License.
/* **************************************************************************/

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReferenceIterator extends StringIterator {

private static final Logger LOGGER = LoggerFactory.getLogger(ReferenceIterator.class);

private StringIterator aOther;
private boolean hasNext = true;
private String aLast;
Expand Down Expand Up @@ -50,7 +44,6 @@ public void reset() {

@Override
public boolean hasNext() {
LOGGER.trace("hasNext = {}, aOther.current() = {}, aLast = {}", hasNext, aOther.current(), aLast);
return hasNext || !aOther.current()
.equals(aLast);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@
limitations under the License.
/* **************************************************************************/

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;

public abstract class StringIterator implements Iterator<String> {
private static final Logger LOGGER = LoggerFactory.getLogger(StringIterator.class);

@SuppressWarnings("IteratorNextCanNotThrowNoSuchElementException")
@Override
public String next() {
String current = nextImpl();
LOGGER.trace("Produced value: '{}' using '{}'", current, this);
return current;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,13 @@

import com.github.curiousoddman.rgxgen.iterators.ReferenceIterator;
import com.github.curiousoddman.rgxgen.iterators.StringIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class GroupIteratorSupplier implements Supplier<StringIterator> {

private static final Logger LOGGER = LoggerFactory.getLogger(GroupIteratorSupplier.class);

private final Supplier<StringIterator> aIteratorSupplier;
private final Map<Integer, List<ReferenceIterator>> aReferenceIteratorMap;
private final Map<Integer, StringIterator> aGroupIteratorsMap;
Expand All @@ -40,18 +35,14 @@ public GroupIteratorSupplier(Supplier<StringIterator> iteratorSupplier, Map<Inte
aReferenceIteratorMap = referenceIteratorMap;
aGroupIteratorsMap = groupIteratorsMap;
aIndex = index;
LOGGER.trace("Creating idx {}\n\tsrc: {}\n\trefs: {}\n\tgrps: {}", index, iteratorSupplier, referenceIteratorMap, groupIteratorsMap);
}

@Override
public StringIterator get() {
LOGGER.trace("Getting idx {}\n\trefs: {}\n\tgrps: {}", aIndex, aReferenceIteratorMap, aGroupIteratorsMap);
final StringIterator stringIterator = aIteratorSupplier.get();
StringIterator stringIterator = aIteratorSupplier.get();
aGroupIteratorsMap.put(aIndex, stringIterator);
final List<ReferenceIterator> orDefault = aReferenceIteratorMap.getOrDefault(aIndex, Collections.emptyList());
LOGGER.debug("ReferenceIterators to connect: {}", orDefault);
List<ReferenceIterator> orDefault = aReferenceIteratorMap.getOrDefault(aIndex, Collections.emptyList());
for (ReferenceIterator referenceIterator : orDefault) {
LOGGER.debug("GroupRef[{}] connecting to group {} ", aIndex, stringIterator);
referenceIterator.setOther(stringIterator);
}
return stringIterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,13 @@

import com.github.curiousoddman.rgxgen.iterators.ReferenceIterator;
import com.github.curiousoddman.rgxgen.iterators.StringIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class ReferenceIteratorSupplier implements Supplier<StringIterator> {

private static final Logger LOGGER = LoggerFactory.getLogger(ReferenceIteratorSupplier.class);

private final Map<Integer, List<ReferenceIterator>> aReferenceIteratorMap;
private final Map<Integer, StringIterator> aGroupIteratorsMap;
private final int aIndex;
Expand All @@ -38,20 +33,16 @@ public ReferenceIteratorSupplier(Map<Integer, List<ReferenceIterator>> reference
aReferenceIteratorMap = referenceIteratorMap;
aGroupIteratorsMap = groupIteratorsMap;
aIndex = index;
LOGGER.trace("Creating idx {}\n\trefs: {}\n\tgrps: {}", index, referenceIteratorMap, groupIteratorsMap);
}

@Override
public StringIterator get() {
LOGGER.trace("Getting idx {}\n\trefs: {}\n\tgrps: {}", aIndex, aReferenceIteratorMap, aGroupIteratorsMap);
ReferenceIterator referenceIterator = new ReferenceIterator();
final StringIterator stringIterator = aGroupIteratorsMap.get(aIndex);
if (stringIterator != null) {
LOGGER.debug("GroupRef[{}] connecting to group {} ", aIndex, stringIterator);
referenceIterator.setOther(stringIterator);
}

LOGGER.debug("GroupRef[{}] adding to connection queue group ", aIndex);
aReferenceIteratorMap.computeIfAbsent(aIndex, i -> new ArrayList<>())
.add(referenceIterator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private GroupType processGroupType() {

case "?<":
GroupType res = GroupType.POSITIVE_LOOKBEHIND;
final char next = aCharIterator.next();
char next = aCharIterator.next();
if (next == '!') {
res = GroupType.NEGATIVE_LOOKBEHIND;
} else if (next != '=') { // Positive Lookbehind does not affect generation.
Expand Down
Loading

0 comments on commit c87b6e1

Please sign in to comment.