krysalis-version/src/java/org/krysalis/version VersionFormatException.java,NONE,1.1 VersionSpecification.java,NONE,1.1 Version.java,NONE,1.1 VersionRuntimeException.java,NONE,1.1 VersionManager.java,NONE,1.1 VersionFormatFactory.java,NONE,1.1 VersionMarker.java,NONE,1.1 VersionConstants.java,NONE,1.1 VersionError.java,NONE,1.1 VersionIdentifier.java,NONE,1.1 VersionFormat.java,NONE,1.1 VersionIdentifierType.java,NONE,1.1 VersionException.java,NONE,1.1 VersionSpecificationFactory.java,NONE,1.1
Nick Chalko <[email protected]> Tue, 09 Nov 2004 06:40:30 +0000
| Newsgroups | gmane.comp.krysalis.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/krysalis/krysalis-version/src/java/org/krysalis/version
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20857/src/java/org/krysalis/version
Added Files:
VersionFormatException.java VersionSpecification.java
Version.java VersionRuntimeException.java VersionManager.java
VersionFormatFactory.java VersionMarker.java
VersionConstants.java VersionError.java VersionIdentifier.java
VersionFormat.java VersionIdentifierType.java
VersionException.java VersionSpecificationFactory.java
Log Message:
Mucho Refactoring.
--- NEW FILE: Version.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
import java.util.Comparator;
// :TODO: Based upon thoughts from Anou..
//
// This class is a good start to allow other implementations
// however for a complete "drop in implementation" there is more
// needed. So, either we extend this -- or we keep this simple
// and somehow have a separate VersionImplementation interface/plug-in.
//
// Things that ought be done:
//
// Resolver --- find a Version for an id (string)
// Collecting --- brute force look-ups/extraction
// (Is "collecting" part of resolving?)
// Parse --- could be put into the Version(String ) ctor.
// Others? Dep/Constr? etc.?
//
/**
* This primary Version interface.
*
* This class provides NO restrictions on what and how to
* order and format or display version. How it is created incremented or
* displayed is completly independent.
*
* Given Version objects a and b
* <ul>
* <li>a.equals(b) iff b.equals(a)</li>
* <li>a.compare(b) == 0 iff a.equals(b)</li>
* <li>a.equals(b) --> a.hashCode() == b.hashCode()</li>
* <li>a.equals(b) --> a.isCompatible(b)</li>
* </ul>
* <br/>
* NOTE: Version should be implemented as a Immutable object.
*
* @version $ $
*/
public interface Version extends Comparable {
/**
* May this version be used in place of the specified version.
* @param version the BaseVersion to check with
* @return boolean true iff this Version may be used in place of the specified version.
*/
boolean isCompatible(Version version);
/**
* Create a new VersionObject that is one "level" more.
* Level is implementation specific name, such as "release"or "beta".
* !v.equals(v.increment(X))
* v.compare(v.increment(X))) < 0
* @param level A string decribe the level of the next release
* @return Version a new Version one level higher.
* @throws VersionException
*/
Version increment(String level) throws VersionException;
/**
* Gets version Comparator. Usefull for sorting.
* The Comparator will throw a IllegalArgument of the two objects do not
* have exactly the same id.
* @return Comparator
*
*/
Comparator getComparator();
}
--- NEW FILE: VersionSpecificationFactory.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.krysalis.version.impl.apache.ApacheVersionSpecification;
/**
* @author ajack
*/
public class VersionSpecificationFactory {
private final static VersionSpecificationFactory INSTANCE = new VersionSpecificationFactory();
private static List orderList = null;
private static Map registryMap = null;
static {
orderList = new ArrayList();
registryMap = new HashMap();
// registerVersionSpecification(
// VersionImplementationConstants.DATETIMESTAMPED_VERSION_ID,
// DatetimestampedVersionSpecification.class);
// registerVersionSpecification(
// VersionImplementationConstants.APACHE_VERSION_ID,
// ApacheVersionSpecification.class);
// registerVersionSpecification(
// VersionImplementationConstants.ECLIPSE_VERSION_ID,
// EclipseVersionSpecification.class);
// registerVersionSpecification(
// VersionImplementationConstants.JAVASOFT_VERSION_ID,
// JavasoftVersionSpecification.class);
// registerVersionSpecification(
// VersionImplementationConstants.JAKARTA_COMMONS_VERSION_ID,
// JakartaCommonsVersionSpecification.class);
}
/** Default */
public static VersionSpecification createDefaultVersionSpecification() {
return new ApacheVersionSpecification();
}
/** Named */
public static VersionSpecification createVersionSpecificationFromId(
final String identifier) throws VersionException {
//:TODO: Implement..
// Register existing formats by ID
// allow users to register...
VersionSpecification specification = null;
synchronized (registryMap) {
Class registeredClass = (Class) registryMap.get(identifier);
if (null != registeredClass) {
try {
Object formatInst = registeredClass.newInstance();
specification = (VersionSpecification) formatInst;
} catch (Exception e) {
throw new VersionException(
"Failed to instantiate registered format for identifier ["
+ identifier + "]", e);
}
} else {
//:TODO: Be helpful, show them what you do have...
throw new VersionException(
"No registered format for identifier [" + identifier
+ "]");
}
}
return specification;
}
public static void registerVersionSpecification(String id, Class formatClass) {
synchronized (registryMap) {
if (!registryMap.containsKey(id))
orderList.add(id);
registryMap.put(id, formatClass);
}
}
public static List getRegisteredSpecificationIds() {
return Collections.unmodifiableList(orderList);
}
public static Set getRegisteredSpecifications() {
return Collections.unmodifiableSet(registryMap.entrySet());
}
}
--- NEW FILE: VersionMarker.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
import java.util.List;
import java.util.Map;
// :TODO: Based upon thoughts from Anou..
//
// This class is a good start to allow other implementations
// however for a complete "drop in implementation" there is more
// needed. So, either we extend this -- or we keep this simple
// and somehow have a separate VersionImplementation interface/plug-in.
//
// Things that ought be done:
//
// Resolver --- find a Version for an id (string)
// Collecting --- brute force look-ups/extraction
// (Is "collecting" part of resolving?)
// Parse --- could be put into the Version(String ) ctor.
// Others? Dep/Constr? etc.?
//
/**
* WHAT IS THIS INTERFACCE FOR. Version is just a "number" a VersionMarker. Is
* associated with a ID and therefer a program ? What is the differnce between
* Attributes and Annontations
*
* NOTE: VersionMarker should be implemented as a Immutable object.
*
*
* @author $Author: chalko $
* @version $Revision: 1.1 $
*/
public interface VersionMarker extends Comparable {
public final static VersionMarker UNKNOWN = new VersionMarker() {
public String getId() {
// TODO Auto-generated method stub
return null;
}
public Version getVersion() {
// TODO Auto-generated method stub
return null;
}
public String getShortVersion() {
// TODO Auto-generated method stub
return null;
}
public String getLongVersion() {
// TODO Auto-generated method stub
return null;
}
public String getDefaultVersion() {
// TODO Auto-generated method stub
return null;
}
public Map getAttributes() {
// TODO Auto-generated method stub
return null;
}
public List getAnnotations() {
// TODO Auto-generated method stub
return null;
}
public VersionSpecification getSpecification() {
// TODO Auto-generated method stub
return null;
}
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
};
/**
* Get the globaly unique id for the Product. (org.apache.version) This
* should be a SAFE name without special chars of spaces.
*
* @return String
*/
String getId();
/**
* Version (1.2.7)
*
* @return Version
*/
Version getVersion();
/**
* Short Version (1.2)
*
* @return String
*/
String getShortVersion();
/**
* Long Version 1.2.7 (fred) built June 7,1981
*
* @return String
*/
String getLongVersion(); // TODO: Rename as asLongFormat
/**
* Default Version 1.2.7 (fred) built June 7,1981
*
* @return String
*/
String getDefaultVersion(); // TODO: Rename as asLongFormat
/**
* Get any associated attributes, or null if none.
*/
Map getAttributes();
/**
* Get any associated annotations, or null if none.
*/
List getAnnotations();
/**
* The specifiaction used for this version.
*
* @return VersionSpecification
*/
VersionSpecification getSpecification();
}
--- NEW FILE: VersionIdentifierType.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
import java.io.PrintWriter;
import java.util.HashMap;
/**
* @author ajack
*/
public class VersionIdentifierType implements Comparable {
/** Package Versions */
public static final String PACKAGE_NAME = "package";
public static final String PACKAGE_URI =
"http://apache.org/version/package";
/** Product Versions */
public static final String PRODUCT_NAME_NAME = "product";
public static final String PRODUCT_NAME_URI =
"http://apache.org/version/productname";
/** JVK Versions */
public static final String JVM_NAME = "jvm";
public static final String JVM_URI = "http://apache.org/version/jvm";
/** JVM Extension Versions */
public static final String JVM_EXTENSION_NAME = "jvm.extension";
public static final String JVM_EXTENSION_URI =
"http://apache.org/version/jvm/extension";
/** Unknown Versions */
public static final String UNKNOWN_NAME = "unknown";
public static final String UNKNOWN_URI =
"http://apache.org/version/unknown";
private String m_typeURI = PACKAGE_URI;
private String m_typeName = PACKAGE_NAME;
private static HashMap l_urisByName = new HashMap();
private static HashMap l_namesByURI = new HashMap();
private static HashMap l_typesByURI = new HashMap();
private static HashMap l_typesByName = new HashMap();
public final static VersionIdentifierType PACKAGE_TYPE;
public final static VersionIdentifierType PRODUCT_NAME_TYPE;
public final static VersionIdentifierType JVM_TYPE;
public final static VersionIdentifierType JVM_EXTENSION_TYPE;
public final static VersionIdentifierType UNKNOWN_TYPE;
static {
l_urisByName.put(PACKAGE_NAME, PACKAGE_URI);
l_urisByName.put(PRODUCT_NAME_NAME, PRODUCT_NAME_URI);
l_urisByName.put(JVM_NAME, JVM_URI);
l_urisByName.put(JVM_EXTENSION_NAME, JVM_EXTENSION_URI);
l_urisByName.put(UNKNOWN_NAME, UNKNOWN_URI);
l_namesByURI.put(PACKAGE_URI, PACKAGE_NAME);
l_namesByURI.put(PRODUCT_NAME_URI, PRODUCT_NAME_NAME);
l_namesByURI.put(JVM_URI, JVM_NAME);
l_namesByURI.put(JVM_EXTENSION_URI, JVM_EXTENSION_NAME);
l_namesByURI.put(UNKNOWN_URI, UNKNOWN_NAME);
UNKNOWN_TYPE = new VersionIdentifierType(UNKNOWN_NAME, UNKNOWN_URI);
PACKAGE_TYPE = new VersionIdentifierType(PACKAGE_NAME, PACKAGE_URI);
PRODUCT_NAME_TYPE =
new VersionIdentifierType(PRODUCT_NAME_NAME, PRODUCT_NAME_URI);
JVM_TYPE = new VersionIdentifierType(JVM_NAME, JVM_URI);
JVM_EXTENSION_TYPE =
new VersionIdentifierType(JVM_EXTENSION_NAME, JVM_EXTENSION_URI);
l_typesByName.put(PACKAGE_NAME, PACKAGE_TYPE);
l_typesByName.put(PRODUCT_NAME_NAME, PRODUCT_NAME_TYPE);
l_typesByName.put(JVM_NAME, JVM_TYPE);
l_typesByName.put(JVM_EXTENSION_NAME, JVM_EXTENSION_TYPE);
l_typesByName.put(UNKNOWN_NAME, UNKNOWN_TYPE);
l_typesByURI.put(PACKAGE_URI, PACKAGE_TYPE);
l_typesByURI.put(PRODUCT_NAME_URI, PRODUCT_NAME_TYPE);
l_typesByURI.put(JVM_URI, JVM_TYPE);
l_typesByURI.put(JVM_EXTENSION_URI, JVM_EXTENSION_TYPE);
l_typesByURI.put(UNKNOWN_URI, UNKNOWN_TYPE);
}
public static VersionIdentifierType importVersionIdentifierType(String typeString) {
VersionIdentifierType type = null;
type = (VersionIdentifierType) l_typesByName.get(typeString);
if (null == type)
type = (VersionIdentifierType) l_typesByURI.get(typeString);
if (null == type) {
throw new IllegalArgumentException(
"Illegal VersionIdentiferType: " + typeString);
}
return type;
}
public VersionIdentifierType(String typeName, String typeURI) {
m_typeName = typeName;
m_typeURI = typeURI;
validate();
}
public VersionIdentifierType(VersionIdentifierType other) {
m_typeName = other.m_typeName;
m_typeURI = other.m_typeURI;
validate();
}
void validate() {
if ((null == m_typeName) || (0 == m_typeName.length()))
throw new IllegalArgumentException("Illegal VersionIdentifer typeName. It cannot be null or \"\"");
if ((null == m_typeURI) || (0 == m_typeURI.length()))
throw new IllegalArgumentException("Illegal VersionIdentifer typeURI. It cannot be null or \"\"");
}
/**
* Returns the typeURI.
* @return String
*/
public String getTypeURI() {
return m_typeURI;
}
public String toString() { // :TODO: Lazy, make more efficient...
if (PACKAGE_NAME.equals(m_typeName))
return "";
return m_typeName;
}
/**
* @see Object#equals(Object)
*/
public boolean equals(Object other) {
boolean equal = false;
if (other instanceof VersionIdentifierType) {
VersionIdentifierType otherVersionIdType =
(VersionIdentifierType) other;
equal = otherVersionIdType.m_typeURI.equalsIgnoreCase(m_typeURI);
}
return equal;
}
/**
* @see Object#hashCode()
*/
public int hashCode() {
return m_typeURI.hashCode();
}
/**
* @see Object#equals(Object)
*/
public int compareTo(Object other) {
int comparison = -1;
if (null == other) {
comparison = -1;
}
else if (other instanceof VersionIdentifierType) {
VersionIdentifierType otherVersionIdType =
(VersionIdentifierType) other;
comparison = (otherVersionIdType.m_typeURI.compareTo(m_typeURI));
}
else
throw new IllegalArgumentException(
"Not a VersionIdentifierType: "
+ other.getClass()
+ " : "
+ other.toString());
return comparison;
}
public void dump(PrintWriter out, int depth, boolean verbose) {
final String indent =" ";
if ((null != m_typeName) && verbose) {
out.print(indent);
out.println("Type Name: " + m_typeName);
}
if ((null != m_typeURI) && verbose) {
out.print(indent);
out.println("Type URI: " + m_typeURI);
}
}
}
--- NEW FILE: VersionFormatException.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
/**
* @author ajack
*/
public class VersionFormatException extends VersionException {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = -8251465912680204684L;
private VersionFormat m_format = null;
/**
* Constructor for FormatException.
*/
public VersionFormatException(VersionFormat format) {
super();
m_format = format;
}
/**
* Constructor for FormatException.
* @param message
*/
public VersionFormatException(VersionFormat format, String message) {
super(message);
m_format = format;
}
/**
* Constructor for FormatException.
* @param message
* @param cause
*/
public VersionFormatException(
VersionFormat format,
String message,
Throwable cause) {
super(message, cause);
m_format = format;
}
/**
* Constructor for FormatException.
* @param cause
*/
public VersionFormatException(VersionFormat format, Throwable cause) {
super(cause);
m_format = format;
}
/**
* Returns the format.
* @return VersionFormat
*/
public VersionFormat getFormat() {
return m_format;
}
/**
* @see java.lang.Throwable#getMessage()
*/
public String getMessage() {
return super.getMessage() + ", format: " + m_format;
}
}
--- NEW FILE: VersionException.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
/**
* @author ajack
*/
public class VersionException extends Exception {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = -6458898037691815493L;
/**
*
*/
public VersionException() {
super();
}
/**
* @param message
*/
public VersionException(String message) {
super(message);
}
/**
* @param message
* @param cause
*/
public VersionException(String message, Throwable cause) {
super(message, cause);
}
/**
* @param cause
*/
public VersionException(Throwable cause) {
super(cause);
}
}
--- NEW FILE: VersionError.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
/**
* @author ajack
*/
public class VersionError extends Error{
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 6999200834029388000L;
/**
*
*/
public VersionError() {
super();
}
/**
* @param message
*/
public VersionError(String message) {
super(message);
}
/**
* @param message
* @param cause
*/
public VersionError(String message, Throwable cause) {
super(message, cause);
}
/**
* @param cause
*/
public VersionError(Throwable cause) {
super(cause);
}
}
--- NEW FILE: VersionRuntimeException.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
/**
* @author ajack
*/
public class VersionRuntimeException extends RuntimeException {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
/**
*
*/
public VersionRuntimeException() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public VersionRuntimeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public VersionRuntimeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public VersionRuntimeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
--- NEW FILE: VersionSpecification.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
/**
* Describes a specific version specifcation.
* TODO: this should probably move to the top level package.
*
* @author ajack
*/
public interface VersionSpecification {
/**
* The globally unique id of the specification
* @return the specification id as a String
*/
String getId();
/**
* The description of this specification
* @return the description as a String
*/
String getDescription();
// Data Contents
int getElementMask();
/**
* The version formater for this specification.
* @return the VersionFormat used by this specification.
*/
VersionFormat getVersionFormat();
}
--- NEW FILE: VersionConstants.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
/**
* @author ajack
*/
public class VersionConstants {
public static final String BASE = "org.krysalis.version";
// Version Attributes
public static final String DESCRIPTION = BASE + ".description";
public static final String LOCATION_URL = BASE + ".location.url";
private static final String BUILD_BASE = BASE + ".build";
public static final String BUILD_JVM = BUILD_BASE + ".jvm";
public static final String BUILD_DATETIME = BUILD_BASE + ".datetime";
public static final String BUILD_ANT = BUILD_BASE + ".ant";
public static final String BUILD_HOST = BUILD_BASE + ".host";
public static final String BUILD_USER = BUILD_BASE + ".user";
public static final String BUILD_TAG = BUILD_BASE + ".tag";
}
--- NEW FILE: VersionFormat.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
import java.util.Map;
/**
*
*
* Implements field -> method
*
* Get from :
*
* public static final {Class} {METHOD_NAME}
*
* Set to:
*
* public set{MethodName}({Class})
*
*
* @author ajack
*/
public interface VersionFormat {
public final String LONG = "LONG";
public final String SHORT = "SHORT";
public final String DEFUALT = "DEFAULT";
String getFormatId();
/**
* Parse a version creating a VersionData object for it.
*
* @param version
* The version string to be parsed
* @return a Version representing the Version specified by the string.
*
* @throws VersionFormatException
*
*/
public Version parseVersion(String version) throws VersionFormatException;
/**
* @param id
* @param version
* @param properties
* @return
*/
VersionMarker createVersionMarker(VersionIdentifier id, Version version, Map properties);
}
--- NEW FILE: VersionFormatFactory.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
import java.util.HashMap;
import org.krysalis.version.impl.apache.ApacheVersionFormat;
/**
* @author ajack
*/
public class VersionFormatFactory {
private static HashMap l_registry = null;
static {
l_registry = new HashMap();
registerVersionFormat(
org.krysalis.version.impl.VersionImplementationConstants.APACHE_VERSION_ID,
org.krysalis.version.impl.apache.ApacheVersionFormat.class);
}
public static VersionFormat createApacheVersionFormat() {
return new ApacheVersionFormat();
}
/** Default */
public static VersionFormat createDefaultVersionFormat() {
return new org.krysalis.version.impl.apache.ApacheVersionFormat();
}
/** Named */
public static VersionFormat createVersionFormatFromId(final String identifier)
throws VersionException {
//:TODO: Implement..
// Register existing formats by ID
// allow users to register...
VersionFormat format = null;
synchronized (l_registry) {
Class registeredClass = (Class) l_registry.get(identifier);
if (null != registeredClass) {
try {
Object formatInst = registeredClass.newInstance();
format = (VersionFormat) formatInst;
}
catch (Exception e) {
throw new VersionException(
"Failed to instantiate registered format for identifier ["
+ identifier
+ "]",
e);
}
}
else {
//:TODO: Be helpful, show them what you do have...
throw new VersionException(
"No registered format for identifier [" + identifier + "]");
}
}
return format;
}
public static void registerVersionFormat(String id, Class formatClass) {
synchronized (l_registry) {
l_registry.put(id, formatClass);
}
}
}
--- NEW FILE: VersionManager.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
import java.util.Map;
import org.krysalis.version.impl.apache.ApacheVersion;
import org.krysalis.version.impl.apache.ApacheVersionMarker;
/**
* @author [email protected]
*/
public class VersionManager {
public static final VersionManager getManager() {
return new VersionManager();
}
/**
* Create a version marker from and ID and a version string.
*
* @param id
* The id of the versioned artifact
* @param version
* as a String
* @return a VersinMarker object.
* @throws VersionException
*/
public final VersionMarker createVersionMarker(String id, String version)
throws VersionException {
return new ApacheVersionMarker(new VersionIdentifier(id),
new ApacheVersion(version));
}
/**
* Create a VersionMarker from an arbitrary object. Usually a generated
* version stamp.
*
* @param stamp
* A instance if a version stamp.
* @return VersionMarker of the correct spec with all values filled in.
*/
public VersionMarker createVersionMarker(Object stamp) {
try {
String specId = getSpecificationId(stamp);
String versionString = getVersionString(stamp);
VersionIdentifier id = VersionIdentifier
.extractVersionIdentifier(stamp);
Map properties = getProperties(stamp);
VersionSpecification spec = VersionSpecificationFactory
.createVersionSpecificationFromId(specId);
VersionFormat versionFormat = spec.getVersionFormat();
final VersionMarker marker = versionFormat.createVersionMarker(
id, versionFormat.parseVersion(versionString), properties);
return marker;
} catch (VersionException e) {
return VersionMarker.UNKNOWN;
}
}
/**
* @param stamp
* @return
*/
private Map getProperties(Object stamp) {
// TODO Auto-generated method stub
return null;
}
/**
* @param stamp
* @return
*/
private String getStampId(Object stamp) {
// TODO Auto-generated method stub
return null;
}
/**
* @param stamp
* @return
*/
private String getVersionString(Object stamp) {
// TODO Auto-generated method stub
return null;
}
/**
* @param stamp
* @return
*/
private String getSpecificationId(Object stamp) {
// TODO Auto-generated method stub
return null;
}
/**
* Create a Version object from the given string.
*
* @param versionString
* @return
* @throws VersionFormatException
*/
public Version createVersion(String versionString)
throws VersionFormatException {
VersionSpecification spec = VersionSpecificationFactory
.createDefaultVersionSpecification();
return createVersion(spec, versionString);
}
/**
* @param spec
* @param versionString
* @return Version
* @throws VersionFormatException
*/
public Version createVersion(VersionSpecification spec, String versionString)
throws VersionFormatException {
return spec.getVersionFormat().parseVersion(versionString);
}
// :TODO:
// Stamp a version for ...
// Generate code for ...
// Increment ...
}
--- NEW FILE: VersionIdentifier.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.version;
import java.io.PrintWriter;
/**
* @author ajack
*/
public class VersionIdentifier implements Comparable {
private String m_key = null;
private VersionIdentifierType m_type = VersionIdentifierType.PACKAGE_TYPE;
public final static VersionIdentifier UNKNOWN;
static {
UNKNOWN =
new VersionIdentifier(
VersionIdentifierType.UNKNOWN_TYPE,
"unknown");
}
public static VersionIdentifier importVersionIdentifier(String idString) {
VersionIdentifierType type = null;
String idPart = null;
int colonPosn = idString.indexOf(":");
if ((0 < colonPosn) && (colonPosn < idString.length())) {
type =
VersionIdentifierType.importVersionIdentifierType(
idString.substring(0, colonPosn));
idPart = idString.substring(colonPosn + 1);
}
else {
// Without a "." is a PRODUCT_NAME not PACKAGE
if (-1 == idString.indexOf('.')) {
type = VersionIdentifierType.PRODUCT_NAME_TYPE;
}
else
type = VersionIdentifierType.PACKAGE_TYPE;
idPart = idString;
}
return new VersionIdentifier(type, idPart);
}
public static VersionIdentifier extractVersionIdentifier(Object obj) {
return extractVersionIdentifier(obj.getClass());
}
// :TODO: Ensure we don't have ApacheVersion by accident...
public static VersionIdentifier extractVersionIdentifier(Class clazz) {
String key = clazz.getName();
int packagePosition = key.lastIndexOf('.');
if (0 < packagePosition)
key = key.substring(0, packagePosition);
if (key.endsWith(".version"))
key = key.substring(0, key.length() - 8);
return new VersionIdentifier(key);
}
public VersionIdentifier(String key) {
this(VersionIdentifier.importVersionIdentifier(key));
}
public VersionIdentifier(String typeURI, String key) {
this(VersionIdentifierType.importVersionIdentifierType(typeURI), key);
}
public VersionIdentifier(VersionIdentifierType type, String key) {
m_type = type;
m_key = key;
validate();
}
public VersionIdentifier(String typeName, String typeURI, String key) {
this(new VersionIdentifierType(typeName, typeURI), key);
}
public VersionIdentifier(VersionIdentifier other) {
m_type = other.m_type;
m_key = other.m_key;
validate();
}
void validate() {
if ((null == m_key) || (0 == m_key.length()))
throw new IllegalArgumentException("Illegal VersionIdentifer key. It cannot be null or \"\"");
if (null == m_type)
throw new IllegalArgumentException("Illegal VersionIdentifer typeName. It cannot be null or \"\"");
m_type.validate();
}
/**
* Returns the key.
* @return String
*/
public String getKey() {
return m_key;
}
/**
* Returns the typeURI.
* @return String
*/
public String getTypeURI() {
return m_type.getTypeURI();
}
/**
* Sets the key.
* @param key The key to set
*/
public void setKey(String key) {
m_key = key;
}
public String toString() { // :TODO: Lazy, make more efficient...
if (VersionIdentifierType.PACKAGE_TYPE.equals(m_type))
return m_key;
return m_type + ":" + m_key;
}
/**
* @see Object#equals(Object)
*/
public boolean equals(Object other) {
boolean equal = false;
if (other instanceof VersionIdentifier) {
VersionIdentifier otherVersionId = (VersionIdentifier) other;
equal = (otherVersionId.m_key.equals(m_key));
equal &= otherVersionId.m_type.equals(m_type);
}
return equal;
}
/**
* @see Object#hashCode()
*/
public int hashCode() {
return m_type.hashCode() + m_key.hashCode();
}
/**
* @see Object#equals(Object)
*/
public int compareTo(Object other) {
int comparison = -1;
if (null == other) {
comparison = -1;
}
else if (other instanceof VersionIdentifier) {
VersionIdentifier otherVersionId = (VersionIdentifier) other;
comparison = (otherVersionId.m_type.compareTo(m_type));
if (0 == comparison)
comparison = (otherVersionId.m_key.compareTo(m_key));
}
else
throw new IllegalArgumentException(
"Not a VersionIdentifier: "
+ other.getClass()
+ " : "
+ other.toString());
return comparison;
}
public void dump(PrintWriter out, int depth, boolean verbose) {
final String indent = " ";
if ((null != m_type) && verbose) {
m_type.dump(out, depth, verbose);
}
if (null != m_key) {
out.print(indent);
out.println("Identifier Key: " + m_key);
}
}
}
-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click