01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.validator.internal;
16:
17: import org.strecks.util.Assert;
18:
19: /**
20: * Used internally to arrange a set of <code>Validators</code> according to their position in an
21: * assignment hierarchy of their generic parameterization type. Used only for
22: * <code>Validators</code> which use the converted value, that is, rely on converters
23: * @author Phil Zoio
24: */
25: public class SortableValidator implements Comparable<SortableValidator> {
26:
27: private ValidatorWrapper wrapper;
28:
29: public SortableValidator(ValidatorWrapper wrapper) {
30: super ();
31: Assert.notNull(wrapper);
32: Assert.notNull(wrapper.getParameterizedType());
33: this .wrapper = wrapper;
34: }
35:
36: public int compareTo(SortableValidator o) {
37:
38: ValidatorWrapper otherWrapper = o.getWrapper();
39: Class<?> this Type = wrapper.getParameterizedType();
40: Class<?> otherType = otherWrapper.getParameterizedType();
41:
42: if (this Type.equals(otherType))
43: return 0;
44:
45: // whether this type is assignable to other type
46: boolean assignableTo = (otherType.isAssignableFrom(this Type));
47:
48: // whether other type is assignable from other type
49: boolean assignableForm = (this Type.isAssignableFrom(otherType));
50:
51: // if assignable to then should come afterwards
52: if (assignableTo)
53: return -1;
54: if (assignableForm)
55: return 1;
56:
57: return 0;
58: }
59:
60: public ValidatorWrapper getWrapper() {
61: return wrapper;
62: }
63:
64: }
|