View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.harmony.pack200;
18  
19  import java.util.List;
20  
21  /**
22   * Constant pool entry for a signature.
23   */
24  public class CPSignature extends ConstantPoolEntry implements Comparable {
25  
26      private final CPUTF8 signatureForm;
27      private final List<CPClass> classes;
28      private final String signature;
29      private final boolean formStartsWithBracket;
30  
31      public CPSignature(final String signature, final CPUTF8 signatureForm, final List<CPClass> classes) {
32          this.signature = signature;
33          this.signatureForm = signatureForm;
34          this.classes = classes;
35          formStartsWithBracket = signatureForm.toString().startsWith("(");
36      }
37  
38      @Override
39      public int compareTo(final Object arg0) {
40          if (signature.equals(((CPSignature) arg0).signature)) {
41              return 0;
42          }
43          if (formStartsWithBracket && !((CPSignature) arg0).formStartsWithBracket) {
44              return 1;
45          }
46          if (((CPSignature) arg0).formStartsWithBracket && !formStartsWithBracket) {
47              return -1;
48          }
49          if (classes.size() - ((CPSignature) arg0).classes.size() != 0) {
50              return classes.size() - ((CPSignature) arg0).classes.size();
51          }
52          if (classes.size() > 0) {
53              for (int i = classes.size() - 1; i >= 0; i--) {
54                  final CPClass cpClass = classes.get(i);
55                  final CPClass compareClass = ((CPSignature) arg0).classes.get(i);
56                  final int classComp = cpClass.compareTo(compareClass);
57                  if (classComp != 0) {
58                      return classComp;
59                  }
60              }
61          }
62          return signature.compareTo(((CPSignature) arg0).signature);
63      }
64  
65      public List<CPClass> getClasses() {
66          return classes;
67      }
68  
69      public int getIndexInCpUtf8() {
70          return signatureForm.getIndex();
71      }
72  
73      public CPUTF8 getSignatureForm() {
74          return signatureForm;
75      }
76  
77      public String getUnderlyingString() {
78          return signature;
79      }
80  
81      @Override
82      public String toString() {
83          return signature;
84      }
85  }