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

Range resolver #28

Open
wants to merge 2 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
36 changes: 27 additions & 9 deletions impl/src/main/antlr4/imports/ConfigBnf.g4
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,28 @@ collect_url_line returns [ NameBoundResolver s ]
}
: ( s_tk=tuple_key { keys = Collections.singletonList($s_tk.s); }
EQ_KW
SQBRACE_OPEN_LIT
s0=tuple_value{ tuples.add(new ResolverTuple($s0.s)); }
(COMMA_LIT sN=tuple_value{ tuples.add(new ResolverTuple($sN.s)); })*
SQBRACE_CLOSE_LIT
(
SQBRACE_OPEN_LIT
s0=tuple_value{ tuples.add(new ResolverTuple($s0.s)); }
(COMMA_LIT sN=tuple_value{ tuples.add(new ResolverTuple($sN.s)); })*
SQBRACE_CLOSE_LIT
{
$s = new SimpleBoundNameResolver(
new SimpleBoundNameResolver.Names(keys),
new ConstResolver(tuples));
}
| range_begin=int_val DOT_DOT_LIT range_end=int_val
{
if ($range_begin.s < Integer.MIN_VALUE ||
$range_end.s < Integer.MIN_VALUE ||
$range_begin.s > Integer.MAX_VALUE ||
$range_end.s > Integer.MAX_VALUE)
throw new IllegalArgumentException("integer out of range");
$s = new SimpleBoundNameResolver(
new SimpleBoundNameResolver.Names(keys),
new RangeResolver((int)$range_begin.s, (int)$range_end.s));
}
)
| keys=collect_url_line_key_tuple
{ keys = $keys.s; }
EQ_KW
Expand All @@ -486,12 +504,12 @@ collect_url_line returns [ NameBoundResolver s ]
{ tuples.add(new ResolverTuple($values.s)); }
)*
SQBRACE_CLOSE_LIT
{
$s = new SimpleBoundNameResolver(
new SimpleBoundNameResolver.Names(keys),
new ConstResolver(tuples));
}
)
{
$s = new SimpleBoundNameResolver(
new SimpleBoundNameResolver.Names(keys),
new ConstResolver(tuples));
}
;
collect_url_line_key_tuple returns [ List<Any2<Integer, String>> s = new ArrayList<>() ]
: BRACE_OPEN_LIT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,50 @@
import static com.groupon.lex.metrics.resolver.ResolverTuple.newTupleElement;
import static java.lang.Integer.max;
import java.util.ArrayList;
import java.util.Collection;
import static java.util.Collections.unmodifiableList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
* A resolver that emits a range of numbers.
*
* Bounds are inclusive.
*/
@Getter
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class RangeResolver implements Resolver {
private final int begin, end;
private final List<ResolverTuple> tuples;

public RangeResolver(int end) {
this(0, end);
public RangeResolver(int begin, int end) {
this.begin = begin;
this.end = end;
this.tuples = createTuples(begin, end);
}

@Override
public int getTupleWidth() {
return 1;
}

public int getTupleWidth() { return 1; }
@Override
public Collection<ResolverTuple> getTuples() {
List<ResolverTuple> tuples = new ArrayList<>(max(end - begin, 0));
public String configString() { return begin + ".." + end; }

private static List<ResolverTuple> createTuples(int begin, int end) {
final ArrayList<ResolverTuple> tuples = new ArrayList<>(max(end - begin + 1, 0));

/*
* We need a for-loop: for (int i = begin; i <= end; ++i) { ... }
* But this won't work if end == Integer.MAX_VALUE, since i can never
* exceed Integer.MAX_VALUE (wrapping around to Integer.MIN_VALUE
* instead).
* So the equals-case is handled separately in an if-statement below.
*/
for (int i = begin; i < end; ++i)
tuples.add(new ResolverTuple(newTupleElement(i)));
return tuples;
}
if (end >= begin)
tuples.add(new ResolverTuple(newTupleElement(end)));

@Override
public String configString() {
StringBuilder buf = new StringBuilder()
.append("range(");
if (begin != 0) {
buf
.append(begin)
.append(", ");
}
return buf
.append(end)
.append(')')
.toString();
return unmodifiableList(tuples);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2016, Groupon, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of GROUPON nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.groupon.lex.metrics.resolver;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class RangeResolverTest {
@Test
public void values() {
RangeResolver range = new RangeResolver(-1, 1);

assertEquals(1, range.getTupleWidth());
assertEquals(-1, range.getBegin());
assertEquals(1, range.getEnd());
assertThat(range.getTuples(),
containsInAnyOrder(
new ResolverTuple(ResolverTuple.newTupleElement(-1)),
new ResolverTuple(ResolverTuple.newTupleElement(0)),
new ResolverTuple(ResolverTuple.newTupleElement(1))));
assertEquals("-1..1", range.configString());
}

@Test
public void negativeRange() {
RangeResolver range = new RangeResolver(1, -1);

assertEquals(1, range.getTupleWidth());
assertEquals(1, range.getBegin());
assertEquals(-1, range.getEnd());
assertTrue(range.getTuples().isEmpty());
assertEquals("1..-1", range.configString());
}

@Test
public void singleElementRange() {
RangeResolver range = new RangeResolver(17, 17);

assertEquals(1, range.getTupleWidth());
assertEquals(17, range.getBegin());
assertEquals(17, range.getEnd());
assertThat(range.getTuples(),
containsInAnyOrder(
new ResolverTuple(ResolverTuple.newTupleElement(17))));
assertEquals("17..17", range.configString());
}

@Test
public void intMinValue() {
RangeResolver range = new RangeResolver(Integer.MIN_VALUE, Integer.MIN_VALUE);

assertEquals(1, range.getTupleWidth());
assertEquals(Integer.MIN_VALUE, range.getBegin());
assertEquals(Integer.MIN_VALUE, range.getEnd());
assertThat(range.getTuples(),
containsInAnyOrder(
new ResolverTuple(ResolverTuple.newTupleElement(Integer.MIN_VALUE))));
assertEquals(Integer.MIN_VALUE + ".." + Integer.MIN_VALUE, range.configString());
}

@Test
public void intMaxValue() {
RangeResolver range = new RangeResolver(Integer.MAX_VALUE, Integer.MAX_VALUE);

assertEquals(1, range.getTupleWidth());
assertEquals(Integer.MAX_VALUE, range.getBegin());
assertEquals(Integer.MAX_VALUE, range.getEnd());
assertThat(range.getTuples(),
containsInAnyOrder(
new ResolverTuple(ResolverTuple.newTupleElement(Integer.MAX_VALUE))));
assertEquals(Integer.MAX_VALUE + ".." + Integer.MAX_VALUE, range.configString());
}
}