1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package signature.io.html; 18 19 import signature.compare.model.IExecutableMemberDelta; 20 import signature.model.IArrayType; 21 import signature.model.IClassDefinition; 22 import signature.model.IClassReference; 23 import signature.model.IExecutableMember; 24 import signature.model.IParameter; 25 import signature.model.IParameterizedType; 26 import signature.model.IPrimitiveType; 27 import signature.model.ITypeDefinition; 28 import signature.model.ITypeReference; 29 import signature.model.ITypeVariableDefinition; 30 import signature.model.ITypeVariableReference; 31 32 import java.util.Comparator; 33 import java.util.Iterator; 34 import java.util.List; 35 36 public class ExecutableMemberComparator implements 37 Comparator<IExecutableMemberDelta<? extends IExecutableMember>> { 38 39 public int compare(IExecutableMemberDelta<? extends IExecutableMember> a, 40 IExecutableMemberDelta<? extends IExecutableMember> b) { 41 assert a.getType() == b.getType(); 42 IExecutableMember aMember = null; 43 IExecutableMember bMember = null; 44 45 if (a.getFrom() != null) { 46 aMember = a.getFrom(); 47 bMember = b.getFrom(); 48 } else { 49 aMember = a.getTo(); 50 bMember = b.getTo(); 51 } 52 53 int returnValue = aMember.getName().compareTo(bMember.getName()); 54 return returnValue != 0 ? returnValue : compareParameterLists(aMember 55 .getParameters(), bMember.getParameters()); 56 } 57 58 private int compareParameterLists(List<IParameter> a, List<IParameter> b) { 59 if (a.size() != b.size()) { 60 return a.size() - b.size(); 61 } 62 Iterator<IParameter> aIt = a.iterator(); 63 Iterator<IParameter> bIt = b.iterator(); 64 int returnValue = 0; 65 while (aIt.hasNext() && bIt.hasNext()) { 66 returnValue += getTypeName(aIt.next().getType()).compareTo( 67 getTypeName(bIt.next().getType())); 68 } 69 return returnValue; 70 } 71 72 private String getTypeName(ITypeReference type) { 73 if (type instanceof IClassReference) { 74 return getTypeName(((IClassReference) type).getClassDefinition()); 75 } 76 if (type instanceof ITypeVariableReference) { 77 return getTypeName(((ITypeVariableReference) type) 78 .getTypeVariableDefinition()); 79 } 80 if (type instanceof IParameterizedType) { 81 return getTypeName(((IParameterizedType) type).getRawType()); 82 } 83 if (type instanceof IArrayType) { 84 return getTypeName(((IArrayType) type).getComponentType()); 85 } 86 if (type instanceof IPrimitiveType) { 87 return ((IPrimitiveType) type).getName(); 88 } 89 return type.toString(); 90 } 91 92 private String getTypeName(ITypeDefinition type) { 93 if (type instanceof IClassDefinition) { 94 return ((IClassDefinition) type).getName(); 95 } 96 if (type instanceof ITypeVariableDefinition) { 97 return ((ITypeVariableDefinition) type).getName(); 98 } 99 return type.toString(); 100 } 101 102 } 103