RangeHeader { public static RangeHeader parse(@Nullable final String header) { if (header == null) { return RangeHeader.create(); } final RangeHeader.Builder builder = RangeHeader.builder(); final List<String> parts = ImmutableList.copyOf(Splitter.on(';').trimResults().split(header)); final int count = parts.size(); if (count > 0) { final Matcher first = ID_PATTERN.matcher(parts.get(0)); if (first.matches()) { final String fromId = first.group("fromId"); final String toId = first.group("toId"); builder.field(first.group("field")).fromId(fromId).toId(toId); if (fromId != null) { builder.fromInclusive(Strings.isNullOrEmpty(first.group("fromInclusive"))); } if (toId != null) { builder.toInclusive(Strings.isNullOrEmpty(first.group("toInclusive"))); } } } if (count > 1) { final Matcher second = OPTIONS_PATTERN.matcher(parts.get(1)); if (second.matches()) { try { builder.max(Integer.parseInt(second.group("max"))); } catch (NumberFormatException ignore) { } } } return builder.build(); } private  RangeHeader(final Builder builder); static Builder builder(); static RangeHeader create(); static RangeHeader parse(@Nullable final String header); Optional<String> getField(); Optional<String> getFromId(); Optional<Boolean> getFromInclusive(); Optional<String> getToId(); Optional<Boolean> getToInclusive(); Optional<Integer> getMax(); @Override boolean equals(final Object obj); @Override int hashCode(); @Override String toString();  }
@Test public void testParse() throws Exception { RangeHeader expected = RangeHeader.builder() .field("id") .fromId("1") .fromInclusive(true) .toId("26") .toInclusive(true) .max(1) .build(); RangeHeader actual = RangeHeader.parse("id 1..26; max=1"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField().get()).isEqualTo("id"); assertThat(actual.getFromId().get()).isEqualTo("1"); assertThat(actual.getFromInclusive().get()).isEqualTo(true); assertThat(actual.getToId().get()).isEqualTo("26"); assertThat(actual.getToInclusive().get()).isEqualTo(true); assertThat(actual.getMax().get()).isEqualTo(1); expected = RangeHeader.builder() .field("id") .fromId("1") .fromInclusive(true) .toId("26") .toInclusive(true) .build(); actual = RangeHeader.parse("id 1..26"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField().get()).isEqualTo("id"); assertThat(actual.getFromId().get()).isEqualTo("1"); assertThat(actual.getFromInclusive().get()).isEqualTo(true); assertThat(actual.getToId().get()).isEqualTo("26"); assertThat(actual.getToInclusive().get()).isEqualTo(true); assertThat(actual.getMax()).isEqualTo(Optional.empty()); expected = RangeHeader.builder() .field("id") .fromId("1") .fromInclusive(true) .toId("26") .toInclusive(true) .build(); actual = RangeHeader.parse("id 1..26;"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField().get()).isEqualTo("id"); assertThat(actual.getFromId().get()).isEqualTo("1"); assertThat(actual.getFromInclusive().get()).isEqualTo(true); assertThat(actual.getToId().get()).isEqualTo("26"); assertThat(actual.getToInclusive().get()).isEqualTo(true); assertThat(actual.getMax()).isEqualTo(Optional.empty()); expected = RangeHeader.builder() .field("id") .fromId("1") .fromInclusive(true) .toId("26") .toInclusive(true) .max(1) .build(); actual = RangeHeader.parse("id 1..26 ; max=1"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField().get()).isEqualTo("id"); assertThat(actual.getFromId().get()).isEqualTo("1"); assertThat(actual.getFromInclusive().get()).isEqualTo(true); assertThat(actual.getToId().get()).isEqualTo("26"); assertThat(actual.getToInclusive().get()).isEqualTo(true); assertThat(actual.getMax().get()).isEqualTo(1); expected = RangeHeader.builder().field("id").fromId("1").fromInclusive(true).max(1).build(); actual = RangeHeader.parse("id 1..; max=1"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField().get()).isEqualTo("id"); assertThat(actual.getFromId().get()).isEqualTo("1"); assertThat(actual.getFromInclusive().get()).isEqualTo(true); assertThat(actual.getToId()).isEqualTo(Optional.empty()); assertThat(actual.getToInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getMax().get()).isEqualTo(1); expected = RangeHeader.builder().field("id").fromId("1").fromInclusive(false).max(1).build(); actual = RangeHeader.parse("id ]1..; max=1"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField().get()).isEqualTo("id"); assertThat(actual.getFromId().get()).isEqualTo("1"); assertThat(actual.getFromInclusive().get()).isEqualTo(false); assertThat(actual.getToId()).isEqualTo(Optional.empty()); assertThat(actual.getToInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getMax().get()).isEqualTo(1); expected = RangeHeader.builder().field("id").toId("26").toInclusive(true).max(1).build(); actual = RangeHeader.parse("id ..26; max=1"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField().get()).isEqualTo("id"); assertThat(actual.getFromId()).isEqualTo(Optional.empty()); assertThat(actual.getFromInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getToId().get()).isEqualTo("26"); assertThat(actual.getToInclusive().get()).isEqualTo(true); assertThat(actual.getMax().get()).isEqualTo(1); expected = RangeHeader.builder().field("id").toId("26").toInclusive(false).max(1).build(); actual = RangeHeader.parse("id ..26[; max=1"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField().get()).isEqualTo("id"); assertThat(actual.getFromId()).isEqualTo(Optional.empty()); assertThat(actual.getFromInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getToId().get()).isEqualTo("26"); assertThat(actual.getToInclusive().get()).isEqualTo(false); assertThat(actual.getMax().get()).isEqualTo(1); expected = RangeHeader.builder().max(1).build(); actual = RangeHeader.parse("id; max=1"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField()).isEqualTo(Optional.empty()); assertThat(actual.getFromId()).isEqualTo(Optional.empty()); assertThat(actual.getFromInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getToId()).isEqualTo(Optional.empty()); assertThat(actual.getToInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getMax().get()).isEqualTo(1); expected = RangeHeader.builder().max(1).build(); actual = RangeHeader.parse("id;max=1"); assertThat(actual).isEqualTo(expected); assertThat(actual.getField()).isEqualTo(Optional.empty()); assertThat(actual.getFromId()).isEqualTo(Optional.empty()); assertThat(actual.getFromInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getToId()).isEqualTo(Optional.empty()); assertThat(actual.getToInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getMax().get()).isEqualTo(1); expected = RangeHeader.create(); actual = RangeHeader.parse(null); assertThat(actual).isEqualTo(expected); assertThat(actual.getField()).isEqualTo(Optional.empty()); assertThat(actual.getFromId()).isEqualTo(Optional.empty()); assertThat(actual.getFromInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getToId()).isEqualTo(Optional.empty()); assertThat(actual.getToInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getMax()).isEqualTo(Optional.empty()); expected = RangeHeader.create(); actual = RangeHeader.parse(""); assertThat(actual).isEqualTo(expected); assertThat(actual.getField()).isEqualTo(Optional.empty()); assertThat(actual.getFromId()).isEqualTo(Optional.empty()); assertThat(actual.getFromInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getToId()).isEqualTo(Optional.empty()); assertThat(actual.getToInclusive()).isEqualTo(Optional.empty()); assertThat(actual.getMax()).isEqualTo(Optional.empty()); }