1 /*
2  * Copyright 2011 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  */

16 package org.modelmapper.internal;
17
18 /**
19  * @author Jonathan Halterman
20  */

21 class TypePair<S, D> {
22   private final Class<S> sourceType;
23   private final Class<D> destinationType;
24   private final String name;
25   private final int hashCode;
26
27   private TypePair(Class<S> sourceType, Class<D> destinationType, String name) {
28     this.sourceType = sourceType;
29     this.destinationType = destinationType;
30     this.name = name;
31     hashCode = computeHashCode();
32   }
33
34   static <T1, T2> TypePair<T1, T2> of(Class<T1> sourceType, Class<T2> destinationType, String name) {
35     return new TypePair<T1, T2>(sourceType, destinationType, name);
36   }
37
38   @Override
39   public boolean equals(Object other) {
40     if (other == this)
41       return true;
42     if (!(other instanceof TypePair<?, ?>))
43       return false;
44     TypePair<?, ?> otherPair = (TypePair<?, ?>) other;
45     if (name == null) {
46       if (otherPair.name != null)
47         return false;
48     } else if (!name.equals(otherPair.name))
49       return false;
50     return sourceType.equals(otherPair.sourceType)
51         && destinationType.equals(otherPair.destinationType);
52   }
53
54   @Override
55   public final int hashCode() {
56     return hashCode;
57   }
58
59   @Override
60   public String toString() {
61     String str = sourceType.getName() + " to " + destinationType.getName();
62     if (name != null)
63       str += " as " + name;
64     return str;
65   }
66
67   Class<D> getDestinationType() {
68     return destinationType;
69   }
70
71   Class<S> getSourceType() {
72     return sourceType;
73   }
74
75   private int computeHashCode() {
76     final int prime = 31;
77     int result = 1;
78     result = prime * result + sourceType.hashCode();
79     result = prime * result + destinationType.hashCode();
80     result = prime * result + ((name == null) ? 0 : name.hashCode());
81     return result;
82   }
83 }