Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-neck/8aeaf58baeea47dadd89 to your computer and use it in GitHub Desktop.
Save mike-neck/8aeaf58baeea47dadd89 to your computer and use it in GitHub Desktop.
型引数からnewしたい時のおすすめの方法をためしてみた see http://mike-neck.hatenadiary.com/entry/2015/03/11/151938
/*
* Copyright 2015Shinya Mochida
*
* Licensed under the Apache License,Version2.0(the"License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,software
* Distributed under the License is distributed on an"AS IS"BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.IntFunction;
public interface Generator<OBJ extends InputAndExpected<IN, EX>, IN, EX> {
@Contract("_, _ -> !null")
public static <O extends InputAndExpected<I, E>, I, E>
Generator<O, I, E> provider(
@NotNull BiFunction<I, E, O> fun, @NotNull IntFunction<O[]> af) {
Objects.requireNonNull(fun);
Objects.requireNonNull(af);
return new GeneratorImpl<>(fun, af);
}
Expecting<OBJ, IN, EX> when(IN in);
OBJ[] toArray();
@FunctionalInterface
public static interface Expecting<O extends InputAndExpected<I, E>, I, E> {
Generator<O, I, E> then(E ex);
}
}
/*
* Copyright 2015Shinya Mochida
*
* Licensed under the Apache License,Version2.0(the"License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,software
* Distributed under the License is distributed on an"AS IS"BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.IntFunction;
public class GeneratorImpl<OBJ extends InputAndExpected<IN, EX>, IN, EX>
implements Generator<OBJ, IN, EX> {
private final BiFunction<IN, EX, OBJ> fun;
private final IntFunction<OBJ[]> af;
private final List<Triple<OBJ, IN, EX>> list = new ArrayList<>();
GeneratorImpl(BiFunction<IN, EX, OBJ> fun, IntFunction<OBJ[]> af) {
this.fun = fun;
this.af = af;
}
@Override
public Expecting<OBJ, IN, EX> when(IN in) {
return ex -> {
list.add(new Triple<>(fun, in, ex));
return GeneratorImpl.this;
};
}
@Override
public OBJ[] toArray() {
return list.stream().map(Triple::toObject).toArray(af);
}
private static class Triple<O extends InputAndExpected<I, E>, I, E> {
private final I in;
private final E ex;
private final BiFunction<I, E, O> fun;
private Triple(BiFunction<I, E, O> fun, I in, E ex) {
this.fun = fun;
this.in = in;
this.ex = ex;
}
private O toObject() {
return fun.apply(in, ex);
}
}
}
/*
* Copyright 2015Shinya Mochida
*
* Licensed under the Apache License,Version2.0(the"License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,software
* Distributed under the License is distributed on an"AS IS"BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.Function;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public abstract class InputAndExpected<INPUT, EXPECTED> {
private final INPUT input;
private final EXPECTED expected;
protected InputAndExpected(INPUT input, EXPECTED expected) {
this.input = input;
this.expected = expected;
}
public INPUT getInput() {
return input;
}
public EXPECTED getExpected() {
return expected;
}
}
/*
* Copyright 2015Shinya Mochida
*
* Licensed under the Apache License,Version2.0(the"License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,software
* Distributed under the License is distributed on an"AS IS"BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(Theories.class)
public static class SampleTest {
@DataPoints
public static InputAndExpected<String, Integer>[] TEST_DATA =
Generator.provider(TestData::new, TestData[]::new)
.when("hoge").then(4)
.when("foo").then(3)
.when("bar").then(3)
.toArray();
@Theory
public void test(InputAndExpected<String, Integer> data) {
assertThat(data.getInput().length(), is(data.getExpected()));
}
private static class TestData extends InputAndExpected<String, Integer> {
protected TestData(String s, Integer i) {
super(s, i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment