krysalis-version/src/java/org/krysalis/version/impl/data/util VersionDataIterator.java,NONE,1.1 VersionDataRangeIterator.java,NONE,1.1 VersionDataRange.java,NONE,1.1 VersionDataIncrementor.java,NONE,1.1 VersionDataComparison.java,NONE,1.1

[email protected] Sun, 23 Mar 2003 17:25:46 -0800
Newsgroups gmane.comp.krysalis.sandbox
Message-ID <[email protected]>
Update of /cvsroot/metamorphosis/krysalis-version/src/java/org/krysalis/version/impl/data/util
In directory sc8-pr-cvs1:/tmp/cvs-serv27679/src/java/org/krysalis/version/impl/data/util

Added Files:
	VersionDataIterator.java VersionDataRangeIterator.java 
	VersionDataRange.java VersionDataIncrementor.java 
	VersionDataComparison.java 
Log Message:
Cleaned up package naming

--- NEW FILE: VersionDataIterator.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01

Copyright (c) 2002-2003 Nicola Ken Barozzi.  All rights reserved.

This Licence is compatible with the BSD licence as described and 
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.

3. The end-user documentation included with the redistribution,
   if any, must include the following acknowledgment:
      "This product includes software developed for project 
       Krysalis (http://www.krysalis.org/)."
   Alternately, this acknowledgment may appear in the software itself,
   if and wherever such third-party acknowledgments normally appear.

4. The names "Krysalis" and "Nicola Ken Barozzi" and
   "Krysalis Centipede" must not be used to endorse or promote products
   derived from this software without prior written permission. For
   written permission, please contact [email protected].

5. Products derived from this software may not be called "Krysalis",
   "Krysalis Centipede", nor may "Krysalis" appear in their name, without
    prior written permission of Nicola Ken Barozzi.

6. This software may contain voluntary contributions made by many 
   individuals, who decided to donate the code to this project in respect 
   of this licence.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.impl.data.util;

import java.util.Iterator;

import org.krysalis.version.impl.data.VersionData;
import org.krysalis.version.impl.data.VersionDataElement;

/**
 * @author ajack
 */
public class VersionDataIterator implements Iterator {

    protected VersionDataIncrementor m_incrementor = null;

    protected boolean m_unused = true;
    protected VersionData m_first = null;

    protected VersionData m_next = null;

    public VersionDataIterator(
        VersionData start,
        VersionDataIncrementor incrementor) {
        m_incrementor = incrementor;

        m_next = m_first = start;
    }

    public VersionDataIterator(final VersionData start) {
        m_incrementor = VersionDataIncrementor.BUILD_NUMBER;

        m_next = m_first = start;
    }

    public VersionDataIterator() {

        m_incrementor = VersionDataIncrementor.BUILD_NUMBER;

        m_next =
            m_first =
                new VersionData(
                    VersionDataElement.MIN_MAJOR,
                    VersionDataElement.MIN_MINOR,
                    VersionDataElement.MIN_RELEASE_LEVEL,
                    VersionDataElement.MIN_RELEASE_QUALIFIER,
                    VersionDataElement.MIN_POINT,
                    VersionDataElement.MIN_BUILD_NUMBER);
    }

    /**
     * @see java.util.Iterator#hasNext()
     */
    public boolean hasNext() {
        return (null != m_next);
    }

    /**
     * @see java.util.Iterator#next()
     */
    public Object next() {
        VersionData currentData = m_next;

        //VersionLogger.g_log.debug("next m_ :" + m_next);
        //VersionLogger.g_log.debug("data pre :" + currentData);

        skipToNext();

        //VersionLogger.g_log.debug("data post :" + currentData);

        return currentData;
    }

    /**
     * @see java.util.Iterator#remove()
     */
    public void remove() {
        skipToNext();
    }

    protected void skipToNext() {
        if (m_next != null) {
            //VersionLogger.g_log.debug("Skip To Next:" + m_next);

            m_next = m_incrementor.increment(m_next);

            //VersionLogger.g_log.debug("Next:" + m_next);
        }
    }

    public static void main(String args[]) throws Exception {
        VersionData lower =
            new VersionData(
                VersionDataElement.MAX_MAJOR - 10,
                VersionDataElement.MAX_MAJOR - 10,
                "alpha",
                VersionDataElement.MAX_RELEASE_QUALIFIER - 10,
                VersionDataElement.MAX_POINT - 10,
                VersionDataElement.MAX_BUILD_NUMBER - 10);

        for (Iterator elements = VersionDataElement.ELEMENTS.iterator();
            elements.hasNext();
            ) {
            VersionDataElement element = (VersionDataElement) elements.next();

            System.out.println("Incrementing Element:" + element);

            int count = 0;
            for (Iterator i =
                new VersionDataIterator(
                    lower,
                    new VersionDataIncrementor(element));
                i.hasNext();
                ++count) {

                VersionData next = (VersionData) i.next();

                if (0 == (count % 10000))
                    System.out.println(next);
            }

            System.out.println("Total Count:" + count);
        }
    }

}

--- NEW FILE: VersionDataRangeIterator.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01

Copyright (c) 2002-2003 Nicola Ken Barozzi.  All rights reserved.

This Licence is compatible with the BSD licence as described and 
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.

3. The end-user documentation included with the redistribution,
   if any, must include the following acknowledgment:
      "This product includes software developed for project 
       Krysalis (http://www.krysalis.org/)."
   Alternately, this acknowledgment may appear in the software itself,
   if and wherever such third-party acknowledgments normally appear.

4. The names "Krysalis" and "Nicola Ken Barozzi" and
   "Krysalis Centipede" must not be used to endorse or promote products
   derived from this software without prior written permission. For
   written permission, please contact [email protected].

5. Products derived from this software may not be called "Krysalis",
   "Krysalis Centipede", nor may "Krysalis" appear in their name, without
    prior written permission of Nicola Ken Barozzi.

6. This software may contain voluntary contributions made by many 
   individuals, who decided to donate the code to this project in respect 
   of this licence.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.impl.data.util;

import java.util.Iterator;

import org.krysalis.version.impl.data.VersionData;

/**
 * @author ajack
 */
public class VersionDataRangeIterator extends VersionDataIterator {
    protected VersionDataRange m_range = null;

    public VersionDataRangeIterator(
        VersionData lower,
    VersionData higher) {
        super(lower);

        m_range =
            new VersionDataRange(
                VersionDataComparison.getGreaterThanOrEqual(lower),
                VersionDataComparison.getLessThanOrEqual(higher));
    }

    public VersionDataRangeIterator(
    VersionData lower,
    VersionData higher,
        VersionDataIncrementor incrementor) {
        super(lower, incrementor);

        m_range =
            new VersionDataRange(
                VersionDataComparison.getGreaterThanOrEqual(lower),
                VersionDataComparison.getLessThanOrEqual(higher));
    }

    public VersionDataRangeIterator(VersionDataRange range) {
        super(range.getLower().getComparison());

        m_range = range;
    }

    public VersionDataRangeIterator(
        VersionDataRange range,
        VersionDataIncrementor incrementor) {
        super(range.getLower().getComparison(), incrementor);

        m_range = range;
    }

    /**
     * @see java.util.Iterator#hasNext()
     */
    public boolean hasNext() {
        return (null != m_next);
    }

    /**
     * @see java.util.Iterator#remove()
     */
    public void remove() {
        skipToNext();
    }

    protected void skipToNext() {
        super.skipToNext();

        if (m_next != null) {
            //VersionLogger.g_log.debug("RSkip :" + m_next);

            if (!m_range.inRange(m_next))
                m_next = null;

            //VersionLogger.g_log.debug("RNext:" + m_next);
        }
    }

    public static void main(String args[]) throws Exception {
        VersionData lower = new VersionData(1, 1, "alpha", 1, 1, 1);
        VersionData upper = new VersionData(1, 1, "alpha", 1, 1, 10);

        int count = 0;
        for (Iterator i =
            new VersionDataRangeIterator(
                lower,
                upper,
                VersionDataIncrementor.POINT);
            i.hasNext();
            ++count) {
            System.out.println("Count:" + count);
            i.next();
        }

        System.out.println("Total Count:" + count);
    }
}

--- NEW FILE: VersionDataRange.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01

Copyright (c) 2002-2003 Nicola Ken Barozzi.  All rights reserved.

This Licence is compatible with the BSD licence as described and 
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.

3. The end-user documentation included with the redistribution,
   if any, must include the following acknowledgment:
      "This product includes software developed for project 
       Krysalis (http://www.krysalis.org/)."
   Alternately, this acknowledgment may appear in the software itself,
   if and wherever such third-party acknowledgments normally appear.

4. The names "Krysalis" and "Nicola Ken Barozzi" and
   "Krysalis Centipede" must not be used to endorse or promote products
   derived from this software without prior written permission. For
   written permission, please contact [email protected].

5. Products derived from this software may not be called "Krysalis",
   "Krysalis Centipede", nor may "Krysalis" appear in their name, without
    prior written permission of Nicola Ken Barozzi.

6. This software may contain voluntary contributions made by many 
   individuals, who decided to donate the code to this project in respect 
   of this licence.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.impl.data.util;

import org.krysalis.version.exception.VersionError;
import org.krysalis.version.impl.KrysalisVersion;
import org.krysalis.version.impl.data.VersionData;

/**
 * @author ajack
 */
public class VersionDataRange {
    private VersionDataComparison m_lower = null;
    private VersionDataComparison m_upper = null;

    public VersionDataRange(VersionData lower, VersionData upper) {
        m_lower = VersionDataComparison.getGreaterThanOrEqual(lower);
        m_upper = VersionDataComparison.getLessThanOrEqual(upper);

        check();
    }

    public VersionDataRange(
        VersionDataComparison lower,
        VersionDataComparison upper) {
        m_lower = lower;
        m_upper = upper;

        check();
    }

    private void check() {
        if (m_lower.getComparison().compareTo(m_upper.getComparison()) < 0)
            throw new VersionError(
                "Lower and Upper Mis-configured, Lower ["
                    + m_lower
                    + "] Upper ["
                    + m_upper
                    + "]");

        if ((VersionDataComparison.isEqual(m_lower)
            || VersionDataComparison.isEqual(m_upper))
            && !m_lower.equals(m_upper)) {
            throw new VersionError(
                "Lower and Upper Inconsistent (Equal Operator w/o Unary Range), Lower ["
                    + m_lower
                    + "] Upper ["
                    + m_upper
                    + "]");
        }

        if (!VersionDataComparison.isUpperFacing(m_lower)) {
            throw new VersionError(
                "Lower misconfigured for lower bound [" + m_lower + "]");
        }

        if (!VersionDataComparison.isLowerFacing(m_upper)) {
            throw new VersionError(
                "Upper misconfigured for upper bound [" + m_upper + "]");
        }
    }

    public boolean inRange(VersionData test) {
        boolean result = false;
        result = m_lower.evaluate(test);
        result &= m_upper.evaluate(test);
        return result;
    } /**
                       * Returns the lower.
                       * @return VersionDataComparison
                       */
    public VersionDataComparison getLower() {
        return m_lower;
    } 
    
    
    /**
     * Returns the upper.
     * @return VersionDataComparison
     */
    public VersionDataComparison getUpper() {
        return m_upper;
    } 
    
    /**
    * Sets the lower.
    * @param lower The lower to set
    */
    public void setLower(VersionDataComparison lower) {
        m_lower = lower;
    } 
    
    
    /**
    * Sets the upper.
    * @param upper The upper to set
    */
    public void setUpper(VersionDataComparison upper) {
        m_upper = upper;
    }
}

--- NEW FILE: VersionDataIncrementor.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01

Copyright (c) 2002-2003 Nicola Ken Barozzi.  All rights reserved.

This Licence is compatible with the BSD licence as described and 
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.

3. The end-user documentation included with the redistribution,
   if any, must include the following acknowledgment:
      "This product includes software developed for project
       Krysalis (http://www.krysalis.org/)."
   Alternately, this acknowledgment may appear in the software itself,
   if and wherever such third-party acknowledgments normally appear.

4. The names "Krysalis" and "Nicola Ken Barozzi" and
   "Krysalis Centipede" must not be used to endorse or promote products
   derived from this software without prior written permission. For
   written permission, please contact [email protected].

5. Products derived from this software may not be called "Krysalis",
   "Krysalis Centipede", nor may "Krysalis" appear in their name, without
    prior written permission of Nicola Ken Barozzi.

6. This software may contain voluntary contributions made by many 
   individuals, who decided to donate the code to this project in respect 
   of this licence.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.impl.data.util;

import java.util.Iterator;

import org.krysalis.version.impl.data.ReleaseLevel;
import org.krysalis.version.impl.data.VersionData;
import org.krysalis.version.impl.data.VersionDataElement;

/**
 * @author [email protected]
 */
public class VersionDataIncrementor {

    private VersionDataElement m_element = null;

    public static final VersionDataIncrementor BUILD_NUMBER =
        new VersionDataIncrementor(VersionDataElement.BUILD_NUMBER);
    public static final VersionDataIncrementor RELEASE_LEVEL =
        new VersionDataIncrementor(VersionDataElement.RELEASE_LEVEL);
    public static final VersionDataIncrementor POINT =
        new VersionDataIncrementor(VersionDataElement.POINT);
    public static final VersionDataIncrementor MINOR =
        new VersionDataIncrementor(VersionDataElement.MINOR);
    public static final VersionDataIncrementor MAJOR =
        new VersionDataIncrementor(VersionDataElement.MAJOR);

    public VersionDataIncrementor(VersionDataElement element) {

        m_element = element;
    }

    // :TODO: public VersionDataIncrementor getForElement(VersionDataElement element) 

    public VersionData increment(final VersionData initial) {
        VersionData inc = null;

        //
        // Current settings
        //
        int buildNumber = initial.getBuildNumber();
        int releaseQualifier = initial.getReleaseQualifier();
        ReleaseLevel releaseLevel = initial.getReleaseLevel();
        int point = initial.getPoint();
        int minor = initial.getMinor();
        int major = initial.getMajor();

        if (m_element == VersionDataElement.BUILD_NUMBER)
            buildNumber++;

        if (buildNumber > VersionDataElement.MAX_BUILD_NUMBER) {
            buildNumber = VersionDataElement.MIN_BUILD_NUMBER;
            releaseQualifier++;
        }

        if (m_element == VersionDataElement.RELEASE_QUALIFIER)
            releaseQualifier++;

        ReleaseLevel incLevel = releaseLevel;
        if (releaseQualifier > VersionDataElement.MAX_RELEASE_QUALIFIER) {
            releaseQualifier = VersionDataElement.MIN_RELEASE_QUALIFIER;
            incLevel = releaseLevel.nextLevel();
        }

        if (m_element == VersionDataElement.RELEASE_LEVEL)
            incLevel = releaseLevel.nextLevel();

        if (null == incLevel) {
            releaseLevel = VersionDataElement.MIN_RELEASE_LEVEL;
            point++;
        }

        if (m_element == VersionDataElement.POINT)
            point++;

        if (point > VersionDataElement.MAX_POINT) {
            point = VersionDataElement.MIN_POINT;

            minor++;
        }

        if (m_element == VersionDataElement.MINOR)
            minor++;

        if (minor > VersionDataElement.MAX_MINOR) {
            minor = VersionDataElement.MIN_MINOR;
            major++;
        }

        if (m_element == VersionDataElement.MAJOR)
            major++;

        if (major <= VersionDataElement.MAX_MAJOR)
            inc =
                new VersionData(
                    major,
                    minor,
                    releaseLevel,
                    releaseQualifier,
                    point,
                    buildNumber);

        return inc;
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return super.toString() + m_element;
    }

    public static void main(String args[]) {
        for (Iterator elements = VersionDataElement.ELEMENTS.iterator();
            elements.hasNext();
            ) {
            VersionDataElement element = (VersionDataElement) elements.next();

            System.out.println("Incrementing Element:" + element);

            VersionData data1 = new VersionData();

            VersionData data2 =
                new VersionDataIncrementor(element).increment(data1);

            System.out.println("Version Data1: " + data1);
            System.out.println("Version Data2: " + data2);
        }
    }
}

--- NEW FILE: VersionDataComparison.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01

Copyright (c) 2002-2003 Nicola Ken Barozzi.  All rights reserved.

This Licence is compatible with the BSD licence as described and 
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.

3. The end-user documentation included with the redistribution,
   if any, must include the following acknowledgment:
      "This product includes software developed for project 
       Krysalis (http://www.krysalis.org/)."
   Alternately, this acknowledgment may appear in the software itself,
   if and wherever such third-party acknowledgments normally appear.

4. The names "Krysalis" and "Nicola Ken Barozzi" and
   "Krysalis Centipede" must not be used to endorse or promote products
   derived from this software without prior written permission. For
   written permission, please contact [email protected].

5. Products derived from this software may not be called "Krysalis",
   "Krysalis Centipede", nor may "Krysalis" appear in their name, without
    prior written permission of Nicola Ken Barozzi.

6. This software may contain voluntary contributions made by many 
   individuals, who decided to donate the code to this project in respect 
   of this licence.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.impl.data.util;

// :TODO: Can we use ? import java.util.Comparator;

import org.krysalis.version.exception.VersionError;
import org.krysalis.version.exception.VersionException;
import org.krysalis.version.impl.KrysalisVersion;
import org.krysalis.version.impl.data.VersionData;

/**
 * @author ajack
 */
public class VersionDataComparison {
    public static final int TYPE_UNSET = 0;
    public static final int TYPE_LESS_THAN = 1;
    public static final int TYPE_LESS_THAN_OR_EQUAL = 2;
    public static final int TYPE_EQUAL = 3;
    public static final int TYPE_COMPATIBLE_WITH = 4;
    public static final int TYPE_NOT_EQUAL = 5;
    public static final int TYPE_GREATER_THAN = 6;
    public static final int TYPE_GREATER_THAN_OR_EQUAL = 7;

    private int m_type = TYPE_UNSET;
    private VersionData m_comparisonVersion = null;

    public static VersionDataComparison getEqual(VersionData comparisonData) {
        return new VersionDataComparison(TYPE_EQUAL, comparisonData);
    }

    public static VersionDataComparison getNotEqual(VersionData comparisonData) {
        return new VersionDataComparison(TYPE_NOT_EQUAL, comparisonData);
    }

    public static VersionDataComparison getLessThan(VersionData comparisonData) {
        return new VersionDataComparison(TYPE_LESS_THAN, comparisonData);
    }

    public static VersionDataComparison getLessThanOrEqual(VersionData comparisonData) {
        return new VersionDataComparison(
            TYPE_LESS_THAN_OR_EQUAL,
            comparisonData);
    }

    public static VersionDataComparison getGreaterThan(VersionData comparison) {
        return new VersionDataComparison(TYPE_GREATER_THAN, comparison);
    }

    public static VersionDataComparison getGreaterThanOrEqual(VersionData comparisonData) {
        return new VersionDataComparison(
            TYPE_GREATER_THAN_OR_EQUAL,
            comparisonData);
    }

    public VersionDataComparison(int type, VersionData comparisonData) {
        m_type = type;
        m_comparisonVersion = comparisonData;
    }

    public VersionDataComparison(String type, VersionData comparisonData)
        throws VersionException {
        m_type = getTypeForString(type);
        m_comparisonVersion = comparisonData;
    }

    public VersionDataComparison(VersionData comparisonData) {
        m_type = TYPE_EQUAL;
        m_comparisonVersion = comparisonData;
    }

    public static boolean isEqual(VersionDataComparison comp) {
        return TYPE_EQUAL == comp.m_type;
    }

    public static boolean isNotEqual(VersionDataComparison comp) {
        return TYPE_NOT_EQUAL == comp.m_type;
    }

    public static boolean isCompatibleWith(VersionDataComparison comp) {
        return TYPE_COMPATIBLE_WITH == comp.m_type;
    }

    public static boolean isLessThan(VersionDataComparison comp) {
        return TYPE_LESS_THAN == comp.m_type;
    }

    public static boolean isLessThanOrEqual(VersionDataComparison comp) {
        return TYPE_LESS_THAN_OR_EQUAL == comp.m_type;
    }

    public static boolean isLowerFacing(VersionDataComparison comp) {
        return (isLessThanOrEqual(comp) || isLessThan(comp));
    }

    public static boolean isGreaterThan(VersionDataComparison comp) {
        return TYPE_GREATER_THAN == comp.m_type;
    }

    public static boolean isGreaterThanOrEqual(VersionDataComparison comp) {
        return TYPE_GREATER_THAN_OR_EQUAL == comp.m_type;
    }

    public static boolean isUpperFacing(VersionDataComparison comp) {
        return (isGreaterThanOrEqual(comp) || isGreaterThan(comp));
    }

    public boolean evaluate(VersionData test) {
        boolean result = false;

        int comparison = m_comparisonVersion.compareTo(test);

        switch (m_type) {
            case TYPE_UNSET :
                {
                    result = false;
                    break;
                }

            case TYPE_LESS_THAN :
                {
                    result = (comparison < 0);
                    break;
                }

            case TYPE_LESS_THAN_OR_EQUAL :
                {
                    result = (comparison <= 0);
                    break;
                }

            case TYPE_EQUAL :
                {
                    result = (comparison == 0);
                    break;
                }

            case TYPE_NOT_EQUAL :
                {
                    result = (comparison != 0);
                    break;
                }

            case TYPE_GREATER_THAN :
                {
                    result = (comparison > 0);
                    break;
                }

            case TYPE_GREATER_THAN_OR_EQUAL :
                {
                    result = (comparison >= 0);
                    break;
                }

            default :
                {
                    throw new VersionError("Unknown Comparison Type " + m_type);
                }
        }

        return result;
    }

    private String getStringForType() {
        String representation = "?";

        switch (m_type) {
            case TYPE_UNSET :
                {
                    representation = "?";
                    break;
                }

            case TYPE_LESS_THAN :
                {
                    representation = "<";
                    break;
                }

            case TYPE_LESS_THAN_OR_EQUAL :
                {
                    representation = "<=";
                    break;
                }

            case TYPE_EQUAL :
                {
                    representation = "=";
                    break;
                }

            case TYPE_COMPATIBLE_WITH :
               {
                   representation = "~";
                   break;
               }

            case TYPE_NOT_EQUAL :
                {
                    representation = "<>";
                    break;
                }

            case TYPE_GREATER_THAN :
                {
                    representation = ">";
                    break;
                }

            case TYPE_GREATER_THAN_OR_EQUAL :
                {
                    representation = ">=";
                    break;
                }

            default :
                {
                    throw new VersionError("Unknown Comparison Type " + m_type);
                }
        }

        return representation;
    }

    private String getWordForType() {
        String representation = "Unknown";

        switch (m_type) {
            case TYPE_UNSET :
                {
                    representation = "Unset";
                    break;
                }

            case TYPE_LESS_THAN :
                {
                    representation = "LessThan";
                    break;
                }

            case TYPE_LESS_THAN_OR_EQUAL :
                {
                    representation = "ThankThanOrEqualTo";
                    break;
                }

            case TYPE_EQUAL :
                {
                    representation = "EqualTo";
                    break;
                }

            case TYPE_COMPATIBLE_WITH :
                {
                    representation = "CompatibleWith";
                    break;
                }

            case TYPE_NOT_EQUAL :
                {
                    representation = "NotEqualTo";
                    break;
                }

            case TYPE_GREATER_THAN :
                {
                    representation = "GreaterThan";
                    break;
                }

            case TYPE_GREATER_THAN_OR_EQUAL :
                {
                    representation = "GreaterThanOrEqualTo";
                    break;
                }
        }

        return representation;
    }

    private int getTypeForString(String type) throws VersionException {
        int typeVal = TYPE_UNSET;

        if ("<".equals(type) || "LessThan".equalsIgnoreCase(type))
            typeVal = TYPE_LESS_THAN;
        else if (
            "<=".equals(type)
                || "LessThanOrEqualTo".equalsIgnoreCase(type))
            typeVal = TYPE_LESS_THAN_OR_EQUAL;
        else if ("=".equals(type) || "EqualTo".equalsIgnoreCase(type))
            typeVal = TYPE_EQUAL;
        else if ("~".equals(type) || "CompatibleWith".equalsIgnoreCase(type))
            typeVal = TYPE_COMPATIBLE_WITH;
        else if ("!=".equals(type) || "NotEqualTo".equalsIgnoreCase(type))
            typeVal = TYPE_NOT_EQUAL;
        else if (">".equals(type) || "GreaterThan".equalsIgnoreCase(type))
            typeVal = TYPE_GREATER_THAN;
        else if (
            ">=".equals(type)
                || "GreaterThanOrEqualTo".equalsIgnoreCase(type))
            typeVal = TYPE_GREATER_THAN_OR_EQUAL;
        else
            throw new VersionException("Unknown Operation : [" + type + "]");

        return typeVal;
    }

    /**
     * Returns the comparisonData.
     * @return VersionData
     */
    public VersionData getComparison() {
        return m_comparisonVersion;
    }

    /**
     * Sets the comparisonData.
     * @param comparisonData The comparisonData to set
     */
    public void setComparison(VersionData comparisonData) {
        m_comparisonVersion = comparisonData;
    }

    public String toString() {
        return getWordForType() + " : " + m_comparisonVersion;
    }
}




-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open! 
Get cracking and register here for some mind boggling fun and 
the chance of winning an Apple iPod:
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en