|
| 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 | + */ |
1 | 16 | package org.springframework.data.rest.convert;
|
2 | 17 |
|
3 | 18 | import java.util.Stack;
|
4 | 19 |
|
5 | 20 | import org.springframework.core.convert.ConversionService;
|
6 | 21 | import org.springframework.core.convert.ConverterNotFoundException;
|
7 | 22 | import org.springframework.core.convert.TypeDescriptor;
|
| 23 | +import org.springframework.util.Assert; |
8 | 24 |
|
9 | 25 | /**
|
10 | 26 | * This {@link ConversionService} implementation delegates the actual conversion to the {@literal ConversionService} it
|
11 | 27 | * finds in its internal {@link Stack} that claims to be able to convert a given class. It will roll through the
|
12 | 28 | * {@literal ConversionService}s until it finds one that can convert the given type.
|
13 |
| - * |
| 29 | + * |
14 | 30 | * @author Jon Brisbin
|
| 31 | + * @authot Oliver Gierke |
15 | 32 | */
|
16 | 33 | public class DelegatingConversionService implements ConversionService {
|
17 | 34 |
|
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 | + } |
92 | 107 |
|
| 108 | + throw new ConverterNotFoundException(from, to); |
| 109 | + } |
93 | 110 | }
|
0 commit comments