View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang;
5   
6   /**
7    * Created by christoferdutz on 21.09.14.
8    */
9   public class LanguageVersion implements Comparable<LanguageVersion> {
10  
11      private final Language language;
12      private final String version;
13      private final LanguageVersionHandler languageVersionHandler;
14  
15      public LanguageVersion(Language language, String version, LanguageVersionHandler languageVersionHandler) {
16          this.language = language;
17          this.version = version;
18          this.languageVersionHandler = languageVersionHandler;
19      }
20  
21      public Language getLanguage() {
22          return language;
23      }
24  
25      public String getVersion() {
26          return version;
27      }
28  
29      public LanguageVersionHandler getLanguageVersionHandler() {
30          return languageVersionHandler;
31      }
32  
33      /**
34       * Get the name of this LanguageVersion.  This is Language name
35       * appended with the LanguageVersion version if not an empty String.
36       * @return The name of this LanguageVersion.
37       */
38      public String getName() {
39          return version.length() > 0 ? language.getName() + ' ' + version : language.getName();
40      }
41  
42      /**
43       * Get the short name of this LanguageVersion.  This is Language short name
44       * appended with the LanguageVersion version if not an empty String.
45       * @return The short name of this LanguageVersion.
46       */
47      public String getShortName() {
48          return version.length() > 0 ? language.getShortName() + ' ' + version : language.getShortName();
49      }
50  
51      /**
52       * Get the terse name of this LanguageVersion.  This is Language terse name
53       * appended with the LanguageVersion version if not an empty String.
54       * @return The terse name of this LanguageVersion.
55       */
56      public String getTerseName() {
57          return version.length() > 0 ? language.getTerseName() + ' ' + version : language.getTerseName();
58      }
59  
60      @Override
61      public int compareTo(LanguageVersion o) {
62          if(o == null) {
63              return 1;
64          }
65  
66          int comp = getName().compareTo(o.getName());
67          if(comp != 0) {
68              return comp;
69          }
70  
71          String[] vals1 = getName().split("\\.");
72          String[] vals2 = o.getName().split("\\.");
73          int i = 0;
74          while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
75              i++;
76          }
77          if (i < vals1.length && i < vals2.length) {
78              int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
79              return Integer.signum(diff);
80          } else {
81              return Integer.signum(vals1.length - vals2.length);
82          }
83      }
84  
85      @Override
86      public String toString() {
87          return language.toString() + "+version:" + version;
88      }
89  }