Skip to content

Commit

Permalink
Merge pull request #889 from caelum/ot-usingmodernapis
Browse files Browse the repository at this point in the history
Refactoring internal API to use modern Java APIs
  • Loading branch information
garcia-jj committed Nov 17, 2014
2 parents fbe3635 + 986d817 commit 414fb6f
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ public DefaultEnvironment(EnvironmentType environmentType) throws IOException {

private void loadAndPut(String environment) throws IOException {
String name = "/" + environment + ".properties";
InputStream stream = DefaultEnvironment.class.getResourceAsStream(name);
Properties properties = new Properties();

if (stream != null) {
properties.load(stream);
this.properties.putAll(properties);
} else {
LOG.warn("Could not find the file '{}.properties' to load.", environment);
try (InputStream stream = DefaultEnvironment.class.getResourceAsStream(name)) {
Properties properties = new Properties();

if (stream != null) {
properties.load(stream);
this.properties.putAll(properties);
} else {
LOG.warn("Could not find the file '{}.properties' to load.", environment);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.enterprise.inject.Vetoed;
Expand All @@ -43,7 +42,7 @@ public class VRaptorRequest extends HttpServletRequestWrapper implements Mutable

private static final Logger logger = LoggerFactory.getLogger(VRaptorRequest.class);

private final Hashtable<String, String[]> extraParameters = new Hashtable<>();
private final Map<String, String[]> extraParameters = new HashMap<>();

public VRaptorRequest(HttpServletRequest request) {
super(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ private Pattern compilePattern(String originalPattern, Map<String, String> param
@Override
public String fillUri(Parameter[] paramNames, Object... paramValues) {
if (paramNames.length != paramValues.length) {
throw new IllegalArgumentException("paramNames must have the same length as paramValues. Names: " + paramNames + " Values: " + Arrays.toString(paramValues));
String message = String.format("paramNames must have the same length as paramValues. Names: %s Values: %s",
Arrays.toString(paramNames), Arrays.toString(paramValues));
throw new IllegalArgumentException(message);
}

String[] splittedPatterns = StringUtils.extractParameters(originalPattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.sort;

import java.io.Serializable;
import java.util.Comparator;
import java.util.List;

Expand Down Expand Up @@ -102,12 +103,14 @@ public <T> Serializer from(T object, String alias) {
* @author A.C de Souza
* @since 3.4.0
*/
static final class ApplicationPackageFirst implements Comparator<Serialization> {
static final class ApplicationPackageFirst implements Comparator<Serialization>, Serializable {

public static final long serialVersionUID = 1L;

private static final String VRAPTOR_PACKAGE = "br.com.caelum.vraptor.serialization";

private int priority(Serialization s) {
return s.getClass().getPackage().getName().startsWith(VRAPTOR_PACKAGE)? 1 : 0;
return s.getClass().getPackage().getName().startsWith(VRAPTOR_PACKAGE) ? 1 : 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.InputStreamReader;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;
Expand Down Expand Up @@ -156,7 +157,7 @@ public Object[] deserialize(InputStream inputStream, ControllerMethod method) {
}

private String getContentOfStream(InputStream input) throws IOException {
String charset = getRequestCharset();
Charset charset = Charset.forName(getRequestCharset());
logger.debug("Using charset {}", charset);

return CharStreams.toString(new InputStreamReader(input, charset));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package br.com.caelum.vraptor.validator;

import java.io.Serializable;
import java.util.ResourceBundle;

import javax.enterprise.inject.Vetoed;
Expand All @@ -29,8 +30,10 @@
* @since 3.4.0
*/
@Vetoed
public class I18nParam {

public class I18nParam implements Serializable {

public static final long serialVersionUID = 1L;

private final String key;

public I18nParam(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package br.com.caelum.vraptor.view;

import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -99,8 +100,8 @@ private String chooseMimeType(String acceptHeader) {
}

private static class MimeType implements Comparable<MimeType> {
String type;
double qualifier;
private final String type;
private final double qualifier;

public MimeType(String type, double qualifier) {
this.type = type;
Expand All @@ -113,6 +114,21 @@ public int compareTo(MimeType mime) {
return Double.compare(mime.qualifier, this.qualifier);
}

@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}

MimeType other = (MimeType) obj;
return Objects.equals(type, other.type) && Objects.equals(qualifier, other.qualifier);
}

@Override
public int hashCode() {
return Objects.hash(type, qualifier);
}

public String getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.util.Collections.sort;
import static javassist.CtNewMethod.abstractMethod;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -210,7 +211,9 @@ private List<Method> getMethods(Class<?> controller) {
return methods;
}

private final class SortByArgumentsLengthDesc implements Comparator<Method> {
private static final class SortByArgumentsLengthDesc implements Comparator<Method>, Serializable {
public static final long serialVersionUID = 1L;

@Override
public int compare(Method o1, Method o2) {
return Integer.compare(o2.getParameterTypes().length, o1.getParameterTypes().length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

package br.com.caelum.vraptor.http;

import static java.util.Collections.enumeration;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.when;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -43,11 +44,11 @@ public class VRaptorRequestTest {
public void setup() {
MockitoAnnotations.initMocks(this);

final Hashtable<String, String[]> t = new Hashtable<>();
final Map<String, String[]> t = new HashMap<>();
t.put("name", new String[] { "guilherme" });
t.put("age", new String[] { "27" });

when(request.getParameterNames()).thenReturn(t.keys());
when(request.getParameterNames()).thenReturn(enumeration(t.keySet()));
when(request.getParameterMap()).thenReturn(t);
when(request.getParameter("name")).thenReturn("guilherme");
when(request.getParameter("minimum")).thenReturn(null);
Expand Down

0 comments on commit 414fb6f

Please sign in to comment.