@SuppressWarnings("unchecked") protected <A> Class<? extends A> validate(String property, Class<A> propertyType) { Class<A> validatedType = (Class<A>) validator.validate(getType(), property, propertyType); if (validatedType != null) { return validatedType; } else { throw new IllegalArgumentException("Illegal property " + property); } }
@Test public void calling_get_with_the_same_name_and_different_types_returns_specific_type_when_validating() { PathBuilder<User> entity = new PathBuilder<User>(User.class, "entity", PathBuilderValidator.FIELDS); String pathName = "username"; assertEquals(String.class, entity.get(pathName).getType()); assertEquals(String.class, entity.get(pathName, Comparable.class).getType()); assertEquals(String.class, entity.get(pathName, Object.class).getType()); }
@Test public void calling_get_with_the_same_name_and_different_types_returns_correct_type() { PathBuilder<User> entity = new PathBuilder<User>(User.class, "entity"); String pathName = "some_path"; assertEquals(Object.class, entity.get(pathName).getType()); assertEquals(Integer.class, entity.get(pathName, Integer.class).getType()); assertEquals(User.class, entity.get(pathName, User.class).getType()); }
/** * Creates an {@link Expression} for the given {@link Order} property. * * @param order must not be {@literal null}. * @return */ private Expression<?> buildOrderPropertyPathFrom(Order order) { Assert.notNull(order, "Order must not be null!"); PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType()); Expression<?> sortPropertyExpression = builder; while (path != null) { if (!path.hasNext() && order.isIgnoreCase()) { // if order is ignore-case we have to treat the last path segment as a String. sortPropertyExpression = Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower(); } else { sortPropertyExpression = Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment()); } path = path.next(); } return sortPropertyExpression; } }
@Test public void create() { PathBuilderFactory factory = new PathBuilderFactory(""); PathBuilder<Object> pathBuilder = factory.create(Object.class); assertEquals("object", pathBuilder.toString()); assertEquals(Object.class, pathBuilder.getType()); pathBuilder.get("prop", Object.class); pathBuilder.get("prop", String.class); pathBuilder.get("prop", Object.class); }
@Test public void create_withSuffix() { PathBuilderFactory factory = new PathBuilderFactory("_"); PathBuilder<Object> pathBuilder = factory.create(Object.class); assertEquals("object_", pathBuilder.toString()); assertEquals(Object.class, pathBuilder.getType()); pathBuilder.get("prop", Object.class); pathBuilder.get("prop", String.class); pathBuilder.get("prop", Object.class); }
@SuppressWarnings("unchecked") protected <A> Class<? extends A> validate(String property, Class<A> propertyType) { Class<A> validatedType = (Class<A>) validator.validate(getType(), property, propertyType); if (validatedType != null) { return validatedType; } else { throw new IllegalArgumentException("Illegal property " + property); } }
/** * Creates an {@link Expression} for the given {@link Order} property. * * @param order must not be {@literal null}. * @param builder must not be {@literal null}. * @return */ private static Expression<?> buildOrderPropertyPathFrom(Order order, PathBuilder<?> builder) { Assert.notNull(order, "Order must not be null!"); Assert.notNull(builder, "Builder must not be null!"); PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType()); Expression<?> sortPropertyExpression = builder; while (path != null) { if (!path.hasNext() && order.isIgnoreCase()) { // if order is ignore-case we have to treat the last path segment as a String. sortPropertyExpression = Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower(); } else { sortPropertyExpression = Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment()); } path = path.next(); } return sortPropertyExpression; }
/** * Creates an {@link Expression} for the given {@link Order} property. * * @param order must not be {@literal null}. * @param builder must not be {@literal null}. * @return */ private static Expression<?> buildOrderPropertyPathFrom(Order order, PathBuilder<?> builder) { Assert.notNull(order, "Order must not be null!"); Assert.notNull(builder, "Builder must not be null!"); PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType()); Expression<?> sortPropertyExpression = builder; while (path != null) { if (!path.hasNext() && order.isIgnoreCase()) { // if order is ignore-case we have to treat the last path segment as a String. sortPropertyExpression = Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower(); } else { sortPropertyExpression = Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment()); } path = path.next(); } return sortPropertyExpression; }
/** * Creates an {@link Expression} for the given {@link Order} property. * * @param order must not be {@literal null}. * @param builder must not be {@literal null}. * @return */ private static Expression<?> buildOrderPropertyPathFrom(Order order, PathBuilder<?> builder) { Assert.notNull(order, "Order must not be null!"); Assert.notNull(builder, "Builder must not be null!"); PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType()); Expression<?> sortPropertyExpression = builder; while (path != null) { if (!path.hasNext() && order.isIgnoreCase()) { // if order is ignore-case we have to treat the last path segment as a String. sortPropertyExpression = Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower(); } else { sortPropertyExpression = Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment()); } path = path.next(); } return sortPropertyExpression; }
/** * Creates an {@link Expression} for the given {@link Order} property. * * @param order must not be {@literal null}. * @return */ private Expression<?> buildOrderPropertyPathFrom(Order order) { Assert.notNull(order, "Order must not be null!"); PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType()); Expression<?> sortPropertyExpression = builder; while (path != null) { if (!path.hasNext() && order.isIgnoreCase()) { // if order is ignore-case we have to treat the last path segment as a String. sortPropertyExpression = Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower(); } else { sortPropertyExpression = Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment()); } path = path.next(); } return sortPropertyExpression; } }