Skip to content

Commit

Permalink
* iterate using iterator, for better performance on non-random-access…
Browse files Browse the repository at this point in the history
… lists.

* added unique check
  • Loading branch information
Milan Mimica committed Oct 19, 2021
1 parent 7f73f85 commit fde6c58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
11 changes: 7 additions & 4 deletions src/main/java/com/gliwka/hyperscan/wrapper/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ private Database(NativeDatabase database, List<Expression> expressions) {

this.expressions = new HashMap<>(expressionCount);
if (hasIds) {
expressions.forEach(expression -> this.expressions.put(expression.getId(), expression));
for (Expression expression : expressions) {
if (this.expressions.put(expression.getId(), expression) != null)
throw new IllegalStateException("Expression ID must be unique within a Database.");
}
} else {
for (int i = 0; i < expressions.size(); i++) {
this.expressions.put(i, expressions.get(i));
int i = 0;
for (Expression expression : expressions) {
this.expressions.put(i++, expression);
}
}
}
Expand Down Expand Up @@ -93,7 +97,6 @@ public static Database compile(List<Expression> expressions) throws CompileError
throw new IllegalStateException("You can't mix expressions with and without id's in a single database");
}


for (int i = 0; i < expressionsSize; i++) {
flags[i] = expressions.get(i).getFlagBits();

Expand Down
22 changes: 10 additions & 12 deletions src/main/java/com/gliwka/hyperscan/wrapper/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
@ToString
public class Expression {
@Getter @NonNull private final String expression;
@Getter private EnumSet<ExpressionFlag> flags = EnumSet.of(ExpressionFlag.NO_FLAG);
@Getter private Integer id;
@Getter private final EnumSet<ExpressionFlag> flags;
@Getter private final Integer id;

/**
* Represents the validation results for a expression
Expand All @@ -37,34 +37,32 @@ public static class ValidationResult {
}

public Expression(@NonNull String expression) {
this.expression = expression;
this(expression, EnumSet.of(ExpressionFlag.NO_FLAG), null);
}

public Expression(@NonNull String expression, Integer id) {
this.expression = expression;
this.id = id;
this(expression, EnumSet.of(ExpressionFlag.NO_FLAG), id);
}

public Expression(@NonNull String expression, @NonNull EnumSet<ExpressionFlag> flags) {
this.expression = expression;
this.flags = flags;
this(expression, flags, null);
}

public Expression(@NonNull String expression, @NonNull ExpressionFlag flag) {
this.expression = expression;
this.flags = EnumSet.of(flag);
this(expression, EnumSet.of(flag), null);
}

public Expression(@NonNull String expression, @NonNull EnumSet<ExpressionFlag> flags, Integer id) {
if (id != null && id < 0)
throw new IllegalArgumentException("id must be >=0: " + id);

this.expression = expression;
this.flags = flags;
this.id = id;
}

public Expression(@NonNull String expression, @NonNull ExpressionFlag flag, Integer id) {
this.expression = expression;
this.flags = EnumSet.of(flag);
this.id = id;
this(expression, EnumSet.of(flag), id);
}

public ValidationResult validate() {
Expand Down

0 comments on commit fde6c58

Please sign in to comment.