Skip to content

Commit

Permalink
Addresses most of the problems in #3806 (casting to pair)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed May 30, 2023
1 parent 355a0d5 commit 9b09614
Showing 1 changed file with 45 additions and 49 deletions.
94 changes: 45 additions & 49 deletions msi.gama.core/src/msi/gaml/types/GamaPairType.java
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* GamaPairType.java, in msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v.1.9.2).
* 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;

Expand All @@ -21,8 +21,8 @@
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaListFactory;
import msi.gama.util.GamaPair;
import msi.gama.util.IList;
import msi.gama.util.IMap;
import msi.gaml.expressions.IExpression;

/**
* Written by drogoul Modified on 1 ao�t 2010
Expand All @@ -47,20 +47,24 @@ public GamaPair cast(final IScope scope, final Object obj, final Object param, f
}

@Override
public int getNumberOfParameters() {
return 2;
}
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
* @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
* @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 {
Expand All @@ -70,40 +74,16 @@ public static GamaPair staticCast(final IScope scope, final Object obj, final IT
value = ((GamaPair) obj).value;
} else
// 8/01/14: No more automatic casting between points and pairs (as
// points can now have 3 coordinates
// if ( obj instanceof GamaPoint ) { return new GamaPair(((GamaPoint)
// obj).x, ((GamaPoint) obj).y); }
// 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) {
final IMap m = (IMap) obj;
} 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 if (obj instanceof IList) {
final IList l = (IList) obj;
switch (l.size()) {
case 0:
key = null;
value = null;
break;
case 1:
key = l.get(0);
value = l.get(0);
break;
case 2:
key = l.get(0);
value = l.get(1);
break;
default:
key = l;
value = l;
}

} else {
// 8/01/14 : Change of behavior for the default pair: now returns a
// pair object::object
// 8/01/14 : Change of behavior: now returns a pair object::object
key = obj;
value = obj;
}
Expand All @@ -113,23 +93,39 @@ public static GamaPair staticCast(final IScope scope, final Object obj, final IT
}

@Override
public GamaPair getDefault() {
return new GamaPair(null, null, Types.NO_TYPE, Types.NO_TYPE);
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 getContentType() {
return Types.get(NONE);
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;
}
//
// @Override
// public boolean hasContents() {
// return true;
// }

}

0 comments on commit 9b09614

Please sign in to comment.