|
21 | 21 | import com.laytonsmith.core.objects.ObjectDefinitionTable; |
22 | 22 | import com.laytonsmith.core.objects.UserObject; |
23 | 23 | import java.util.Arrays; |
| 24 | +import java.util.Collection; |
24 | 25 | import java.util.Comparator; |
25 | 26 | import java.util.HashMap; |
26 | 27 | import java.util.HashSet; |
@@ -786,4 +787,48 @@ public boolean doesExtend(Environment env, CClassType superClass) { |
786 | 787 | public static CClassType getFromGenericTypeName(String genericTypeName, Target t) { |
787 | 788 | throw new UnsupportedOperationException("Not yet implemented"); |
788 | 789 | } |
| 790 | + |
| 791 | + /** |
| 792 | + * Get the most specific type for a collection of types. This is the type for which holds that all types are |
| 793 | + * instance of this type. None (Java {@code null}) types are ignored. |
| 794 | + * When {@link CVoid#TYPE} is present in the types, it is returned. |
| 795 | + * Note that the intention of this method is to be replaced by a variant that returns a multi-type instead. |
| 796 | + * @param types - The types. |
| 797 | + * @param defaultType - The default type. |
| 798 | + * @param env - The environment. |
| 799 | + * @return The most specific type, or the provided default type if no such type exists. |
| 800 | + */ |
| 801 | + public static CClassType getMostSpecificType( |
| 802 | + Collection<CClassType> types, CClassType defaultType, Environment env) { |
| 803 | + // TODO - Change this method to return a multi-type based on the passed types when the type system allows this. |
| 804 | + search: |
| 805 | + for(CClassType type1 : types) { |
| 806 | + if(type1 != null) { |
| 807 | + if(type1.equals(CVoid.TYPE)) { |
| 808 | + return CVoid.TYPE; |
| 809 | + } |
| 810 | + for(CClassType type2 : types) { |
| 811 | + if(type2 != null && !InstanceofUtil.isInstanceof(type2, type1, env)) { |
| 812 | + continue search; |
| 813 | + } |
| 814 | + } |
| 815 | + return type1; |
| 816 | + } |
| 817 | + } |
| 818 | + return defaultType; |
| 819 | + } |
| 820 | + |
| 821 | + /** |
| 822 | + * Get the most specific type for a collection of types. This is the type for which holds that all types are |
| 823 | + * instance of this type. None (Java {@code null}) types are ignored. |
| 824 | + * When {@link CVoid#TYPE} is present in the types, it is returned. |
| 825 | + * Note that the intention of this method is to be replaced by a variant that returns a multi-type instead. |
| 826 | + * @param types - The types. |
| 827 | + * @param env - The environment. |
| 828 | + * @return The most specific type, or {@link CClassType#AUTO} if no such type exists. |
| 829 | + */ |
| 830 | + public static CClassType getMostSpecificType(Collection<CClassType> types, Environment env) { |
| 831 | + // TODO - Change this method to return a multi-type based on the passed types when the type system allows this. |
| 832 | + return getMostSpecificType(types, CClassType.AUTO, env); |
| 833 | + } |
789 | 834 | } |
0 commit comments