Skip to content

Commit

Permalink
Replace guava with funclite
Browse files Browse the repository at this point in the history
  • Loading branch information
hamnis committed Feb 24, 2014
1 parent e6bcc1b commit 40b9b81
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
<groupId>net.hamnaberg</groupId>
<artifactId>funclite</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>

Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/codehaus/httpcache4j/uri/QueryParams.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.codehaus.httpcache4j.uri;

import com.google.common.base.Splitter;

import java.text.Collator;
import java.util.*;

Expand Down Expand Up @@ -151,8 +149,7 @@ public String toQuery(boolean sort) {
Collections.sort(params, new Comparator<QueryParam>() {
@Override
public int compare(QueryParam o1, QueryParam o2) {
//TODO: Possible bug here.
return Collator.getInstance(Locale.getDefault()).compare(o1.getName(), o2.getName());
return Collator.getInstance(Locale.ENGLISH).compare(o1.getName(), o2.getName());
}
});
}
Expand Down Expand Up @@ -201,17 +198,17 @@ public int hashCode() {
public static QueryParams parse(String query) {
Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
if (query != null) {
Iterable<String> parts = Splitter.on("&").omitEmptyStrings().trimResults().split(query);
String[] parts = query.split("&");
for (String part : parts) {
String[] equalParts = part.split("=");
String[] equalParts = part.trim().split("=");
String name = null;
String value = null;
if (equalParts.length == 1) {
name = equalParts[0];
name = equalParts[0].trim();
}
else if (equalParts.length == 2) {
name = equalParts[0];
value = equalParts[1];
name = equalParts[0].trim();
value = equalParts[1].trim();
}
if (name != null) {
addToQueryMap(map, URIDecoder.decodeUTF8(name), URIDecoder.decodeUTF8(value));
Expand Down
30 changes: 13 additions & 17 deletions src/main/java/org/codehaus/httpcache4j/uri/URIBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

package org.codehaus.httpcache4j.uri;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import net.hamnaberg.funclite.*;

import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -70,7 +67,7 @@ public URIBuilder withHost(String host) {
*/
public URIBuilder withPort(int port) {
Optional<Integer> defaultPort = schemeDefaults.get().getPort(scheme);
if (defaultPort.isPresent() && (port == defaultPort.get())) {
if (defaultPort.isSome() && (port == defaultPort.get())) {
port = -1;
}
return new URIBuilder(scheme, schemeSpecificPart, host, port, path, fragment, parameters, wasPathAbsolute, endsWithSlash);
Expand Down Expand Up @@ -99,11 +96,10 @@ public URIBuilder addRawPath(String path) {
boolean pathAbsolute = wasPathAbsolute || this.path.isEmpty() && path.startsWith("/");
boolean endsWithSlash = this.endsWithSlash || this.path.isEmpty() && path.endsWith("/");
List<Path> appendedPath = toPathParts(path);
ImmutableList.Builder<Path> currentPath = ImmutableList.builder();
ArrayList<Path> currentPath = new ArrayList<Path>();
currentPath.addAll(this.path);
currentPath.addAll(appendedPath);
return new URIBuilder(scheme, schemeSpecificPart, host, port, currentPath.build(), fragment, parameters, pathAbsolute, endsWithSlash);

return pathInternal(currentPath, pathAbsolute, endsWithSlash);
}

/**
Expand All @@ -115,11 +111,11 @@ public URIBuilder addRawPath(String path) {
* @return a new URI builder which contains the new path.
*/
public URIBuilder addPath(List<String> path) {
List<Path> appendedPath = Lists.transform(path, stringToPath);
ImmutableList.Builder<Path> currentPath = ImmutableList.builder();
List<Path> appendedPath = CollectionOps.map(path, stringToPath);
ArrayList<Path> currentPath = new ArrayList<Path>();
currentPath.addAll(this.path);
currentPath.addAll(appendedPath);
return new URIBuilder(scheme, schemeSpecificPart, host, port, currentPath.build(), fragment, parameters, wasPathAbsolute, endsWithSlash);
return pathInternal(currentPath, wasPathAbsolute, false);
}

/**
Expand Down Expand Up @@ -151,7 +147,7 @@ public URIBuilder withPath(String... path) {
* @return a new URI builder which contains the new path.
*/
public URIBuilder withPath(List<String> pathList) {
List<Path> paths = Lists.transform(pathList, stringToPath);
List<Path> paths = CollectionOps.map(pathList, stringToPath);
return pathInternal(paths, false, false);
}

Expand All @@ -169,7 +165,7 @@ public URIBuilder withRawPath(String path) {
}

private URIBuilder pathInternal(List<Path> pathList, boolean pathAbsolute, boolean endsWithSlash) {
return new URIBuilder(scheme, schemeSpecificPart, host, port, ImmutableList.copyOf(pathList), fragment, parameters, pathAbsolute, endsWithSlash);
return new URIBuilder(scheme, schemeSpecificPart, host, port, pathList, fragment, parameters, pathAbsolute, endsWithSlash);
}

public URIBuilder withFragment(String fragment) {
Expand Down Expand Up @@ -344,7 +340,7 @@ public List<QueryParam> getParametersByName(final String name) {
if (params == null) {
return Collections.emptyList();
}
return Lists.transform(params, new Function<String, QueryParam>() {
return CollectionOps.map(params, new Function<String, QueryParam>() {
public QueryParam apply(String s) {
return new QueryParam(name, s);
}
Expand Down Expand Up @@ -387,11 +383,11 @@ public int getPort() {
}

public List<String> getPath() {
return Lists.transform(path, pathToString);
return CollectionOps.map(path, pathToString);
}

public List<String> getEncodedPath() {
return Lists.transform(path, encodedPathToString);
return CollectionOps.map(path, encodedPathToString);
}

public String getCurrentPath() {
Expand Down Expand Up @@ -422,7 +418,7 @@ private static List<Path> toPathParts(String path) {
path = path.substring(1);
}
List<String> stringList = Arrays.asList(path.split("/"));
return ImmutableList.copyOf(Lists.transform(stringList, stringToPath));
return CollectionOps.map(stringList, stringToPath);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.codehaus.httpcache4j.uri;

import java.util.Map;
import java.util.HashMap;
import net.hamnaberg.funclite.Optional;

import com.google.common.base.Optional;
import com.google.common.collect.Maps;

public final class URISchemeDefaults {
private final Map<String, Integer> map = Maps.newHashMap();
private final Map<String, Integer> map = new HashMap<String, Integer>();

public URISchemeDefaults(Map<String, Integer> defaults) {
map.putAll(defaults);
Expand Down
13 changes: 11 additions & 2 deletions src/test/java/org/codehaus/httpcache4j/uri/URIBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

package org.codehaus.httpcache4j.uri;

import com.google.common.collect.Iterables;
import org.junit.Assert;
import org.junit.Test;
import net.hamnaberg.funclite.*;

import java.net.URI;
import java.util.UUID;
Expand Down Expand Up @@ -246,6 +246,15 @@ public void uriWithoutQueryNameIsApparentlyLegal() {
URIBuilder builder = URIBuilder.fromURI(uri);
assertEquals("URIs did not match", uri, builder.toURI());
Iterable<QueryParam> parameters = builder.getParameters();
assertEquals(1, Iterables.size(parameters));
assertEquals(1, size(parameters));
}

public static <A> int size(Iterable<A> iterable) {
int size = 0;
for (A value : iterable) {
size++;
}
return size;
}

}

0 comments on commit 40b9b81

Please sign in to comment.