krysalis-update/src/java/org/krysalis/depot/update/artifact/compare ArtifactGroupNameComparator.java,NONE,1.1 StandardArtifactComparisons.java,NONE,1.1 AbstractArtifactGroupComparator.java,NONE,1.1 AbstractArtifactComparator.java,NONE,1.1 TypeComparator.java,NONE,1.1 NameComparator.java,NONE,1.1 VersionComparator.java,NONE,1.1 FilenameComparator.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:45 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/artifact/compare
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/artifact/compare

Added Files:
	ArtifactGroupNameComparator.java 
	StandardArtifactComparisons.java 
	AbstractArtifactGroupComparator.java 
	AbstractArtifactComparator.java TypeComparator.java 
	NameComparator.java VersionComparator.java 
	FilenameComparator.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: FilenameComparator.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.depot.update.artifact.compare;

import java.util.Comparator;

import org.krysalis.depot.update.ArtifactInstance;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;

/**
 * @author arb_jack
 */
public final class FilenameComparator extends AbstractArtifactComparator {

	private static final FilenameComparator INSTANCE = new FilenameComparator();

	/**
	 * @param expression
	 */
	private FilenameComparator() {
		super(Messages.getString(MessageConstants.FILENAME_COMPARATOR));
	}

	public Comparator getInstance() {
		return INSTANCE;
	}

	public int compareResources(final ArtifactInstance artifact1, final ArtifactInstance artifact2) {
		int comparison =
			artifact1.getLocator().getFilename().compareToIgnoreCase(
				artifact2.getLocator().getFilename());
		return comparison;
	}
}

--- NEW FILE: ArtifactGroupNameComparator.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.depot.update.artifact.compare;

import java.util.Comparator;

import org.krysalis.depot.update.artifact.ArtifactGroup;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;

/**
 * @author arb_jack
 * 
 * Compare two entities by their group name.
 */
public final class ArtifactGroupNameComparator extends AbstractArtifactGroupComparator {

	private static final ArtifactGroupNameComparator INSTANCE = new ArtifactGroupNameComparator();

	/**
	 * @param expression
	 */
	private ArtifactGroupNameComparator() {
		super(Messages.getString(MessageConstants.NAME_COMPARATOR));
	}

	public static Comparator getInstance() {
		return INSTANCE;
	}

	public int compareResourceGroups(
		final ArtifactGroup resourceGroup1,
		final ArtifactGroup resourceGroup2) {
		int comparison =
			resourceGroup1.getName().compareToIgnoreCase(
				resourceGroup2.getName());
		return comparison;
	}
}

--- NEW FILE: NameComparator.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.depot.update.artifact.compare;

import java.util.Comparator;

import org.krysalis.depot.update.ArtifactInstance;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;

/**
 * @author arb_jack
 * 
 * Compare two entities by name (i.e. id).
 */
public final class NameComparator extends AbstractArtifactComparator {

	private static final NameComparator INSTANCE = new NameComparator();

	/**
	 * @param expression
	 */
	private NameComparator() {
		super(Messages.getString(MessageConstants.NAME_COMPARATOR));
	}

	public static Comparator getInstance() {
		return INSTANCE;
	}

	public int compareResources(
		final ArtifactInstance artifact1,
		final ArtifactInstance artifact2) {
		int comparison =
			artifact1.getIdentifier().getId().compareToIgnoreCase(
				artifact2.getIdentifier().getId());
		return comparison;
	}
}

--- NEW FILE: AbstractArtifactComparator.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.depot.update.artifact.compare;

import java.util.Comparator;

import org.krysalis.depot.update.ArtifactInstance;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;

/**
 * @author arb_jack
 */
public abstract class AbstractArtifactComparator implements Comparator {
	private String m_description = null;

	protected AbstractArtifactComparator(String description) {
		m_description = description;
	}

	public int compare(final Object object1, final Object object2) {
		int comparison = 0;

		if (object1 instanceof ArtifactInstance) {
			if (object2 instanceof ArtifactInstance) {

				//				System.out.println(
				//					SystemUtils.getShortName(getClass())
				//						+ ": "
				//						+ object1
				//						+ " & "
				//						+ object2);

				comparison =
					compareResources((ArtifactInstance) object1, (ArtifactInstance) object2);
			}
			else {
				throw new IllegalArgumentException(
					Messages.getString(
						MessageConstants.WRONG_TYPE,
						new Object[] {
							object2.getClass().getName(),
							ArtifactInstance.class.getName()}));
			}
		}
		else {
			throw new IllegalArgumentException(
				Messages.getString(
					MessageConstants.WRONG_TYPE,
					new Object[] {
						object1.getClass().getName(),
						ArtifactInstance.class.getName()}));
		}

		return comparison;
	}

	public String toString() {
		return m_description;
	}

	public abstract int compareResources(
		final ArtifactInstance artifact1,
		final ArtifactInstance artifact2);
}

--- NEW FILE: VersionComparator.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.depot.update.artifact.compare;
import java.util.Comparator;

import org.krysalis.depot.update.ArtifactInstance;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;
import org.krysalis.version.Version;

/**
 * Compare two Resources by Version.
 */
public final class VersionComparator extends AbstractArtifactComparator {
	public static final Comparator INSTANCE = new VersionComparator();
	/**
	 * Campare to Resources by Version in reverse order.
	 */
	public static final Comparator REVERSE = new Comparator() {
		public int compare(final Object arg0, final Object arg1) {
			return INSTANCE.compare(arg1, arg0);
		}
	};
	/**
	 * @param expression
	 */
	private VersionComparator() {
		super(Messages.getString(MessageConstants.VERSION_COMPARATOR));
	}
	public static Comparator getInstance() {
		return INSTANCE;
	}
	public int compareResources(final ArtifactInstance artifact1,
			final ArtifactInstance artifact2) {
		final Version version1 = artifact1.getArtifact().getVersion();
		final Version version2 = artifact2.getArtifact().getVersion();
		int comparison = 0;
		if (null != version1) {
			if (null != version2) {
				comparison = version1.compareTo(version2);
			} else
				comparison = 1;
		} else
			comparison = (null == version2) ? 0 : -1;
		return comparison;
	}
}

--- NEW FILE: TypeComparator.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.depot.update.artifact.compare;

import java.util.Comparator;

import org.krysalis.depot.update.ArtifactInstance;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;

/**
 * @author arb_jack
 * 
 * Compare two resources by their Type.
 */
public final class TypeComparator extends AbstractArtifactComparator {

	private static final TypeComparator INSTANCE = new TypeComparator();

	/**
	 * @param expression
	 */
	private TypeComparator() {
		super(Messages.getString(MessageConstants.TYPE_COMPARATOR));
	}

	public static Comparator getInstance() {
		return INSTANCE;
	}

	public int compareResources(
		final ArtifactInstance artifact1,
		final ArtifactInstance artifact2) {
		int comparison =
			artifact1.getArtifact().getType().compareTo(
				artifact2.getArtifact().getType());
		return comparison;
	}
}

--- NEW FILE: StandardArtifactComparisons.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.depot.update.artifact.compare;

import org.krysalis.depot.update.util.compare.ComparatorSequence;


/**
 * @author arb_jack
 * @author anou_mana
 */
public class StandardArtifactComparisons {

	private static ComparatorSequence l_nameTypeVersion = null;
	private static ComparatorSequence l_nameVersion = null;
	private static ComparatorSequence l_name = null;
	private static ComparatorSequence l_version = null;
	private static ComparatorSequence l_nameType = null;

	static {
		l_nameTypeVersion = new ComparatorSequence();
		l_nameTypeVersion.addComparator(NameComparator.getInstance());
		l_nameTypeVersion.addComparator(TypeComparator.getInstance());
		l_nameTypeVersion.addComparator(VersionComparator.getInstance());

		l_nameVersion = new ComparatorSequence();
		l_nameVersion.addComparator(NameComparator.getInstance());
		l_nameVersion.addComparator(VersionComparator.getInstance());
		
		l_name = new ComparatorSequence();
		l_name.addComparator(NameComparator.getInstance());
		
		l_version = new ComparatorSequence();
		l_version.addComparator(VersionComparator.getInstance());
		
		l_nameType = new ComparatorSequence();
		l_nameType.addComparator(NameComparator.getInstance());
		l_nameType.addComparator(TypeComparator.getInstance());
	}

	/**
	 * @return
	 */
	public static ComparatorSequence getNameTypeVersionComparatorSequence() {
		return l_nameTypeVersion;
	}
	
	/**
	 * @return
	 */
	public static ComparatorSequence getNameTypeComparatorSequence() {
		return l_nameType;
	}
	
	/**
	 * @return
	 */
	public static ComparatorSequence getNameVersionComparatorSequence() {
		return l_nameVersion;
	}
	
	/**
	 * @return
	 */
	public static ComparatorSequence getVersionComparatorSequence() {
		return l_version;
	}
	
	/**
	 * @return
	 */
	public static ComparatorSequence getNameComparatorSequence() {
		return l_name;
	}
}

--- NEW FILE: AbstractArtifactGroupComparator.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.depot.update.artifact.compare;

import java.util.Comparator;

import org.krysalis.depot.update.artifact.ArtifactGroup;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;

/**
 * @author arb_jack
 */
public abstract class AbstractArtifactGroupComparator implements Comparator {
	private String m_description = null;

	protected AbstractArtifactGroupComparator(String description) {
		m_description = description;
	}

	public int compare(final Object object1, final Object object2) {
		int comparison = 0;

		if (object1 instanceof ArtifactGroup) {
			if (object2 instanceof ArtifactGroup) {

				//				System.out.println(
				//					SystemUtils.getShortName(getClass())
				//						+ ": "
				//						+ object1
				//						+ " & "
				//						+ object2);

				comparison =
					compareResourceGroups((ArtifactGroup) object1, (ArtifactGroup) object2);
			}
			else {
				throw new IllegalArgumentException(
					Messages.getString(
						MessageConstants.WRONG_TYPE,
						new Object[] {
							object2.getClass().getName(),
							ArtifactGroup.class.getName()}));
			}
		}
		else {
			throw new IllegalArgumentException(
				Messages.getString(
					MessageConstants.WRONG_TYPE,
					new Object[] {
						object1.getClass().getName(),
						ArtifactGroup.class.getName()}));
		}

		return comparison;
	}

	public String toString() {
		return m_description;
	}

	public abstract int compareResourceGroups(
		final ArtifactGroup artifactGroup1,
		final ArtifactGroup artifactGroup2);
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/