View Javadoc
1   
2   package com.simpligility.maven.plugins.android.configuration;
3   
4   import org.apache.maven.plugin.MojoExecutionException;
5   
6   import static java.lang.String.format;
7   
8   /**
9    * @author Pappy STÄ‚NESCU - pappy.stanescu@gmail.com
10   * @author Wang Xuerui  - idontknw.wang@gmail.com
11   */
12  public class VersionGenerator
13  {
14  
15      private final int elementsCount;
16  
17      private final int[] multipliers;
18  
19      private final VersionElementParser elementParser;
20  
21      public VersionGenerator()
22      {
23          this( "4,3,3", "" );
24      }
25  
26      public VersionGenerator( String versionDigits )
27      {
28          this( versionDigits, "" );
29      }
30  
31      public VersionGenerator( String versionDigits, String versionNamingPattern )
32      {
33          final String[] digits = versionDigits.split( "[,;]" );
34  
35          this.elementsCount = digits.length;
36          this.multipliers = new int[this.elementsCount];
37  
38          int total = 0;
39  
40          for ( int k = 0; k < this.elementsCount; k++ )
41          {
42              int value = Integer.valueOf( digits[k].trim() );
43  
44              total += value;
45  
46              this.multipliers[k] = (int) Math.pow( 10, value );
47          }
48  
49          if ( total < 1 || total > 10 )
50          {
51              throw new IllegalArgumentException( format( "Invalid number of digits, got %d", total ) );
52          }
53  
54          // Choose a version element parser implementation based on the naming pattern
55          // passed in; an empty pattern triggers the old behavior.
56          if ( versionNamingPattern != null && ! versionNamingPattern.isEmpty() )
57          {
58              this.elementParser = new RegexVersionElementParser( versionNamingPattern );
59          }
60          else
61          {
62              this.elementParser = new SimpleVersionElementParser();
63          }
64      }
65  
66      public int generate( String versionName ) throws MojoExecutionException
67      {
68          final int[] versionElements = elementParser.parseVersionElements( versionName );
69  
70          long versionCode = 0;
71  
72          for ( int k = 0; k < this.elementsCount; k++ )
73          {
74              versionCode *= this.multipliers[k];
75  
76              if ( k < versionElements.length )
77              {
78                  final int elementValue = versionElements[k];
79  
80                  if ( elementValue >= this.multipliers[k] )
81                  {
82                      throw new MojoExecutionException( format( "The version element is too large: %d, max %d",
83                          elementValue, this.multipliers[k] - 1 ) );
84                  }
85  
86                  versionCode += elementValue;
87              }
88          }
89  
90          if ( versionCode > Integer.MAX_VALUE )
91          {
92              throw new MojoExecutionException( format( "The version code is too large: %d", versionCode ) );
93          }
94  
95          return (int) versionCode;
96      }
97  }