package dyvil.tools.compiler.util;

import dyvil.tools.compiler.ast.member.IMember;
import dyvil.tools.compiler.ast.method.IMethod;
import dyvil.tools.compiler.ast.type.IType;
import dyvil.tools.compiler.ast.type.builtin.Types;
import dyvil.tools.parsing.Name;

import java.util.Comparator;

public class MemberSorter
{
	public static final Comparator<? super IMember> MEMBER_COMPARATOR = MemberSorter::compareMembers;
	public static final Comparator<? super IMethod> METHOD_COMPARATOR = MemberSorter::compareMethods;

	public static int compareNames(Name name1, Name name2)
	{
		if (Util.startsWithSymbol(name1))
		{
			if (!Util.startsWithSymbol(name2))
			{
				return -1;
			}
		}
		else if (Util.startsWithSymbol(name2))
		{
			if (!Util.startsWithSymbol(name1))
			{
				return 1;
			}
		}

		return name1.qualified.compareToIgnoreCase(name2.qualified);
	}

	public static int compareMembers(IMember member1, IMember member2)
	{
		return compareNames(member1.getName(), member2.getName());
	}

	public static int compareMethods(IMethod method1, IMethod method2)
	{
		final int n = compareMembers(method1, method2);
		if (n != 0)
		{
			return n;
		}
		if (method1.checkOverride(method2, null))
		{
			return compareTypes(method1.getType(), method2.getType());
		}

		return method1.getDescriptor().compareTo(method2.getDescriptor());
	}

	private static int compareTypes(IType type1, IType type2)
	{
		if (Types.isSameType(type1, type2))
		{
			return 0;
		}
		if (Types.isSuperType(type1, type2))
		{
			return 1;
		}
		if (Types.isSuperType(type2, type1))
		{
			return -1;
		}
		return 0;
	}
}

@@ -575,227 +575,8 @@
 %0A%09%7B%0A
-%09%09if (Util.startsWithSymbol(name1))%0A%09%09%7B%0A%09%09%09if (!Util.startsWithSymbol(name2))%0A%09%09%09%7B%0A%09%09%09%09return -1;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else if (Util.startsWithSymbol(name2))%0A%09%09%7B%0A%09%09%09if (!Util.startsWithSymbol(name1))%0A%09%09%09%7B%0A%09%09%09%09return 1;%0A%09%09%09%7D%0A%09%09%7D%0A%0A
 %09%09re

Improve MemberSorter