| 1 |
|
package uk.co.badgersinfoil.asxsd; |
| 2 |
|
|
| 3 |
|
import java.util.HashMap; |
| 4 |
|
import java.util.Map; |
| 5 |
|
import org.eclipse.xsd.XSDConcreteComponent; |
| 6 |
|
|
| 7 |
20 |
public class MappingFunctionStore { |
| 8 |
20 |
private Map store = new HashMap(); |
| 9 |
|
|
| 10 |
|
public MappingFunction get(CodegenRole role, |
| 11 |
|
XSDConcreteComponent component) |
| 12 |
|
{ |
| 13 |
186 |
Key key = new Key(role, component); |
| 14 |
186 |
return (MappingFunction)store.get(key); |
| 15 |
|
} |
| 16 |
|
|
| 17 |
|
public void put(CodegenRole role, |
| 18 |
|
XSDConcreteComponent component, |
| 19 |
|
MappingFunction fn) |
| 20 |
|
{ |
| 21 |
161 |
Key key = new Key(role, component); |
| 22 |
161 |
if (store.containsKey(key)) { |
| 23 |
0 |
throw new IllegalArgumentException("Function already defined for "+role+"+"+component); |
| 24 |
|
} |
| 25 |
161 |
store.put(key, fn); |
| 26 |
161 |
} |
| 27 |
|
|
| 28 |
20 |
private static class Key { |
| 29 |
|
private CodegenRole role; |
| 30 |
|
private XSDConcreteComponent component; |
| 31 |
347 |
public Key(CodegenRole role, XSDConcreteComponent component) { |
| 32 |
347 |
this.role = role; |
| 33 |
347 |
this.component = component; |
| 34 |
347 |
} |
| 35 |
|
public int hashCode() { |
| 36 |
508 |
final int PRIME = 31; |
| 37 |
508 |
int result = 1; |
| 38 |
508 |
result = PRIME * result + ((component == null) ? 0 : component.hashCode()); |
| 39 |
508 |
result = PRIME * result + ((role == null) ? 0 : role.hashCode()); |
| 40 |
508 |
return result; |
| 41 |
|
} |
| 42 |
|
public boolean equals(Object obj) { |
| 43 |
25 |
if (this == obj) |
| 44 |
0 |
return true; |
| 45 |
25 |
if (obj == null) |
| 46 |
0 |
return false; |
| 47 |
25 |
if (getClass() != obj.getClass()) |
| 48 |
0 |
return false; |
| 49 |
25 |
final Key other = (Key) obj; |
| 50 |
25 |
if (component == null) { |
| 51 |
0 |
if (other.component != null) |
| 52 |
0 |
return false; |
| 53 |
25 |
} else if (!component.equals(other.component)) |
| 54 |
0 |
return false; |
| 55 |
25 |
if (role == null) { |
| 56 |
0 |
if (other.role != null) |
| 57 |
0 |
return false; |
| 58 |
25 |
} else if (!role.equals(other.role)) |
| 59 |
0 |
return false; |
| 60 |
25 |
return true; |
| 61 |
|
} |
| 62 |
|
} |
| 63 |
|
} |