Consolidate partialPaths under FullPathComposite

This commit is contained in:
Rossen Stoyanchev 2015-03-30 15:56:03 -04:00
parent 7668ea1549
commit 3a8a28beec
2 changed files with 103 additions and 107 deletions

View File

@ -642,13 +642,49 @@ final class HierarchicalUriComponents extends UriComponents {
public FullPathComponent(String path) { public FullPathComponent(String path) {
this.partialPaths = PartialPath.parse(path); this.partialPaths = Collections.unmodifiableList(initPartialPaths(path));
this.path = path; this.path = path;
} }
private FullPathComponent(List<PartialPath> partialPaths) { private FullPathComponent(List<PartialPath> partialPaths) {
this.partialPaths = partialPaths; this.partialPaths = Collections.unmodifiableList(partialPaths);
this.path = PartialPath.getPath(partialPaths); this.path = initPath(partialPaths);
}
private static String initPath(List<PartialPath> partialPaths) {
StringBuilder builder = new StringBuilder();
for (PartialPath partialPath : partialPaths) {
builder.append(partialPath.getValue());
}
return builder.toString();
}
private static List<PartialPath> initPartialPaths(String path) {
List<PartialPath> result = new ArrayList<PartialPath>();
int startIdx;
int endIdx = 0;
while ((startIdx = path.indexOf("{/", endIdx)) != -1) {
if (startIdx > endIdx) {
String prevPart = path.substring(endIdx, startIdx);
result.add(new PartialPath(prevPart, Type.PATH));
}
endIdx = path.indexOf('}', startIdx + 2) + 1;
if (endIdx == -1) {
throw new IllegalArgumentException("Path \"" + path + "\" has no " +
"closing \"}\" after \"{/\" at index " + startIdx);
}
String part = path.substring(startIdx, endIdx);
result.add(new PartialPath(part, Type.PATH_SEGMENT));
}
if (endIdx < path.length()) {
String endPart = path.substring(endIdx);
result.add(new PartialPath(endPart, Type.PATH));
}
return result;
}
List<PartialPath> getPartialPaths() {
return this.partialPaths;
} }
@Override @Override
@ -670,21 +706,29 @@ final class HierarchicalUriComponents extends UriComponents {
@Override @Override
public PathComponent encode(String encoding) throws UnsupportedEncodingException { public PathComponent encode(String encoding) throws UnsupportedEncodingException {
List<PartialPath> encodedPath = List<PartialPath> result = new ArrayList<PartialPath>();
PartialPath.encode(this.partialPaths, encoding); for (PartialPath partialPath : this.partialPaths) {
return new FullPathComponent(encodedPath); PartialPath encoded = partialPath.encode(encoding);
result.add(encoded);
}
return new FullPathComponent(result);
} }
@Override @Override
public void verify() { public void verify() {
verifyUriComponent(getPath(), Type.PATH); for (PartialPath partialPath : this.partialPaths) {
partialPath.verify();
}
} }
@Override @Override
public PathComponent expand(UriTemplateVariables uriVariables) { public PathComponent expand(UriTemplateVariables uriVariables) {
List<PartialPath> expandedPath = List<PartialPath> result = new ArrayList<PartialPath>();
PartialPath.expand(this.partialPaths, uriVariables); for (PartialPath partialPath : this.partialPaths) {
return new FullPathComponent(expandedPath); PartialPath expanded = partialPath.expand(uriVariables);
result.add(expanded);
}
return new FullPathComponent(result);
} }
@Override @Override
@ -721,80 +765,30 @@ final class HierarchicalUriComponents extends UriComponents {
this.type = type; this.type = type;
} }
public String getValue() {
return this.value;
}
private PartialPath expand(UriTemplateVariables uriVariables) { private PartialPath expand(UriTemplateVariables uriVariables) {
String expandedValue = expandUriComponent(this.value, uriVariables); String expandedValue = expandUriComponent(this.value, uriVariables);
return new PartialPath(expandedValue, this.type); return new PartialPath(expandedValue, this.type);
} }
private PartialPath encode(String encoding) private PartialPath encode(String encoding) throws UnsupportedEncodingException {
throws UnsupportedEncodingException {
String encodedPath = encodeUriComponent(this.value, encoding, this.type); String encodedPath = encodeUriComponent(this.value, encoding, this.type);
return new PartialPath(encodedPath, this.type); return new PartialPath(encodedPath, this.type);
} }
public void verify() {
verifyUriComponent(this.value, this.type);
}
@Override @Override
public String toString() { public String toString() {
return value; return this.value;
} }
public static List<PartialPath> parse(String path) {
List<PartialPath> result = new ArrayList<PartialPath>();
int startIdx;
int endIdx = 0;
while ((startIdx = path.indexOf("{/", endIdx)) != -1) {
if (startIdx > endIdx) {
String prevPart = path.substring(endIdx, startIdx);
result.add(new PartialPath(prevPart, Type.PATH));
} }
endIdx = path.indexOf('}', startIdx + 2) + 1;
if (endIdx == -1) {
throw new IllegalArgumentException("Path \"" + path + "\" has no " +
"closing \"}\" after \"{/\" at index " + startIdx);
} }
String part = path.substring(startIdx, endIdx);
result.add(new PartialPath(part, Type.PATH_SEGMENT));
}
if (endIdx < path.length()) {
String endPart = path.substring(endIdx);
result.add(new PartialPath(endPart, Type.PATH));
}
return Collections.unmodifiableList(result);
}
public static String getPath(List<PartialPath> partialPaths) {
StringBuilder builder = new StringBuilder();
for (PartialPath partialPath : partialPaths) {
builder.append(partialPath.value);
}
return builder.toString();
}
public static List<PartialPath> expand(
List<PartialPath> partialPaths,
UriTemplateVariables uriVariables) {
List<PartialPath> result = new ArrayList<PartialPath>();
for (PartialPath partialPath : partialPaths) {
PartialPath expanded = partialPath.expand(uriVariables);
result.add(expanded);
}
return result;
}
public static List<PartialPath> encode(
List<PartialPath> partialPaths, String encoding)
throws UnsupportedEncodingException {
List<PartialPath> result = new ArrayList<PartialPath>();
for (PartialPath partialPath : partialPaths) {
PartialPath encoded = partialPath.encode(encoding);
result.add(encoded);
}
return result;
}
}
}
/** /**
* Represents a path backed by a string list (i.e. path segments). * Represents a path backed by a string list (i.e. path segments).
@ -964,6 +958,7 @@ final class HierarchicalUriComponents extends UriComponents {
@Override @Override
public void copyToUriComponentsBuilder(UriComponentsBuilder builder) { public void copyToUriComponentsBuilder(UriComponentsBuilder builder) {
} }
@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return (this == obj); return (this == obj);

View File

@ -16,6 +16,10 @@
package org.springframework.web.util; package org.springframework.web.util;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.web.util.UriComponentsBuilder.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
@ -23,13 +27,10 @@ import java.io.ObjectOutputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.Matchers.*; import org.springframework.web.util.HierarchicalUriComponents.FullPathComponent;
import static org.junit.Assert.*;
import static org.springframework.web.util.UriComponentsBuilder.*;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
@ -168,45 +169,45 @@ public class UriComponentsTests {
@Test @Test
public void partialPath() throws Exception { public void partialPath() throws Exception {
List<HierarchicalUriComponents.FullPathComponent.PartialPath> l; FullPathComponent l;
l = HierarchicalUriComponents.FullPathComponent.PartialPath.parse("x"); l = new FullPathComponent("x");
assertEquals(1, l.size()); assertEquals(1, l.getPartialPaths().size());
assertEquals("x", l.get(0).value); assertEquals("x", l.getPartialPaths().get(0).value);
assertEquals(HierarchicalUriComponents.Type.PATH, l.get(0).type); assertEquals(HierarchicalUriComponents.Type.PATH, l.getPartialPaths().get(0).type);
l = HierarchicalUriComponents.FullPathComponent.PartialPath.parse("/foo"); l = new FullPathComponent("/foo");
assertEquals(1, l.size()); assertEquals(1, l.getPartialPaths().size());
assertEquals("/foo", l.get(0).value); assertEquals("/foo", l.getPartialPaths().get(0).value);
assertEquals(HierarchicalUriComponents.Type.PATH, l.get(0).type); assertEquals(HierarchicalUriComponents.Type.PATH, l.getPartialPaths().get(0).type);
l = HierarchicalUriComponents.FullPathComponent.PartialPath.parse("{/foo}"); l = new FullPathComponent("{/foo}");
assertEquals(1, l.size()); assertEquals(1, l.getPartialPaths().size());
assertEquals("{/foo}", l.get(0).value); assertEquals("{/foo}", l.getPartialPaths().get(0).value);
assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.get(0).type); assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.getPartialPaths().get(0).type);
l = HierarchicalUriComponents.FullPathComponent.PartialPath.parse("/foo{/bar}"); l = new FullPathComponent("/foo{/bar}");
assertEquals(2, l.size()); assertEquals(2, l.getPartialPaths().size());
assertEquals("/foo", l.get(0).value); assertEquals("/foo", l.getPartialPaths().get(0).value);
assertEquals(HierarchicalUriComponents.Type.PATH, l.get(0).type); assertEquals(HierarchicalUriComponents.Type.PATH, l.getPartialPaths().get(0).type);
assertEquals("{/bar}", l.get(1).value); assertEquals("{/bar}", l.getPartialPaths().get(1).value);
assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.get(1).type); assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.getPartialPaths().get(1).type);
l = HierarchicalUriComponents.FullPathComponent.PartialPath.parse("{/foo}{/bar}"); l = new FullPathComponent("{/foo}{/bar}");
assertEquals(2, l.size()); assertEquals(2, l.getPartialPaths().size());
assertEquals("{/foo}", l.get(0).value); assertEquals("{/foo}", l.getPartialPaths().get(0).value);
assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.get(0).type); assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.getPartialPaths().get(0).type);
assertEquals("{/bar}", l.get(1).value); assertEquals("{/bar}", l.getPartialPaths().get(1).value);
assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.get(1).type); assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.getPartialPaths().get(1).type);
l = HierarchicalUriComponents.FullPathComponent.PartialPath.parse("foo{/bar}baz"); l = new FullPathComponent("foo{/bar}baz");
assertEquals(3, l.size()); assertEquals(3, l.getPartialPaths().size());
assertEquals("foo", l.get(0).value); assertEquals("foo", l.getPartialPaths().get(0).value);
assertEquals(HierarchicalUriComponents.Type.PATH, l.get(0).type); assertEquals(HierarchicalUriComponents.Type.PATH, l.getPartialPaths().get(0).type);
assertEquals("{/bar}", l.get(1).value); assertEquals("{/bar}", l.getPartialPaths().get(1).value);
assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.get(1).type); assertEquals(HierarchicalUriComponents.Type.PATH_SEGMENT, l.getPartialPaths().get(1).type);
assertEquals("baz", l.get(2).value); assertEquals("baz", l.getPartialPaths().get(2).value);
assertEquals(HierarchicalUriComponents.Type.PATH, l.get(2).type); assertEquals(HierarchicalUriComponents.Type.PATH, l.getPartialPaths().get(2).type);
} }