JavaValueFilter extends RecipeFilter { @Override public Object apply(Object value, Object... params) { if (params.length != 1) { throw new IllegalArgumentException("must pass a type parameter to " + name); } String strType = super.asString(params[0]); ParamType paramType = CookbookUtils.parseType(strType, getCookbook()); Type type = paramType.getType(); if (paramType.isVararg() || type instanceof ArrayType) { Type baseType = paramType.isVararg() ? type : ((ArrayType) type).getBaseType(); String values = StringUtil.join( ((List<Object>) value) .stream().map(v -> apply(v, baseType.name())).collect(Collectors.toList()), ", "); return "new " + new JavaTypeFilter(getCookbook()).apply(strType, true) + "{" + values + "}"; } else if (type instanceof PrimitiveType) { PrimitiveType primitive = (PrimitiveType) type; switch (primitive.getPrimitive()) { case BOOLEAN: return super.asString(value); case INTEGER: return super.asString(value); case FLOAT: return super.asString(value) + "f"; case STRING: return value == null ? "null" : "\"" + super.asString(value).replace("\"", "\\\"") + "\""; default: throw new IllegalArgumentException("unknown primitive type " + type.name()); } } else if (type instanceof EnumType) { return type.name() + "." + super.asString(value); } return super.asString(value); } JavaValueFilter(Cookbook cookbook); @Override Object apply(Object value, Object... params);  }
@Test(expected = IllegalArgumentException.class) public void testApply_throwsOnMissingTypeParam() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); filter.apply("foobar"); }
@Test public void testApply_convertsString() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("\"foobar\"", filter.apply("foobar", "string")); }
@Test public void testApply_convertsStringWithQuotes() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("\"\\\"foobar\"", filter.apply("\"foobar", "string")); }
@Test public void testApply_convertsNullString() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("null", filter.apply(null, "string")); }
@Test public void testApply_convertsInt() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("5", filter.apply(new Integer(5), "int")); assertEquals("5", filter.apply(5, "int")); }
@Test public void testApply_convertsNegativeInt() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("-1", filter.apply(new Integer(-1), "int")); assertEquals("-1", filter.apply(-1, "int")); }
@Test public void testApply_convertsFloat() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("5.0f", filter.apply(new Float(5.0), "float")); assertEquals("5.0f", filter.apply(5.0f, "float")); }
@Test public void testApply_convertsNegativeFloat() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("-5.0f", filter.apply(new Float(-5.0), "float")); assertEquals("-5.0f", filter.apply(-5.0f, "float")); }
@Test public void testApply_convertsBoolean() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("true", filter.apply(new Boolean(true), "boolean")); assertEquals("false", filter.apply(false, "boolean")); }
@Test public void testApply_convertsFlag() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("true", filter.apply(new Boolean(true), "flag")); assertEquals("false", filter.apply(false, "flag")); }
@Test public void testApply_convertsEnum() { Cookbook cookbook = new Cookbook( "test", new ArrayList<>(), Lists.newArrayList(new Enum("MyEnum", Lists.newArrayList("A")))); Filter filter = new JavaValueFilter(cookbook); assertEquals("MyEnum.A", filter.apply("A", "MyEnum")); }
@Test public void testApply_convertsArrayOfStrings() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals( "new String[]{\"a\", \"b\", \"c\"}", filter.apply(Lists.newArrayList("a", "b", "c"), "string[]")); }
@Test public void testApply_convertsArrayOfInts() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals("new int[]{1, 2, 3}", filter.apply(Lists.newArrayList(1, 2, 3), "int[]")); }
@Test public void testApply_convertsArrayOfEnums() { Cookbook cookbook = new Cookbook( "test", new ArrayList<>(), Lists.newArrayList(new Enum("MyEnum", Lists.newArrayList("A", "B", "C")))); Filter filter = new JavaValueFilter(cookbook); assertEquals( "new MyEnum[]{MyEnum.A, MyEnum.B, MyEnum.C}", filter.apply(Lists.newArrayList("A", "B", "C"), "MyEnum[]")); }
@Test public void testApply_convertsVarargOfStrings() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals( "new String[]{\"a\", \"b\", \"c\"}", filter.apply(Lists.newArrayList("a", "b", "c"), "string...")); }
@Test public void testApply_convertsVarargOfArraysOfStrings() { Cookbook cookbook = new Cookbook("test", new ArrayList<>(), new ArrayList<>()); Filter filter = new JavaValueFilter(cookbook); assertEquals( "new String[][]{new String[]{\"a\"}, new String[]{\"b\", \"c\"}}", filter.apply( Lists.newArrayList(Lists.newArrayList("a"), Lists.newArrayList("b", "c")), "string[]...")); }