|
From: Stefan T. <th...@us...> - 2002-03-07 16:14:51
|
Update of /cvsroot/xpg-xml/org/dinopolis/util
In directory usw-pr-cvs1:/tmp/cvs-serv28270
Added Files:
DSearchPath.java
Log Message:
SearchPath for the DFactory
--- NEW FILE: DSearchPath.java ---
/***********************************************************************
* @(#)$RCSfile: DSearchPath.java,v $ $Revision: 1.1 $ $Date: 2002/03/07 16:14:48 $
*
* Copyright (c) 2002 stefan thalauer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License (LGPL)
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
//----------------------------------------------------------------------
/**
* @author Stefan Thalauer
* @version $Revision: 1.1 $
*/
package org.dinopolis.util;
import java.lang.String;
//----------------------------------------------------------------------
/**
*/
public class DSearchPath
{
protected String[] path_;
//----------------------------------------------------------------------
/**
* The constructor
*/
public DSearchPath(String[] path)
{
setSearchPath(path);
}
//----------------------------------------------------------------------
/**
* This method sets the SearchPath
*
* @param path represents the SearchPath
* @return void
* @exception IllegalArgumentException thrown if path is null
* or has length 0
*/
public void setSearchPath(String[] path)
throws IllegalArgumentException
{
if (path == null)
throw(new IllegalArgumentException("SearchPath must not be <null>"));
if (path.length == 0)
throw(new IllegalArgumentException("the length of SearchPath must not be 0"));
path_ = path;
}
//----------------------------------------------------------------------
/**
* This method checks if a path is in the current SearchPath
*
* @param path
* @return returns true if the new path allready exists in the search
* path
* @exception IllegalStateException thrown if no path has been
* set before
*/
public boolean existPath(String path)
throws IllegalStateException
{
if (path_==null)
{
throw new IllegalStateException();
}
for(int count=0; count < path_.length ; count++)
{
if ( path_[count].equals(path) )
return true;
}
return false;
}
//----------------------------------------------------------------------
/**
* This method returns the position the path
*
* @param path
* @return returns the index if the new path allready exists in the search
* path, -1 if not
* @exception IllegalStateException thrown if no path has been
* set before
*/
public int getPathIndex(String path)
throws IllegalStateException
{
if (path_==null)
{
throw new IllegalStateException();
}
for(int count=0; count < path_.length ; count++)
{
if ( path_[count].equals(path) )
return count;
}
return -1;
}
//----------------------------------------------------------------------
/**
* This method appends a path to the current SearchPath.
*
* @param path
*/
public void appendPath(String path)
{
insertPath(path,path_.length);
}
//----------------------------------------------------------------------
/**
* This method inserts a path at the beginning of the current SearchPath.
*
* @param path
*/
public void insertPath(String path)
{
insertPath(path,0);
}
//----------------------------------------------------------------------
/**
* This method inserts the path at the position in the current SearchPath.
*
* @param path
* @param position
* @exception IllegalStateException thrown if no path has been
* set before
* @exception IllegalArgumentException
*/
public void insertPath(String path,int position)
throws IllegalStateException,IllegalArgumentException
{
if (path_==null)
throw new IllegalStateException();
if (path == null)
throw(new IllegalArgumentException("The Path must not be <null>"));
if ( position < 0 | position > path_.length)
throw(new IllegalArgumentException("The Position is out of the Path boundary"));
if (existPath(path))
System.err.println("Path: \"" + path +"\" allready registered in the Search Path ");
else
{
String[] new_path = new String [path_.length + 1];
for (int count = 0; count< position;count++)
{
new_path[count]=path_[count];
}
new_path[position]=path;
for (int count = position; count< path_.length;count++)
{
new_path[count+1]=path_[count];
}
path_ = new_path;
}
}
//----------------------------------------------------------------------
/**
* This method removes the path to the SearchPath.
*
* @param path
* @exception IllegalStateException thrown if no path has been
* set before
* @exception IllegalArgumentException
*/
public void removePath(String path)
throws IllegalStateException,IllegalStateException
{
if (path == null)
throw(new IllegalArgumentException("The Path must not be <null>"));
int position = getPathIndex(path);
if (position < 0)
System.err.println("Path: \"" + path +"\" is not registered in the Search Path ");
else
{
removePath(position);
}
}
//----------------------------------------------------------------------
/**
* This method removes the first path of the SearchPath.
*
* @exception IllegalStateException thrown if no path has been
* set before
* @exception IllegalArgumentException
*/
public void removeFirstPath()
{
removePath(0);
}
//----------------------------------------------------------------------
/**
* This method removes the last path of the SearchPath.
*
* @exception IllegalStateException thrown if no path has been
* set before
* @exception IllegalArgumentException
*/
public void removeLastPath()
{
removePath(path_.length-1);
}
//----------------------------------------------------------------------
/**
* This method removes the path on a specified position of the SearchPath.
*
* @param position
* @exception IllegalStateException thrown if no path has been
* set before
* @exception IllegalArgumentException
*/
public void removePath(int position)
throws IllegalStateException
{
if (path_==null)
throw new IllegalStateException();
if ( path_.length == 0)
System.err.println("There are no Path in the Search Path");
else
{
if ( position < 0 | position > path_.length)
throw(new IllegalArgumentException("The Position is out of the Path boundary"));
String[] new_path = new String [path_.length -1];
for (int count = 0; count< position;count++)
{
new_path[count]=path_[count];
}
for (int count = position; count< new_path.length;count++)
{
new_path[count]=path_[count+1];
}
path_ = new_path;
}
}
//----------------------------------------------------------------------
/**
* This method reverse the strings in the string array.
*
* @exception IllegalStateException thrown if no path has been
* set before
*/
public void reverseSearchPath()
throws IllegalStateException
{
if (path_==null)
{
throw new IllegalStateException();
}
String[] new_path = new String [path_.length];
for(int count = 0;count < path_.length;count++)
{
new_path[count]=path_[path_.length - 1 - count];
}
path_=new_path;
}
//----------------------------------------------------------------------
/**
*/
public String toString()
{
StringBuffer buffer = new StringBuffer("DSearchPath:>> ");
int length = path_.length;
for (int count = 0; count< length;count++)
{
buffer.append("[");
buffer.append(count);
buffer.append("] =\"");
buffer.append(path_[count]);
buffer.append("\",");
}
return new String(buffer);
}
// FIXXME (Stefan Thalauer, Mar 5 2002 19:12) -> this methodes are not well designed now
//----------------------------------------------------------------------
/**
* This method checks if a path is in the current Search path
*
* @param path
* @return returns true if the new path allready exists in the search
* path
* @exception IllegalStateException thrown if no path has been
* set before
*/
// public boolean existPath(String[] path)
// throws IllegalStateException,IllegalArgumentException
// {
// if (path_==null)
// {
// throw new IllegalStateException();
// }
// if (path==null)
// {
// throw new IllegalArgumentException();
// }
// for(int count=0; count < path.length ; count++)
// {
// if (!existPath(path[count]))
// return false;
// }
// return true;
// }
//----------------------------------------------------------------------
/**
* This method appends a string to the current path.
*
* @exception IllegalStateException thrown if no path has been
* set before
*/
// public void appendPath(String[] path)
// throws IllegalArgumentException
// {
// if (path == null)
// throw(new IllegalArgumentException("The Path must not be <null>"));
// if (existPath(path))
// System.err.println("Path: \"" + path +"\" allready registered in String Array ");
// else
// {
// String[] new_path = new String [path_.length + path.length];
// for (int count = 0; count< path_.length;count++)
// {
// new_path[count]=path_[count];
// }
// for (int count = 0; count< path.length;count++)
// {
// new_path[count+path_.length]=path[count];
// }
// path_ = new_path;
// }
// }
// END FIXXME (Stefan Thalauer, Mar 5 2002 19:13)
}
|