Skip to content

Commit 5c61632

Browse files
committed
DATAREST-93 - Fixed formatting in Spring Data REST.
Added formatter to be used within Eclipse going forward.
1 parent eec5247 commit 5c61632

File tree

162 files changed

+4047
-4263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+4047
-4263
lines changed

formatter.xml

+291
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,110 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.rest.convert;
217

318
import java.util.Stack;
419

520
import org.springframework.core.convert.ConversionService;
621
import org.springframework.core.convert.ConverterNotFoundException;
722
import org.springframework.core.convert.TypeDescriptor;
23+
import org.springframework.util.Assert;
824

925
/**
1026
* This {@link ConversionService} implementation delegates the actual conversion to the {@literal ConversionService} it
1127
* finds in its internal {@link Stack} that claims to be able to convert a given class. It will roll through the
1228
* {@literal ConversionService}s until it finds one that can convert the given type.
13-
*
29+
*
1430
* @author Jon Brisbin
31+
* @authot Oliver Gierke
1532
*/
1633
public class DelegatingConversionService implements ConversionService {
1734

18-
private Stack<ConversionService> conversionServices = new Stack<ConversionService>();
19-
20-
public DelegatingConversionService() {
21-
}
22-
23-
public DelegatingConversionService(ConversionService... svcs) {
24-
addConversionServices(svcs);
25-
}
26-
27-
/**
28-
* Add {@link ConversionService}s to the internal list of those to delegate to.
29-
*
30-
* @param svcs
31-
* The ConversionServices to delegate to (in order).
32-
*
33-
* @return @this
34-
*/
35-
public DelegatingConversionService addConversionServices(ConversionService... svcs) {
36-
for(ConversionService svc : svcs) {
37-
conversionServices.add(svc);
38-
}
39-
return this;
40-
}
41-
42-
/**
43-
* Add a {@link ConversionService} to the internal list at a specific index for controlling the priority.
44-
*
45-
* @param atIndex
46-
* Where in the stack to add this ConversionService.
47-
* @param svc
48-
* The ConversionService to add.
49-
*
50-
* @return
51-
*/
52-
public DelegatingConversionService addConversionService(int atIndex, ConversionService svc) {
53-
conversionServices.add(atIndex, svc);
54-
return this;
55-
}
56-
57-
@Override public boolean canConvert(Class<?> from, Class<?> to) {
58-
for(ConversionService svc : conversionServices) {
59-
if(svc.canConvert(from, to)) {
60-
return true;
61-
}
62-
}
63-
return false;
64-
}
65-
66-
@Override public boolean canConvert(TypeDescriptor from, TypeDescriptor to) {
67-
for(ConversionService svc : conversionServices) {
68-
if(svc.canConvert(from, to)) {
69-
return true;
70-
}
71-
}
72-
return false;
73-
}
74-
75-
@Override public <T> T convert(Object o, Class<T> type) {
76-
for(ConversionService svc : conversionServices) {
77-
if(svc.canConvert(o.getClass(), type)) {
78-
return svc.convert(o, type);
79-
}
80-
}
81-
throw new ConverterNotFoundException(TypeDescriptor.forObject(o), TypeDescriptor.valueOf(type));
82-
}
83-
84-
@Override public Object convert(Object o, TypeDescriptor from, TypeDescriptor to) {
85-
for(ConversionService svc : conversionServices) {
86-
if(svc.canConvert(from, to)) {
87-
return svc.convert(o, from, to);
88-
}
89-
}
90-
throw new ConverterNotFoundException(from, to);
91-
}
35+
private final Stack<ConversionService> conversionServices;
36+
37+
public DelegatingConversionService(ConversionService... svcs) {
38+
39+
this.conversionServices = new Stack<ConversionService>();
40+
41+
for (ConversionService svc : svcs) {
42+
Assert.notNull(svc);
43+
conversionServices.add(svc);
44+
}
45+
}
46+
47+
/*
48+
* (non-Javadoc)
49+
* @see org.springframework.core.convert.ConversionService#canConvert(java.lang.Class, java.lang.Class)
50+
*/
51+
@Override
52+
public boolean canConvert(Class<?> from, Class<?> to) {
53+
54+
for (ConversionService svc : conversionServices) {
55+
if (svc.canConvert(from, to)) {
56+
return true;
57+
}
58+
}
59+
60+
return false;
61+
}
62+
63+
/*
64+
* (non-Javadoc)
65+
* @see org.springframework.core.convert.ConversionService#canConvert(org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
66+
*/
67+
@Override
68+
public boolean canConvert(TypeDescriptor from, TypeDescriptor to) {
69+
70+
for (ConversionService svc : conversionServices) {
71+
if (svc.canConvert(from, to)) {
72+
return true;
73+
}
74+
}
75+
76+
return false;
77+
}
78+
79+
/*
80+
* (non-Javadoc)
81+
* @see org.springframework.core.convert.ConversionService#convert(java.lang.Object, java.lang.Class)
82+
*/
83+
@Override
84+
public <T> T convert(Object o, Class<T> type) {
85+
86+
for (ConversionService svc : conversionServices) {
87+
if (svc.canConvert(o.getClass(), type)) {
88+
return svc.convert(o, type);
89+
}
90+
}
91+
92+
throw new ConverterNotFoundException(TypeDescriptor.forObject(o), TypeDescriptor.valueOf(type));
93+
}
94+
95+
/*
96+
* (non-Javadoc)
97+
* @see org.springframework.core.convert.ConversionService#convert(java.lang.Object, org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
98+
*/
99+
@Override
100+
public Object convert(Object o, TypeDescriptor from, TypeDescriptor to) {
101+
102+
for (ConversionService svc : conversionServices) {
103+
if (svc.canConvert(from, to)) {
104+
return svc.convert(o, from, to);
105+
}
106+
}
92107

108+
throw new ConverterNotFoundException(from, to);
109+
}
93110
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.rest.convert;
217

318
import java.text.DateFormat;
@@ -15,62 +30,81 @@
1530
/**
1631
* @author Jon Brisbin
1732
*/
18-
public class ISO8601DateConverter implements ConditionalGenericConverter,
19-
Converter<String[], Date> {
20-
21-
public static final ConditionalGenericConverter INSTANCE = new ISO8601DateConverter();
22-
23-
private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS = new HashSet<ConvertiblePair>();
24-
25-
static {
26-
CONVERTIBLE_PAIRS.add(new ConvertiblePair(String.class, Date.class));
27-
CONVERTIBLE_PAIRS.add(new ConvertiblePair(Date.class, String.class));
28-
}
29-
30-
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
31-
if(String.class.isAssignableFrom(sourceType.getType())) {
32-
return Date.class.isAssignableFrom(targetType.getType());
33-
}
34-
35-
return Date.class.isAssignableFrom(sourceType.getType())
36-
&& String.class.isAssignableFrom(targetType.getType());
37-
}
38-
39-
@Override public Set<ConvertiblePair> getConvertibleTypes() {
40-
return CONVERTIBLE_PAIRS;
41-
}
42-
43-
@Override public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
44-
DateFormat dateFmt = iso8601DateFormat();
45-
if(String.class.isAssignableFrom(sourceType.getType())) {
46-
return dateFmt.format(source);
47-
} else {
48-
try {
49-
return dateFmt.parse(source.toString());
50-
} catch(ParseException e) {
51-
throw new ConversionFailedException(sourceType, targetType, source, e);
52-
}
53-
}
54-
}
55-
56-
@Override public Date convert(String[] source) {
57-
if(source.length > 0) {
58-
try {
59-
return iso8601DateFormat().parse(source[0]);
60-
} catch(ParseException e) {
61-
throw new ConversionFailedException(
62-
TypeDescriptor.valueOf(String[].class),
63-
TypeDescriptor.valueOf(Date.class),
64-
source[0],
65-
new IllegalArgumentException("Source does not conform to ISO8601 date format (YYYY-MM-DDTHH:MM:SS-0000")
66-
);
67-
}
68-
}
69-
return null;
70-
}
71-
72-
private DateFormat iso8601DateFormat() {
73-
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
74-
}
33+
public class ISO8601DateConverter implements ConditionalGenericConverter, Converter<String[], Date> {
34+
35+
public static final ConditionalGenericConverter INSTANCE = new ISO8601DateConverter();
36+
37+
private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS = new HashSet<ConvertiblePair>();
38+
39+
static {
40+
CONVERTIBLE_PAIRS.add(new ConvertiblePair(String.class, Date.class));
41+
CONVERTIBLE_PAIRS.add(new ConvertiblePair(Date.class, String.class));
42+
}
43+
44+
/*
45+
* (non-Javadoc)
46+
* @see org.springframework.core.convert.converter.ConditionalConverter#matches(org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
47+
*/
48+
@Override
49+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
50+
51+
if (String.class.isAssignableFrom(sourceType.getType())) {
52+
return Date.class.isAssignableFrom(targetType.getType());
53+
}
54+
55+
return Date.class.isAssignableFrom(sourceType.getType()) && String.class.isAssignableFrom(targetType.getType());
56+
}
57+
58+
/*
59+
* (non-Javadoc)
60+
* @see org.springframework.core.convert.converter.GenericConverter#getConvertibleTypes()
61+
*/
62+
@Override
63+
public Set<ConvertiblePair> getConvertibleTypes() {
64+
return CONVERTIBLE_PAIRS;
65+
}
66+
67+
/*
68+
* (non-Javadoc)
69+
* @see org.springframework.core.convert.converter.GenericConverter#convert(java.lang.Object, org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
70+
*/
71+
@Override
72+
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
73+
74+
DateFormat dateFmt = iso8601DateFormat();
75+
76+
if (String.class.isAssignableFrom(sourceType.getType())) {
77+
return dateFmt.format(source);
78+
}
79+
80+
try {
81+
return dateFmt.parse(source.toString());
82+
} catch (ParseException e) {
83+
throw new ConversionFailedException(sourceType, targetType, source, e);
84+
}
85+
}
86+
87+
/*
88+
* (non-Javadoc)
89+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
90+
*/
91+
@Override
92+
public Date convert(String[] source) {
93+
94+
if (source.length == 0) {
95+
return null;
96+
}
97+
98+
try {
99+
return iso8601DateFormat().parse(source[0]);
100+
} catch (ParseException e) {
101+
throw new ConversionFailedException(TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(Date.class),
102+
source[0], new IllegalArgumentException(
103+
"Source does not conform to ISO8601 date format (YYYY-MM-DDTHH:MM:SS-0000"));
104+
}
105+
}
75106

107+
private DateFormat iso8601DateFormat() {
108+
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
109+
}
76110
}

0 commit comments

Comments
 (0)