001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.harmony.pack200;
020
021import java.util.List;
022
023/**
024 * Constant pool entry for a signature.
025 */
026public class CPSignature extends ConstantPoolEntry implements Comparable {
027
028    private final CPUTF8 signatureForm;
029    private final List<CPClass> classes;
030    private final String signature;
031    private final boolean formStartsWithBracket;
032
033    public CPSignature(final String signature, final CPUTF8 signatureForm, final List<CPClass> classes) {
034        this.signature = signature;
035        this.signatureForm = signatureForm;
036        this.classes = classes;
037        formStartsWithBracket = signatureForm.toString().startsWith("(");
038    }
039
040    @Override
041    public int compareTo(final Object arg0) {
042        if (signature.equals(((CPSignature) arg0).signature)) {
043            return 0;
044        }
045        if (formStartsWithBracket && !((CPSignature) arg0).formStartsWithBracket) {
046            return 1;
047        }
048        if (((CPSignature) arg0).formStartsWithBracket && !formStartsWithBracket) {
049            return -1;
050        }
051        if (classes.size() - ((CPSignature) arg0).classes.size() != 0) {
052            return classes.size() - ((CPSignature) arg0).classes.size();
053        }
054        if (classes.size() > 0) {
055            for (int i = classes.size() - 1; i >= 0; i--) {
056                final CPClass cpClass = classes.get(i);
057                final CPClass compareClass = ((CPSignature) arg0).classes.get(i);
058                final int classComp = cpClass.compareTo(compareClass);
059                if (classComp != 0) {
060                    return classComp;
061                }
062            }
063        }
064        return signature.compareTo(((CPSignature) arg0).signature);
065    }
066
067    public List<CPClass> getClasses() {
068        return classes;
069    }
070
071    public int getIndexInCpUtf8() {
072        return signatureForm.getIndex();
073    }
074
075    public CPUTF8 getSignatureForm() {
076        return signatureForm;
077    }
078
079    public String getUnderlyingString() {
080        return signature;
081    }
082
083    @Override
084    public String toString() {
085        return signature;
086    }
087}