Added items for MC 1.8, modified the update function, added some libs to

the repo.
This commit is contained in:
Pascal Koenig
2011-10-07 22:08:23 +02:00
parent 49f1341aa1
commit c66ce29467
14 changed files with 976 additions and 18 deletions

View File

@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_26"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_26"/>
<classpathentry kind="lib" path="lib/Achievements.jar"/>
<classpathentry kind="lib" path="lib/EssentialsGroupManager.jar"/>
<classpathentry kind="lib" path="lib/Permissions.jar"/>
<classpathentry kind="lib" path="lib/craftbukkit-0.0.1-SNAPSHOT.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

77
build.xml Normal file
View File

@@ -0,0 +1,77 @@
<?xml version="1.0"?>
<project name="Stats" basedir="." default="build">
<property name="bin" value="bin" />
<property name="dist" value="dist" />
<property name="src" value="src" />
<property name="name" value="Stats" />
<property name="version" value="0.99.1" />
<property name="resources" value="resources" />
<property name="author" value="nidefawl" />
<path id="classpath">
<pathelement location="lib/Achievements.jar" />
<pathelement location="lib/craftbukkit-0.0.1-SNAPSHOT.jar" />
<pathelement location="lib/EssentialsGroupManager.jar" />
<pathelement location="lib/Permissions.jar" />
</path>
<buildnumber file=".build.number" />
<!-- Anwendung bauen -->
<target name="build" description="Build the plugin" depends="zip" />
<!-- Clean up -->
<target name="clean.dist" description="Deletes temporary build files">
<delete dir="${dist}" />
</target>
<!-- Delete temporary build files -->
<target name="clean.classes" description="Deletes temporary build files">
<delete dir="${bin}" />
<delete>
<fileset dir="." includes="**/*~" defaultexcludes="no"/>
</delete>
</target>
<!-- Compile the source code -->
<target name="compile" depends="clean.classes, clean.dist">
<mkdir dir="bin" />
<javac srcdir="${src}"
destdir="${bin}"
debug="false"
deprecation="true"
optimize="true" >
<classpath refid="classpath" />
</javac>
</target>
<!-- Build a jar file -->
<target name="jar" depends="compile">
<copy file="${resources}/plugin.yml" tofile="${bin}/plugin.yml">
<filterchain>
<replacetokens>
<token key="VERSION" value="${version}-b${build.number}" />
<token key="NAME" value="${name}" />
<token key="AUTHOR" value="${author}" />
</replacetokens>
</filterchain>
</copy>
<jar jarfile="${dist}/${name}.jar">
<fileset dir="${bin}"/>
<fileset dir="${src}" includes="**/*.java" />
</jar>
</target>
<!-- build a zip file -->
<target name="zip" depends="jar" description="Zip .jar and config.yml into a zip file">
<zip destfile="${dist}/${name}-${version}-b${build.number}.zip">
<fileset dir="${dist}" includes="${name}.jar" />
<zipfileset dir="${resources}" includes="config.yml" prefix="${name}" />
</zip>
</target>
</project>

BIN
lib/Achievements.jar Normal file

Binary file not shown.

Binary file not shown.

BIN
lib/Permissions.jar Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -1,7 +1,7 @@
name: Stats
main: com.nidefawl.Stats.Stats
version: 0.99
author: nidefawl
name: @NAME@
main: com.@AUTHOR@.@NAME@.@NAME@
version: @VERSION@
author: @AUTHOR@
commands:
stats:
description: Stats command

View File

@@ -64,7 +64,6 @@ public class hModItemResolver implements itemResolver {
writer.write("dispenser:23\r\n");
writer.write("sandstone:24\r\n");
writer.write("musicblock:25\r\n");
writer.write("noteblock:25\r\n");
writer.write("bedblock:26\r\n");
writer.write("wool:35\r\n");
writer.write("cloth:35\r\n");
@@ -141,6 +140,32 @@ public class hModItemResolver implements itemResolver {
writer.write("cakeblock:92\r\n");
writer.write("repeateron:93\r\n");
writer.write("repeateroff:94\r\n");
writer.write("lockedchest:95\r\n");
writer.write("trapdoor:96\r\n");
writer.write("hiddensilverfish:97\r\n");
writer.write("stonebricks:98\r\n");
writer.write("hugebrownmushroom:99\r\n");
writer.write("hugeredmushroom:100\r\n");
writer.write("ironbars:101\r\n");
writer.write("glasspane:102\r\n");
writer.write("melon:103\r\n");
writer.write("pumpkinstem:104\r\n");
writer.write("melonstem:105\r\n");
writer.write("vines:106\r\n");
writer.write("fencegate:107\r\n");
writer.write("brickstairs:108\r\n");
writer.write("stonebrickstairs:109\r\n");
writer.write("mycelium:110\r\n");
writer.write("lilypad:111\r\n");
writer.write("netherbrick:112\r\n");
writer.write("netherbrickfence:113\r\n");
writer.write("netherbrickstairs:114\r\n");
writer.write("netherwartblock:115\r\n");
writer.write("enchantmenttable:116\r\n");
writer.write("brewingstandblock:117\r\n");
writer.write("cauldronblock:118\r\n");
writer.write("airportal:119\r\n");
writer.write("airportalframe:120\r\n");
writer.write("ironshovel:256\r\n");
writer.write("ironspade:256\r\n");
writer.write("ironpickaxe:257\r\n");
@@ -260,6 +285,31 @@ public class hModItemResolver implements itemResolver {
writer.write("cake:354\r\n");
writer.write("bed:355\r\n");
writer.write("repeater:356\r\n");
writer.write("cookie:357\r\n");
writer.write("map:358\r\n");
writer.write("shears:359\r\n");
writer.write("melonslice:360\r\n");
writer.write("pumpkinseeds:361\r\n");
writer.write("melonseeds:362\r\n");
writer.write("rawbeef:363\r\n");
writer.write("steak:364\r\n");
writer.write("rawchicken:365\r\n");
writer.write("cookedchicken:366\r\n");
writer.write("rottenflesh:367\r\n");
writer.write("enderpearl:368\r\n");
writer.write("blazerod:369\r\n");
writer.write("ghasttear:370\r\n");
writer.write("goldnugget:371\r\n");
writer.write("netherwart:372\r\n");
writer.write("potion:373\r\n");
writer.write("glassbottle:374\r\n");
writer.write("spidereye:375\r\n");
writer.write("fermentedspidereye:376\r\n");
writer.write("blazepowder:377\r\n");
writer.write("magmacream:378\r\n");
writer.write("brewingstand:379\r\n");
writer.write("cauldron:380\r\n");
writer.write("eyeofender:381\r\n");
writer.write("goldrecord:2256\r\n");
writer.write("greenrecord:2257\r\n");
} catch (Exception e) {

View File

@@ -0,0 +1,60 @@
/**
*
*/
package com.nidefawl.Stats.Permissions;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* @author socrates
*
*/
public class BukkitPermissionsResolver implements PermissionsResolver {
/* (non-Javadoc)
* @see com.nidefawl.Stats.Permissions.PermissionsResolver#load()
*/
@Override
public boolean load() {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see com.nidefawl.Stats.Permissions.PermissionsResolver#permission(org.bukkit.command.CommandSender, java.lang.String)
*/
@Override
public boolean permission(CommandSender player, String permCmd) {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see com.nidefawl.Stats.Permissions.PermissionsResolver#getGroup(org.bukkit.entity.Player)
*/
@Override
public String getGroup(Player player) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see com.nidefawl.Stats.Permissions.PermissionsResolver#inGroup(org.bukkit.entity.Player, java.lang.String)
*/
@Override
public boolean inGroup(Player player, String group) {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see com.nidefawl.Stats.Permissions.PermissionsResolver#reloadPerms()
*/
@Override
public void reloadPerms() {
// TODO Auto-generated method stub
}
}

View File

@@ -41,8 +41,8 @@ import com.nidefawl.Stats.util.Updater;
public class Stats extends JavaPlugin {
public final static Logger log = Logger.getLogger("Minecraft");
public final static double version = 0.99;
public final static String logprefix = "[Stats-" + version + "]";
public static final String version = "0.99.1";
public static final String logprefix = "[Stats-" + version + "]";
public final static String defaultCategory = "stats";
public boolean enabled = false;
public boolean updated = false;
@@ -80,7 +80,7 @@ public class Stats extends JavaPlugin {
perms.reloadPerms();
}
}
public static void LogError(String Message) {
log.log(Level.SEVERE, logprefix + " " + Message);
}
@@ -414,6 +414,7 @@ public class Stats extends JavaPlugin {
}
public void onEnable() {
getDataFolder().mkdirs();
File statsDirectory = new File("stats");
if (statsDirectory.exists() && statsDirectory.isDirectory()) {

View File

@@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import com.nidefawl.Stats.Stats;
@@ -37,15 +38,27 @@ public class UpdaterFile {
* The local url location
*/
private String localLocation;
private double localVersion;
private double remoteVersion;
private String localVersion;
private String remoteVersion;
public UpdaterFile(String remoteLocation, String localLocation, double localVersion, double remoteVersion) {
public UpdaterFile(String remoteLocation, String localLocation, String localVersion, String remoteVersion) {
this.remoteLocation = remoteLocation;
this.localLocation = localLocation;
this.localVersion = localVersion;
this.remoteVersion = remoteVersion;
}
public UpdaterFile(String remoteLocation, String localLocation, double localVersion, double remoteVersion){
this(remoteLocation, localLocation, String.valueOf(localVersion), String.valueOf(remoteVersion));
}
public UpdaterFile(String remoteLocation, String localLocation, String localVersion, double remoteVersion){
this(remoteLocation, localLocation, localVersion, String.valueOf(remoteVersion));
}
public UpdaterFile(String remoteLocation, String localLocation, double localVersion, String remoteVersion){
this(remoteLocation, localLocation, String.valueOf(localVersion), remoteVersion);
}
/**
* @return the local file location
@@ -89,7 +102,10 @@ public class UpdaterFile {
}
public boolean update(boolean autoUpdate) throws Exception {
if (remoteVersion > localVersion) {
DefaultArtifactVersion remoteVersion = new DefaultArtifactVersion(String.valueOf(this.remoteVersion));
DefaultArtifactVersion localVersion = new DefaultArtifactVersion(this.localVersion);
if (localVersion.compareTo(remoteVersion) < 0) {
if (autoUpdate) {
try {
Stats.LogInfo("Newer version of "+localLocation+" found. local: "+localVersion+" remote: "+remoteVersion);

View File

@@ -0,0 +1,43 @@
package org.apache.maven.artifact.versioning;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/**
* Describes an artifact version in terms of its components, converts it to/from a string and
* compares two versions.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id: ArtifactVersion.java 828793 2009-10-22 17:34:20Z bentmann $
*/
public interface ArtifactVersion
extends Comparable
{
int getMajorVersion();
int getMinorVersion();
int getIncrementalVersion();
int getBuildNumber();
String getQualifier();
void parseVersion( String version );
}

View File

@@ -0,0 +1,466 @@
package org.apache.maven.artifact.versioning;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Properties;
import java.util.Stack;
/**
* Generic implementation of version comparison. Features:
* <ul>
* <li>mixing of '<code>-</code>' (dash) and '<code>.</code>' (dot) separators,</li>
* <li>transition between characters and digits also constitutes a separator:
* <code>1.0alpha1 =&gt; [1, 0, alpha, 1]</code></li>
* <li>unlimited number of version components,</li>
* <li>version components in the text can be digits or strings</li>
* <li>strings are checked for well-known qualifiers and the qualifier ordering is used for version ordering.
* Well-known qualifiers (case insensitive):<ul>
* <li><code>snapshot</code></li>
* <li><code>alpha</code> or <code>a</code></li>
* <li><code>beta</code> or <code>b</code></li>
* <li><code>milestone</code> or <code>m</code></li>
* <li><code>rc</code> or <code>cr</code></li>
* <li><code>(the empty string)</code> or <code>ga</code> or <code>final</code></li>
* <li><code>sp</code></li>
* </ul>
* </li>
* <li>a dash usually precedes a qualifier, and is always less important than something preceded with a dot.</li>
* </ul>
*
* @see <a href="http://docs.codehaus.org/display/MAVEN/Versioning">"Versioning" on Maven Wiki</a>
* @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
* @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
* @version $Id: ComparableVersion.java 958295 2010-06-26 23:16:18Z hboutemy $
*/
public class ComparableVersion
implements Comparable<ComparableVersion>
{
private String value;
private String canonical;
private ListItem items;
private interface Item
{
final int INTEGER_ITEM = 0;
final int STRING_ITEM = 1;
final int LIST_ITEM = 2;
int compareTo( Item item );
int getType();
boolean isNull();
}
/**
* Represents a numeric item in the version item list.
*/
private static class IntegerItem
implements Item
{
private static final BigInteger BigInteger_ZERO = new BigInteger( "0" );
private final BigInteger value;
public static final IntegerItem ZERO = new IntegerItem();
private IntegerItem()
{
this.value = BigInteger_ZERO;
}
public IntegerItem( String str )
{
this.value = new BigInteger( str );
}
public int getType()
{
return INTEGER_ITEM;
}
public boolean isNull()
{
return BigInteger_ZERO.equals( value );
}
public int compareTo( Item item )
{
if ( item == null )
{
return BigInteger_ZERO.equals( value ) ? 0 : 1; // 1.0 == 1, 1.1 > 1
}
switch ( item.getType() )
{
case INTEGER_ITEM:
return value.compareTo( ( (IntegerItem) item ).value );
case STRING_ITEM:
return 1; // 1.1 > 1-sp
case LIST_ITEM:
return 1; // 1.1 > 1-1
default:
throw new RuntimeException( "invalid item: " + item.getClass() );
}
}
public String toString()
{
return value.toString();
}
}
/**
* Represents a string in the version item list, usually a qualifier.
*/
private static class StringItem
implements Item
{
private static final String[] QUALIFIERS = { "alpha", "beta", "milestone", "rc", "snapshot", "", "sp" };
private static final List<String> _QUALIFIERS = Arrays.asList( QUALIFIERS );
private static final Properties ALIASES = new Properties();
static
{
ALIASES.put( "ga", "" );
ALIASES.put( "final", "" );
ALIASES.put( "cr", "rc" );
}
/**
* A comparable value for the empty-string qualifier. This one is used to determine if a given qualifier makes
* the version older than one without a qualifier, or more recent.
*/
private static final String RELEASE_VERSION_INDEX = String.valueOf( _QUALIFIERS.indexOf( "" ) );
private String value;
public StringItem( String value, boolean followedByDigit )
{
if ( followedByDigit && value.length() == 1 )
{
// a1 = alpha-1, b1 = beta-1, m1 = milestone-1
switch ( value.charAt( 0 ) )
{
case 'a':
value = "alpha";
break;
case 'b':
value = "beta";
break;
case 'm':
value = "milestone";
break;
}
}
this.value = ALIASES.getProperty( value , value );
}
public int getType()
{
return STRING_ITEM;
}
public boolean isNull()
{
return ( comparableQualifier( value ).compareTo( RELEASE_VERSION_INDEX ) == 0 );
}
/**
* Returns a comparable value for a qualifier.
*
* This method both takes into account the ordering of known qualifiers as well as lexical ordering for unknown
* qualifiers.
*
* just returning an Integer with the index here is faster, but requires a lot of if/then/else to check for -1
* or QUALIFIERS.size and then resort to lexical ordering. Most comparisons are decided by the first character,
* so this is still fast. If more characters are needed then it requires a lexical sort anyway.
*
* @param qualifier
* @return an equivalent value that can be used with lexical comparison
*/
public static String comparableQualifier( String qualifier )
{
int i = _QUALIFIERS.indexOf( qualifier );
return i == -1 ? _QUALIFIERS.size() + "-" + qualifier : String.valueOf( i );
}
public int compareTo( Item item )
{
if ( item == null )
{
// 1-rc < 1, 1-ga > 1
return comparableQualifier( value ).compareTo( RELEASE_VERSION_INDEX );
}
switch ( item.getType() )
{
case INTEGER_ITEM:
return -1; // 1.any < 1.1 ?
case STRING_ITEM:
return comparableQualifier( value ).compareTo( comparableQualifier( ( (StringItem) item ).value ) );
case LIST_ITEM:
return -1; // 1.any < 1-1
default:
throw new RuntimeException( "invalid item: " + item.getClass() );
}
}
public String toString()
{
return value;
}
}
/**
* Represents a version list item. This class is used both for the global item list and for sub-lists (which start
* with '-(number)' in the version specification).
*/
private static class ListItem
extends ArrayList<Item>
implements Item
{
public int getType()
{
return LIST_ITEM;
}
public boolean isNull()
{
return ( size() == 0 );
}
void normalize()
{
for( ListIterator<Item> iterator = listIterator( size() ); iterator.hasPrevious(); )
{
Item item = iterator.previous();
if ( item.isNull() )
{
iterator.remove(); // remove null trailing items: 0, "", empty list
}
else
{
break;
}
}
}
public int compareTo( Item item )
{
if ( item == null )
{
if ( size() == 0 )
{
return 0; // 1-0 = 1- (normalize) = 1
}
Item first = get( 0 );
return first.compareTo( null );
}
switch ( item.getType() )
{
case INTEGER_ITEM:
return -1; // 1-1 < 1.0.x
case STRING_ITEM:
return 1; // 1-1 > 1-sp
case LIST_ITEM:
Iterator<Item> left = iterator();
Iterator<Item> right = ( (ListItem) item ).iterator();
while ( left.hasNext() || right.hasNext() )
{
Item l = left.hasNext() ? left.next() : null;
Item r = right.hasNext() ? right.next() : null;
// if this is shorter, then invert the compare and mul with -1
int result = l == null ? -1 * r.compareTo( l ) : l.compareTo( r );
if ( result != 0 )
{
return result;
}
}
return 0;
default:
throw new RuntimeException( "invalid item: " + item.getClass() );
}
}
public String toString()
{
StringBuilder buffer = new StringBuilder( "(" );
for( Iterator<Item> iter = iterator(); iter.hasNext(); )
{
buffer.append( iter.next() );
if ( iter.hasNext() )
{
buffer.append( ',' );
}
}
buffer.append( ')' );
return buffer.toString();
}
}
public ComparableVersion( String version )
{
parseVersion( version );
}
public final void parseVersion( String version )
{
this.value = version;
items = new ListItem();
version = version.toLowerCase( Locale.ENGLISH );
ListItem list = items;
Stack<Item> stack = new Stack<Item>();
stack.push( list );
boolean isDigit = false;
int startIndex = 0;
for ( int i = 0; i < version.length(); i++ )
{
char c = version.charAt( i );
if ( c == '.' )
{
if ( i == startIndex )
{
list.add( IntegerItem.ZERO );
}
else
{
list.add( parseItem( isDigit, version.substring( startIndex, i ) ) );
}
startIndex = i + 1;
}
else if ( c == '-' )
{
if ( i == startIndex )
{
list.add( IntegerItem.ZERO );
}
else
{
list.add( parseItem( isDigit, version.substring( startIndex, i ) ) );
}
startIndex = i + 1;
if ( isDigit )
{
list.normalize(); // 1.0-* = 1-*
if ( ( i + 1 < version.length() ) && Character.isDigit( version.charAt( i + 1 ) ) )
{
// new ListItem only if previous were digits and new char is a digit,
// ie need to differentiate only 1.1 from 1-1
list.add( list = new ListItem() );
stack.push( list );
}
}
}
else if ( Character.isDigit( c ) )
{
if ( !isDigit && i > startIndex )
{
list.add( new StringItem( version.substring( startIndex, i ), true ) );
startIndex = i;
}
isDigit = true;
}
else
{
if ( isDigit && i > startIndex )
{
list.add( parseItem( true, version.substring( startIndex, i ) ) );
startIndex = i;
}
isDigit = false;
}
}
if ( version.length() > startIndex )
{
list.add( parseItem( isDigit, version.substring( startIndex ) ) );
}
while ( !stack.isEmpty() )
{
list = (ListItem) stack.pop();
list.normalize();
}
canonical = items.toString();
}
private static Item parseItem( boolean isDigit, String buf )
{
return isDigit ? new IntegerItem( buf ) : new StringItem( buf, false );
}
public int compareTo( ComparableVersion o )
{
return items.compareTo( o.items );
}
public String toString()
{
return value;
}
public boolean equals( Object o )
{
return ( o instanceof ComparableVersion ) && canonical.equals( ( (ComparableVersion) o ).canonical );
}
public int hashCode()
{
return canonical.hashCode();
}
}

View File

@@ -0,0 +1,241 @@
package org.apache.maven.artifact.versioning;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import java.util.StringTokenizer;
/**
* Default implementation of artifact versioning.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id: DefaultArtifactVersion.java 828793 2009-10-22 17:34:20Z bentmann $
*/
public class DefaultArtifactVersion
implements ArtifactVersion
{
private Integer majorVersion;
private Integer minorVersion;
private Integer incrementalVersion;
private Integer buildNumber;
private String qualifier;
private ComparableVersion comparable;
public DefaultArtifactVersion( String version )
{
parseVersion( version );
}
@Override
public int hashCode()
{
return 11 + comparable.hashCode();
}
@Override
public boolean equals( Object other )
{
if ( this == other )
{
return true;
}
if ( !( other instanceof ArtifactVersion ) )
{
return false;
}
return compareTo( other ) == 0;
}
public int compareTo( Object o )
{
DefaultArtifactVersion otherVersion = (DefaultArtifactVersion) o;
return this.comparable.compareTo( otherVersion.comparable );
}
public int getMajorVersion()
{
return majorVersion != null ? majorVersion : 0;
}
public int getMinorVersion()
{
return minorVersion != null ? minorVersion : 0;
}
public int getIncrementalVersion()
{
return incrementalVersion != null ? incrementalVersion : 0;
}
public int getBuildNumber()
{
return buildNumber != null ? buildNumber : 0;
}
public String getQualifier()
{
return qualifier;
}
public final void parseVersion( String version )
{
comparable = new ComparableVersion( version );
int index = version.indexOf( "-" );
String part1;
String part2 = null;
if ( index < 0 )
{
part1 = version;
}
else
{
part1 = version.substring( 0, index );
part2 = version.substring( index + 1 );
}
if ( part2 != null )
{
try
{
if ( ( part2.length() == 1 ) || !part2.startsWith( "0" ) )
{
buildNumber = Integer.valueOf( part2 );
}
else
{
qualifier = part2;
}
}
catch ( NumberFormatException e )
{
qualifier = part2;
}
}
if ( ( part1.indexOf( "." ) < 0 ) && !part1.startsWith( "0" ) )
{
try
{
majorVersion = Integer.valueOf( part1 );
}
catch ( NumberFormatException e )
{
// qualifier is the whole version, including "-"
qualifier = version;
buildNumber = null;
}
}
else
{
boolean fallback = false;
StringTokenizer tok = new StringTokenizer( part1, "." );
try
{
majorVersion = getNextIntegerToken( tok );
if ( tok.hasMoreTokens() )
{
minorVersion = getNextIntegerToken( tok );
}
if ( tok.hasMoreTokens() )
{
incrementalVersion = getNextIntegerToken( tok );
}
if ( tok.hasMoreTokens() )
{
fallback = true;
}
// string tokenzier won't detect these and ignores them
if ( part1.indexOf( ".." ) >= 0 || part1.startsWith( "." ) || part1.endsWith( "." ) )
{
fallback = true;
}
}
catch ( NumberFormatException e )
{
fallback = true;
}
if ( fallback )
{
// qualifier is the whole version, including "-"
qualifier = version;
majorVersion = null;
minorVersion = null;
incrementalVersion = null;
buildNumber = null;
}
}
}
private static Integer getNextIntegerToken( StringTokenizer tok )
{
String s = tok.nextToken();
if ( ( s.length() > 1 ) && s.startsWith( "0" ) )
{
throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" );
}
return Integer.valueOf( s );
}
@Override
public String toString()
{
StringBuilder buf = new StringBuilder();
if ( majorVersion != null )
{
buf.append( majorVersion );
}
if ( minorVersion != null )
{
buf.append( "." );
buf.append( minorVersion );
}
if ( incrementalVersion != null )
{
buf.append( "." );
buf.append( incrementalVersion );
}
if ( buildNumber != null )
{
buf.append( "-" );
buf.append( buildNumber );
}
else if ( qualifier != null )
{
if ( buf.length() > 0 )
{
buf.append( "-" );
}
buf.append( qualifier );
}
return buf.toString();
}
}