| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2007, 2008 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.pde.api.tools.internal.comparator; |
| 12 | |
| 13 | class SignatureDescriptor { |
| 14 | private static final TypeParameterDescriptor[] EMPTY_TYPE_PARAMETER_DESCRIPTORS = new TypeParameterDescriptor[0]; |
| 15 | private static final String[] EMPTY_TYPE_ARGUMENTS = new String[0]; |
| 16 | static final int INITIAL_SIZE = 1; |
| 17 | |
| 18 | TypeParameterDescriptor currentTypeParameterDescriptor; |
| 19 | String superClass; |
| 20 | TypeParameterDescriptor[] typeParameterDescriptors; |
| 21 | int typeParameterDescriptorsCounter; |
| 22 | String[] typeArguments; |
| 23 | int typeArgumentsCounter; |
| 24 | |
| 25 | public SignatureDescriptor() { |
| 26 | } |
| 27 | |
| 28 | public void addInterfaceBound(String bound) { |
| 29 | this.currentTypeParameterDescriptor.addInterfaceBound(bound); |
| 30 | } |
| 31 | |
| 32 | public void addTypeArgument(String typeArgument) { |
| 33 | if (this.typeArguments == null) { |
| 34 | this.typeArguments = new String[INITIAL_SIZE]; |
| 35 | this.typeArgumentsCounter = 0; |
| 36 | } else { |
| 37 | int length = typeArguments.length; |
| 38 | if (length == this.typeArgumentsCounter) { |
| 39 | // resize |
| 40 | System.arraycopy(this.typeArguments, 0, (this.typeArguments = new String[length * 2]), 0, length); |
| 41 | } |
| 42 | } |
| 43 | this.typeArguments[this.typeArgumentsCounter++] = typeArgument; |
| 44 | } |
| 45 | |
| 46 | public void addTypeParameterDescriptor(String name) { |
| 47 | if (this.typeParameterDescriptors == null) { |
| 48 | this.typeParameterDescriptors = new TypeParameterDescriptor[INITIAL_SIZE]; |
| 49 | this.typeParameterDescriptorsCounter = 0; |
| 50 | } else { |
| 51 | int length = this.typeParameterDescriptors.length; |
| 52 | if (this.typeParameterDescriptorsCounter == length) { |
| 53 | System.arraycopy(this.typeParameterDescriptors, 0, (this.typeParameterDescriptors = new TypeParameterDescriptor[length * 2]), 0, length); |
| 54 | } |
| 55 | } |
| 56 | TypeParameterDescriptor typeParameterDescriptor = new TypeParameterDescriptor(name); |
| 57 | this.currentTypeParameterDescriptor = typeParameterDescriptor; |
| 58 | this.typeParameterDescriptors[this.typeParameterDescriptorsCounter++] = typeParameterDescriptor; |
| 59 | } |
| 60 | |
| 61 | public TypeParameterDescriptor[] getTypeParameterDescriptors() { |
| 62 | if (this.typeParameterDescriptors == null) { |
| 63 | return EMPTY_TYPE_PARAMETER_DESCRIPTORS; |
| 64 | } |
| 65 | int length = this.typeParameterDescriptors.length; |
| 66 | if (this.typeParameterDescriptorsCounter != length) { |
| 67 | System.arraycopy(this.typeParameterDescriptors, 0, (this.typeParameterDescriptors = new TypeParameterDescriptor[this.typeParameterDescriptorsCounter]), 0, this.typeParameterDescriptorsCounter); |
| 68 | } |
| 69 | return this.typeParameterDescriptors; |
| 70 | } |
| 71 | |
| 72 | public String[] getTypeArguments() { |
| 73 | if (this.typeArguments == null) { |
| 74 | return EMPTY_TYPE_ARGUMENTS; |
| 75 | } |
| 76 | int length = this.typeArguments.length; |
| 77 | if (this.typeArgumentsCounter != length) { |
| 78 | System.arraycopy(this.typeArguments, 0, (this.typeArguments= new String[this.typeArgumentsCounter]), 0, this.typeArgumentsCounter); |
| 79 | } |
| 80 | return this.typeArguments; |
| 81 | } |
| 82 | |
| 83 | public void setClassBound(String bound) { |
| 84 | this.currentTypeParameterDescriptor.setClassBound(bound); |
| 85 | } |
| 86 | |
| 87 | public void setSuperclass(String superclass) { |
| 88 | this.superClass = superclass; |
| 89 | } |
| 90 | |
| 91 | public String toString() { |
| 92 | StringBuffer buffer = new StringBuffer(); |
| 93 | for (int i = 0, max = this.typeParameterDescriptorsCounter; i < max; i++) { |
| 94 | if (i > 0) buffer.append(','); |
| 95 | buffer.append(this.typeParameterDescriptors[i]); |
| 96 | } |
| 97 | buffer.append("superclass: " + this.superClass); //$NON-NLS-1$ |
| 98 | for (int i = 0, max = this.typeArgumentsCounter; i < max; i++) { |
| 99 | if (i > 0) buffer.append(','); |
| 100 | buffer.append(this.typeArguments[i]); |
| 101 | } |
| 102 | return String.valueOf(buffer); |
| 103 | } |
| 104 | } |