CacheControl { @Override public String toString() { StringBuilder buffer = new StringBuilder(); if (isPrivate()) { buffer.append(PRIVATE); handleFields(getPrivateFields(), buffer); buffer.append(SEPARATOR); } if (isNoCache()) { buffer.append(NO_CACHE); handleFields(getNoCacheFields(), buffer); buffer.append(SEPARATOR); } if (isNoStore()) { buffer.append(NO_STORE).append(SEPARATOR); } if (isNoTransform()) { buffer.append(NO_TRANSFORM).append(SEPARATOR); } if (isMustRevalidate()) { buffer.append(MUST_REVALIDATE).append(SEPARATOR); } if (isProxyRevalidate()) { buffer.append(PROXY_REVALIDATE).append(SEPARATOR); } if (getMaxAge() != -1) { buffer.append(MAX_AGE).append('=').append(getMaxAge()).append(SEPARATOR); } if (getSMaxAge() != -1) { buffer.append(SMAX_AGE).append('=').append(getSMaxAge()).append(SEPARATOR); } Map<String, String> extension = getCacheExtension(); for (Map.Entry<String, String> entry : extension.entrySet()) { buffer.append(entry.getKey()); String value = entry.getValue(); if (value != null) { buffer.append("="); if (value.indexOf(' ') != -1) { buffer.append('\"').append(value).append('\"'); } else { buffer.append(value); } } buffer.append(SEPARATOR); } String string = buffer.toString(); if (string.endsWith(SEPARATOR)) { string = string.substring(0, string.length() - 1); } return string; } CacheControl(); Map<String, String> getCacheExtension(); int getMaxAge(); List<String> getNoCacheFields(); List<String> getPrivateFields(); int getSMaxAge(); boolean isMustRevalidate(); boolean isNoCache(); boolean isNoStore(); boolean isNoTransform(); boolean isPrivate(); boolean isProxyRevalidate(); void setMaxAge(int maxAge); void setMustRevalidate(boolean mustRevalidate); void setNoCache(boolean noCache); void setNoStore(boolean noStore); void setNoTransform(boolean noTransform); void setPrivate(boolean isPrivate); void setProxyRevalidate(boolean proxyRevalidate); void setSMaxAge(int sMaxAge); static CacheControl valueOf(String string); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void testToString() { String expected = "private=\"foo\",no-cache=\"bar\",no-store,no-transform,must-revalidate,proxy-revalidate,max-age=2,s-maxage=3"; String parsed = CacheControl.valueOf(expected).toString(); assertEquals(parsed, expected); }