diff --git a/msi.gama.core/src/msi/gaml/types/GamaPairType.java b/msi.gama.core/src/msi/gaml/types/GamaPairType.java index ad4dd57ccc..a98a67c0ec 100644 --- a/msi.gama.core/src/msi/gaml/types/GamaPairType.java +++ b/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; @@ -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 @@ -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 { @@ -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; } @@ -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; - // } }