⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classpath.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.model;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.jdt.core.IClasspathEntry;

import eclipseme.core.persistence.IPersistable;
import eclipseme.core.persistence.IPersistenceProvider;
import eclipseme.core.persistence.PersistenceException;

/**
 * A representation of a classpath that can be converted to
 * various other representations.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.2 $
 * <br>
 * $Date: 2006/03/02 01:40:21 $
 * <br>
 * @author Craig Setera
 */
public class Classpath implements IPersistable
{
	private ArrayList entries;
	
	/**
	 * Construct a new classpath instance 
	 */
	public Classpath() {
		super();
		entries = new ArrayList();
	}

	/**
	 * Add a new classpath entry.
	 * 
	 * @param pathEntry
	 */
	public void addEntry(ILibrary pathEntry) {
		entries.add(pathEntry);
	}
	
	/**
	 * Return the classpath as a set of Eclipse JDT classpath entries.
	 * @return
	 */
	public IClasspathEntry[] asClasspathEntries() {
		ILibrary[] allEntries = getEntries();
		IClasspathEntry[] cpEntries = new IClasspathEntry[allEntries.length];
		
		for (int i = 0; i < cpEntries.length; i++) {
			cpEntries[i] = allEntries[i].toClasspathEntry();
		}
		
		return cpEntries;
	}

	/**
	 * Return the classpath as an array of the file entries
	 * in the classpath.
	 * 
	 * @return
	 */
	public File[] asFileArray() {
		ILibrary[] allEntries = getEntries();
		File[] files = new File[allEntries.length];

		for (int i = 0; i < files.length; i++) {
			files[i] = allEntries[i].toFile();
		}
		
		return files;
	}
	
	/**
	 * Return the classpath as an array of URL's.
	 * 
	 * @return
	 */
	public URL[] asURLArray() {
		ILibrary[] allEntries = getEntries();
		URL[] urls = new URL[allEntries.length];
		
		for (int i = 0; i < urls.length; i++) {
			urls[i] = allEntries[i].toURL();
		}
		
		return urls;
	}

	/**
	 * Test the equality of this class with another classpath.
	 * 
	 * @param classpath
	 * @return
	 */
	public boolean equals(Classpath classpath) {
		ILibrary[] myEntries = getEntries();
		ILibrary[] otherEntries = classpath.getEntries();
		
		boolean equals = (myEntries.length == otherEntries.length);
		for (int i = 0; equals && (i < myEntries.length); i++) {
			equals = myEntries[i].equals(otherEntries[i]);
		}
		
		return equals;
	}
	
	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		boolean equals = false;
		
		if (obj instanceof Classpath) {
			equals = equals((Classpath) obj);
		}
		
		return equals;
	}

	/**
	 * Return the entries in the classpath.
	 * 
	 * @return
	 */
	public ILibrary[] getEntries() {
		return (ILibrary[]) entries.toArray(new ILibrary[entries.size()]);
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		ILibrary[] allEntries = getEntries();
		int hashCode = allEntries.length << 24;
		
		for (int i = 0; i < allEntries.length; i++) {
			hashCode = hashCode ^ allEntries[i].hashCode();
		}
		
		return hashCode;
	}

	/**
	 * 
	 * @param persistenceProvider
	 * @throws PersistenceException
	 */
	public void loadUsing(IPersistenceProvider persistenceProvider) 
		throws PersistenceException 
	{
		entries.clear();
		int entryCount = persistenceProvider.loadInteger("entryCount");
		for (int i = 0; i < entryCount; i++) {
			entries.add(persistenceProvider.loadPersistable("entry" + i));
		}
	}

	/**
	 * Remove the specified library from the classpath, if it 
	 * is in the classpath.
	 * 
	 * @param library
	 */
	public void removeEntry(ILibrary library) {
		entries.remove(library);
	}
	
	/**
	 * @see eclipseme.core.persistence.IPersistable#storeUsing(eclipseme.core.persistence.IPersistenceProvider)
	 */
	public void storeUsing(IPersistenceProvider persistenceProvider) 
		throws PersistenceException 
	{
		persistenceProvider.storeInteger("entryCount", entries.size());
		
		int i = 0;
		Iterator iterator = entries.iterator();
		while (iterator.hasNext()) {
			ILibrary library = (ILibrary) iterator.next();
			persistenceProvider.storePersistable("entry" + i++, library);
		}
	}

	/**
	 * Converts the classpath to a classpath string with
	 * platform-dependent path separator characters.
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer();
		
		ILibrary[] allEntries = getEntries();
		for (int i = 0; i < allEntries.length; i++) {
			if (i != 0) {
				sb.append(File.pathSeparatorChar);
			}
			
			sb.append(allEntries[i].toFile());
		}
		
		return sb.toString();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -