Ordering { public static Ordering parse(String ordering) { if (ordering == null) { return Ordering.empty(); } final StringTokenizer tokenizer = new StringTokenizer(ordering, ","); final Map<Field, Direction> fields = Maps.newLinkedHashMap(); while (tokenizer.hasMoreTokens()) { final String token = tokenizer.nextToken(); final int index = token.indexOf('-'); final Direction direction = index > -1 ? Direction.DESCENDING : Direction.ASCENDING; final Field field = Field.fromName(token.substring(index + 1)); Preconditions.checkArgument( field != null, "Unsupported ordering field: %s", token ); fields.put(field, direction); } return new Ordering(fields); } private  Ordering(Map<Field, Direction> fields); Map<Field, Direction> getFields(); boolean isEmpty(); static Ordering parse(String ordering); static Ordering empty(); static Builder newBuilder();  }
@Test public void testParse() { assertEquals(Ordering.empty().getFields(), Ordering.parse(null).getFields()); assertEquals(Ordering.empty().getFields(), Ordering.parse("").getFields()); assertEquals( ImmutableMap.of(Ordering.Field.NAME, Ordering.Direction.ASCENDING), Ordering.parse("name").getFields() ); assertEquals( ImmutableMap.of(Ordering.Field.NAME, Ordering.Direction.DESCENDING), Ordering.parse("-name").getFields() ); assertEquals( ImmutableMap.of( Ordering.Field.ACTIVE, Ordering.Direction.ASCENDING, Ordering.Field.NAME, Ordering.Direction.DESCENDING, Ordering.Field.CREATED, Ordering.Direction.ASCENDING ), Ordering.parse("active,-name,created").getFields() ); }
@Test(expected = IllegalArgumentException.class) public void testParseUnknown() { Ordering.parse("unknown"); }