KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > version > common > versionlabel > SerialVersionLabelPolicy


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.version.common.versionlabel;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.alfresco.repo.version.VersionModel;
23 import org.alfresco.service.cmr.version.Version;
24 import org.alfresco.service.cmr.version.VersionType;
25 import org.alfresco.service.namespace.QName;
26
27 /**
28  * The serial version label policy.
29  *
30  * @author Roy Wetherall
31  */

32 public class SerialVersionLabelPolicy
33 {
34     // TODO need to add support for branches into this labeling policy
35

36     /**
37      * Get the version label value base on the data provided.
38      *
39      * @param preceedingVersion the preceeding version, null if none
40      * @param versionNumber the new version number
41      * @param versionProperties the version property values
42      * @return the version label
43      */

44     public String JavaDoc calculateVersionLabel(
45             QName classRef,
46             Version preceedingVersion,
47             int versionNumber,
48             Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties)
49     {
50         SerialVersionLabel serialVersionNumber = null;
51         
52         if (preceedingVersion != null)
53         {
54             serialVersionNumber = new SerialVersionLabel(preceedingVersion.getVersionLabel());
55             
56             VersionType versionType = (VersionType)versionProperties.get(VersionModel.PROP_VERSION_TYPE);
57             if (VersionType.MAJOR.equals(versionType) == true)
58             {
59                 serialVersionNumber.majorIncrement();
60             }
61             else
62             {
63                 serialVersionNumber.minorIncrement();
64             }
65         }
66         else
67         {
68             serialVersionNumber = new SerialVersionLabel(null);
69         }
70         
71         return serialVersionNumber.toString();
72     }
73     
74     /**
75      * Inner class encapsulating the notion of the serial version number.
76      *
77      * @author Roy Wetherall
78      */

79     private class SerialVersionLabel
80     {
81         /**
82          * The version number delimiter
83          */

84         private static final String JavaDoc DELIMITER = ".";
85         
86         /**
87          * The major revision number
88          */

89         private int majorRevisionNumber = 1;
90         
91         /**
92          * The minor revision number
93          */

94         private int minorRevisionNumber = 0;
95         
96         /**
97          * Constructor
98          *
99          * @param version the vesion to take the version from
100          */

101         public SerialVersionLabel(String JavaDoc versionLabel)
102         {
103             if (versionLabel != null && versionLabel.length() != 0)
104             {
105                 int iIndex = versionLabel.indexOf(DELIMITER);
106                 String JavaDoc majorString = versionLabel.substring(0, iIndex);
107                 String JavaDoc minorString = versionLabel.substring(iIndex+1);
108                 
109                 this.majorRevisionNumber = Integer.parseInt(majorString);
110                 this.minorRevisionNumber = Integer.parseInt(minorString);
111             }
112         }
113         
114         /**
115          * Increments the major revision numebr and sets the minor to
116          * zero.
117          */

118         public void majorIncrement()
119         {
120             this.majorRevisionNumber += 1;
121             this.minorRevisionNumber = 0;
122         }
123         
124         /**
125          * Increments only the minor revision number
126          */

127         public void minorIncrement()
128         {
129             this.minorRevisionNumber += 1;
130         }
131         
132         /**
133          * Converts the serial version number into a string
134          */

135         public String JavaDoc toString()
136         {
137             return this.majorRevisionNumber + DELIMITER + this.minorRevisionNumber;
138         }
139     }
140 }
141
Popular Tags