@Test public void oneToOneToMany_projection_as_bean() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.bean(User.class, userName, Projections.bean(Post.class, postId, postName, set(qComment).as("comments")).as("latestPost")))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection() { Map<String, User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).as(Projections.constructor(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment))))); assertEquals(2, results.size()); User user = results.get("Jane"); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void transform_results() { List<Post> results = POST_W_COMMENTS.transform( groupBy(postId).list(Projections.constructor(Post.class, postId, postName, set(qComment)))); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void oneToOneToMany_projection() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.constructor(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment))))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean_and_constructor() { Map<String, User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).as(Projections.bean(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment)).as("latestPost")))); assertEquals(2, results.size()); User user = results.get("Jane"); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void transform_results() { CloseableIterator<Post> resultsIt = POST_W_COMMENTS.transform( groupBy(postId).iterate(Projections.constructor(Post.class, postId, postName, set(qComment)))); List<Post> results = IteratorAdapter.asList(resultsIt); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void transform_as_bean() { List<Post> results = POST_W_COMMENTS.transform( groupBy(postId).list(Projections.bean(Post.class, postId, postName, set(qComment).as("comments")))); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean_and_constructor() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.bean(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment)).as("latestPost")))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection() { CloseableIterator<User> resultsIt = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).iterate(Projections.constructor(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment))))); List<User> results = IteratorAdapter.asList(resultsIt); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean() { Map<String, User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).as(Projections.bean(User.class, userName, Projections.bean(Post.class, postId, postName, set(qComment).as("comments")).as("latestPost")))); assertEquals(2, results.size()); User user = results.get("Jane"); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void transform_as_bean() { CloseableIterator<Post> resultsIt = POST_W_COMMENTS.transform( groupBy(postId).iterate(Projections.bean(Post.class, postId, postName, set(qComment).as("comments")))); List<Post> results = IteratorAdapter.asList(resultsIt); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean_and_constructor() { CloseableIterator<User> resultsIt = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).iterate(Projections.bean(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment)).as("latestPost")))); List<User> results = IteratorAdapter.asList(resultsIt); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean() { CloseableIterator<User> resultsIt = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).iterate(Projections.bean(User.class, userName, Projections.bean(Post.class, postId, postName, set(qComment).as("comments")).as("latestPost")))); List<User> results = IteratorAdapter.asList(resultsIt); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void transform_results() { Map<Integer, Post> results = POST_W_COMMENTS.transform( groupBy(postId).as(Projections.constructor(Post.class, postId, postName, set(qComment)))); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void transform_via_groupByProjection() { Map<Integer, Post> results = POST_W_COMMENTS2.transform( new GroupByProjection<Integer, Post>(postId, postName, set(qComment)) { @Override protected Post transform(Group group) { return new Post( group.getOne(postId), group.getOne(postName), group.getSet(qComment)); } }); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void transform_as_bean() { Map<Integer, Post> results = POST_W_COMMENTS.transform( groupBy(postId).as(Projections.bean(Post.class, postId, postName, set(qComment).as("comments")))); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
Collections.sort(posts, new Comparator<Post>() { @Override public int compare(Post lhs, Post rhs) { String left = lhs.getName().toLowerCase(); String right = rhs.getName().toLowerCase(); if (left.equals(right)) { return lhs.getName().compareTo(rhs.getName()); } else { return left.compareTo(right); } });
Collections.sort(posts, new Comparator<Post>() { @Override public int compare(Post lhs, Post rhs) { return ComparatorChain.start() .compare(lhs.getName(), rhs.getName(), String.CASE_INSENSITIVE_ORDER) .compare(lhs.getName(), rhs.getName()) .compare(lhs.getDate(), rhs.getDate()) .compare(lhs.getId(), rhs.getId()) .result(); } });
Collections.sort(posts, new Comparator<Post>() { @Override public int compare(Post lhs, Post rhs) { int nameComp = lhs.getName().compareToIgnoreCase(rhs.getName()); if (nameComp == 0) { Integer lhsSize = lhs.getSize(); Integer rhsSize = rhs.getSize(); return lhsSize.compareTo(rhsSize); } else { return nameComp; } } });