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