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