SortCriterionImpl implements SortCriterion { public static SortCriterionImpl parse(final String sortCriterion) { requireNonNull(sortCriterion); String[] parts = sortCriterion.split(":"); if (parts.length != 2) throw new IllegalArgumentException("sortOrder must be of form <field name>:ASC/DESC"); if ("ASC".equalsIgnoreCase(parts[1]) || "Ascending".equalsIgnoreCase(parts[1])) return new SortCriterionImpl(parts[0], Order.Ascending); if ("DESC".equalsIgnoreCase(parts[1]) || "Descending".equalsIgnoreCase(parts[1])) return new SortCriterionImpl(parts[0], Order.Descending); throw new IllegalArgumentException("Invalid order " + parts[1]); } SortCriterionImpl(String fieldName, Order order); static SortCriterionImpl parse(final String sortCriterion); @Override String getFieldName(); @Override Order getOrder(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }
@Test public void testParse() { assertEquals(orderByNameAsc, SortCriterionImpl.parse("name:ASC")); assertEquals(orderByDateDesc, SortCriterionImpl.parse("date:DESC")); }
@Test(expected = IllegalArgumentException.class) public void testParseWithTooManyParts() throws Exception { SortCriterionImpl.parse("name:ASC:first"); }
@Test(expected = IllegalArgumentException.class) public void testParseWithOnlyOnePart() throws Exception { SortCriterionImpl.parse("name:"); }
@Test(expected = IllegalArgumentException.class) public void testParseWithInvalidDirection() throws Exception { SortCriterionImpl.parse("name:ASCDESC"); }