View Javadoc
1   package org.apache.maven.shared.release.policy.oddeven;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.maven.shared.release.policy.PolicyException;
25  import org.apache.maven.shared.release.policy.version.VersionPolicy;
26  import org.apache.maven.shared.release.policy.version.VersionPolicyRequest;
27  import org.apache.maven.shared.release.policy.version.VersionPolicyResult;
28  import org.apache.maven.shared.release.versions.DefaultVersionInfo;
29  import org.apache.maven.shared.release.versions.VersionInfo;
30  import org.apache.maven.shared.release.versions.VersionParseException;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * A {@link VersionPolicy} implementation that allows release even version numbers only and skips the odd.
36   */
37  @Component(
38      role = VersionPolicy.class,
39      hint = "OddEvenVersionPolicy",
40      description = "A VersionPolicy implementation that allows release even version numbers"
41  )
42  public final class OddEvenVersionPolicy
43      implements VersionPolicy
44  {
45  
46      public VersionPolicyResult getReleaseVersion( VersionPolicyRequest request )
47          throws PolicyException
48      {
49          return calculateNextVersion( request, false );
50      }
51  
52      public VersionPolicyResult getDevelopmentVersion( VersionPolicyRequest request )
53          throws PolicyException
54      {
55          return calculateNextVersion( request, true );
56      }
57  
58      private VersionPolicyResult calculateNextVersion( VersionPolicyRequest request, boolean development )
59      {
60          DefaultVersionInfo defaultVersionInfo = null;
61  
62          try
63          {
64              defaultVersionInfo = new DefaultVersionInfo( request.getVersion() );
65          }
66          catch ( VersionParseException e )
67          {
68              throw new IllegalArgumentException( "Can't tell if version with no digits is even: " + e.getMessage(), e );
69          }
70  
71          // by default, never reuse revisions
72          int versionNumbersToSkip = 1;
73  
74          // do we need a snapshot? make sure the version info is odd
75          if ( development && !isEven( defaultVersionInfo ) )
76          {
77              versionNumbersToSkip = 2;
78          }
79  
80          // do we need a release? make sure the version info is even
81          if ( !development && isEven( defaultVersionInfo ) )
82          {
83              versionNumbersToSkip = 0;
84          }
85  
86          VersionInfo suggestedVersionInfo = defaultVersionInfo;
87          while ( versionNumbersToSkip != 0 )
88          {
89              suggestedVersionInfo = suggestedVersionInfo.getNextVersion();
90              versionNumbersToSkip--;
91          }
92  
93          String nextVersion = development ? suggestedVersionInfo.getSnapshotVersionString()
94                                           : suggestedVersionInfo.getReleaseVersionString();
95  
96          return new VersionPolicyResult().setVersion( nextVersion );
97      }
98  
99      private boolean isEven( DefaultVersionInfo defaultVersionInfo )
100     {
101         int mostSignificantSegment;
102 
103         if ( StringUtils.isNumeric( defaultVersionInfo.getAnnotationRevision() ) )
104         {
105             mostSignificantSegment = Integer.parseInt( defaultVersionInfo.getAnnotationRevision() );
106         }
107         else
108         {
109             List<String> digits = defaultVersionInfo.getDigits();
110 
111             if ( digits == null )
112             {
113                 throw new IllegalArgumentException( "Can't tell if version with no digits is even." );
114             }
115 
116             mostSignificantSegment = Integer.parseInt( digits.get( digits.size() - 1 ) );
117         }
118 
119         return mostSignificantSegment % 2 == 0;
120     }
121 
122 }