-
Notifications
You must be signed in to change notification settings - Fork 99
/
GamaPairType.java
131 lines (119 loc) · 4.14 KB
/
GamaPairType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*******************************************************************************************************
*
* GamaPairType.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform
* (v.1.9.2).
*
* (c) 2007-2023 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package msi.gaml.types;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.metamodel.shape.DynamicLineString;
import msi.gama.metamodel.shape.GamaShape;
import msi.gama.precompiler.GamlAnnotations.doc;
import msi.gama.precompiler.GamlAnnotations.type;
import msi.gama.precompiler.IConcept;
import msi.gama.precompiler.ISymbolKind;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaListFactory;
import msi.gama.util.GamaPair;
import msi.gama.util.IMap;
import msi.gaml.expressions.IExpression;
/**
* Written by drogoul Modified on 1 ao�t 2010
*
* @todo Description
*
*/
@type (
name = IKeyword.PAIR,
id = IType.PAIR,
wraps = { GamaPair.class },
kind = ISymbolKind.Variable.REGULAR,
concept = { IConcept.TYPE, IConcept.CONTAINER },
doc = @doc ("Represents a pair of 2 arbitrary elements"))
@SuppressWarnings ({ "unchecked", "rawtypes" })
public class GamaPairType extends GamaContainerType<GamaPair> {
@Override
public GamaPair cast(final IScope scope, final Object obj, final Object param, final IType keyType,
final IType contentsType, final boolean copy) {
return staticCast(scope, obj, keyType, contentsType, copy);
}
@Override
public int getNumberOfParameters() { return 2; }
/**
* Static cast.
*
* @param scope
* the scope
* @param obj
* the obj
* @param keyType
* the key type
* @param contentsType
* the contents type
* @param copy
* the copy
* @return the gama pair
* @throws GamaRuntimeException
* the gama runtime exception
*/
public static GamaPair staticCast(final IScope scope, final Object obj, final IType keyType,
final IType contentsType, final boolean copy) throws GamaRuntimeException {
Object key, value;
if (obj instanceof GamaPair) {
key = ((GamaPair) obj).key;
value = ((GamaPair) obj).value;
} else
// 8/01/14: No more automatic casting between points and pairs (as
// points can have 3 coordinates)
if (obj instanceof GamaShape && ((GamaShape) obj).getInnerGeometry() instanceof DynamicLineString) {
final DynamicLineString g = (DynamicLineString) ((GamaShape) obj).getInnerGeometry();
key = g.getSource();
value = g.getTarget();
} else if (obj instanceof IMap m) {
key = GamaListFactory.create(scope, m.getGamlType().getKeyType(), m.keySet());
value = GamaListFactory.create(scope, m.getGamlType().getContentType(), m.values());
} else {
// 8/01/14 : Change of behavior: now returns a pair object::object
key = obj;
value = obj;
}
final IType kt = keyType == null || keyType == Types.NO_TYPE ? GamaType.of(key) : keyType;
final IType ct = contentsType == null || contentsType == Types.NO_TYPE ? GamaType.of(value) : contentsType;
return new GamaPair(toType(scope, key, kt, copy), toType(scope, value, ct, copy), kt, ct);
}
@Override
public IType keyTypeIfCasting(final IExpression exp) {
final IType itemType = exp.getGamlType();
switch (itemType.id()) {
case PAIR:
return itemType.getKeyType();
case MAP:
return Types.LIST.of(itemType.getKeyType());
}
return itemType;
}
@Override
public IType contentsTypeIfCasting(final IExpression exp) {
final IType itemType = exp.getGamlType();
switch (itemType.id()) {
case MAP:
return Types.LIST.of(itemType.getContentType());
case PAIR:
return itemType.getContentType();
}
return itemType;
}
@Override
public GamaPair getDefault() { return new GamaPair(null, null, Types.NO_TYPE, Types.NO_TYPE); }
@Override
public IType getContentType() { return Types.get(NONE); }
@Override
public boolean canCastToConst() {
return true;
}
}