User: user57
Date: 02/02/14 22:14:22
Added: src/main/org/jboss/util/coerce BoundCoercionHandler.java
CharacterHandler.java ClassHandler.java
CoercionHandler.java FileHandler.java package.html
Log:
o importing the useful bits from the bliss cl.
o plus some bits from server/*/util/* have been moved to util.jmx
Revision Changes Path
1.1
jboss-common/src/main/org/jboss/util/coerce/BoundCoercionHandler.java
Index: BoundCoercionHandler.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.coerce;
/**
* A bound CoersionHandler, which is bound to a specific class type.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public abstract class BoundCoercionHandler
extends CoercionHandler
{
/**
* Get the target class type for this CoercionHandler.
*
* @return Class type
*/
public abstract Class getType();
}
1.1
jboss-common/src/main/org/jboss/util/coerce/CharacterHandler.java
Index: CharacterHandler.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.coerce;
import org.jboss.util.CoercionException;
import org.jboss.util.NotCoercibleException;
/**
* A Character coercion handler.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class CharacterHandler
extends BoundCoercionHandler
{
/**
* Get the target class type for this CoercionHandler.
*
* @return Class type
*/
public Class getType() {
return Character.class;
}
/**
* Coerces the given value into the given type (which should be
* Character.class).
*
* <p>This currently only support coercion from a String.
*
* @param value Value to coerce
* @param type Character.class
* @return Value coerced into a Character
*
* @throws CoercionException Failed to coerce
*/
public Object coerce(Object value, Class type) throws CoercionException {
if (value.getClass().equals(String.class)) {
return coerce((String)value);
}
throw new NotCoercibleException(value);
}
/**
* Coerces the given string into a Character, by taking off the first
* index of the string and wrapping it.
*
* @param value String value to convert to a Character
* @return Character value or null if the string is empty.
*/
public Object coerce(String value) {
char[] temp = value.toCharArray();
if (temp.length == 0) {
return null;
}
return new Character(temp[0]);
}
}
1.1 jboss-common/src/main/org/jboss/util/coerce/ClassHandler.java
Index: ClassHandler.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.coerce;
import org.jboss.util.CoercionException;
import org.jboss.util.NotCoercibleException;
/**
* A <tt>java.lang.Class</tt> coercion handler.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class ClassHandler
extends BoundCoercionHandler
{
/**
* Get the target class type for this <tt>CoercionHandler</tt>.
*
* @return Class type.
*/
public Class getType() {
return Class.class;
}
/**
* Coerces the given value into the given type (which should be
* <tt>Class</tt>).
*
* <p>This currently only support coercion from a <tt>String</tt>.
*
* @param value Value to coerce.
* @param type <tt>java.lang.Class</tt>.
* @return Value coerced into a <tt>Class</tt>.
*
* @throws CoercionException Failed to coerce.
*/
public Object coerce(Object value, Class type) throws CoercionException {
if (value.getClass().equals(String.class)) {
return coerce((String)value);
}
throw new NotCoercibleException(value);
}
/**
* Coerces the given String into a <tt>Class</tt> by doing a
* <code>Class.forName()</code>.
*
* @param value String value to convert to a <tt>Class</tt>.
* @return <tt>Class</tt> value.
*
* @throws NotCoercibleException Class not found.
*/
public Object coerce(String value) {
try {
return Class.forName(value);
}
catch (ClassNotFoundException e) {
throw new NotCoercibleException(value, e);
}
}
}
1.1 jboss-common/src/main/org/jboss/util/coerce/CoercionHandler.java
Index: CoercionHandler.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.coerce;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
import org.jboss.util.CoercionException;
import org.jboss.util.NotCoercibleException;
import org.jboss.util.NullArgumentException;
import org.jboss.util.NotImplementedException;
/**
* An abstract class to allow extending the default behavior of
* {@link Objects#coerce(Object, Class)} when it is not possible to implement
* {@link Coercible} directly in the target class or where coercion is desired
* from an unrelated class. Also provides a registry for all of the currently
* installed handlers.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public abstract class CoercionHandler
{
/**
* Coerce the given value into the specified type.
*
* @param value Value to coerce
* @param type Object type to coerce into
* @return Coerced value
*
* @exception CoercionException Failed to coerce
*/
public abstract Object coerce(Object value, Class type)
throws CoercionException;
/**
* Get the target class type for this CoercionHandler.
*
* @return Class type
*
* @throws NotImplementedException Handler is not bound
*/
public Class getType() {
throw new NotImplementedException("handler is not bound");
}
/////////////////////////////////////////////////////////////////////////
// Factory Methods //
/////////////////////////////////////////////////////////////////////////
/** Class -> CoercionHandler map */
private static Map handlers = Collections.synchronizedMap(new HashMap());
/** Initializes the CoercionHandler map */
static {
// initialzie the helper map with some defaults
install(new CharacterHandler());
install(new ClassHandler());
install(new FileHandler());
}
/**
* Install a CoercionHandler for a given class type.
*
* @param handler Coercion handler
*
* @throws NullArgumentException type or handler
*/
public static void install(Class type, CoercionHandler handler) {
if (type == null)
throw new NullArgumentException("type");
if (handler == null)
throw new NullArgumentException("handler");
handlers.put(type, handler);
}
/**
* Install a BoundCoercionHandler.
*
* @param handler Bound coercion handler
*
* @throws NullArgumentException handler
*/
public static void install(BoundCoercionHandler handler) {
if (handler == null)
throw new NullArgumentException("handler");
handlers.put(handler.getType(), handler);
}
/**
* Uninstall a CoercionHandler for a given class type.
*
* @param type Class type
*
* @throws NullArgumentException type
*/
public static void uninstall(Class type) {
if (type == null)
throw new NullArgumentException("type");
handlers.remove(type);
}
/**
* Check if there is a CoercionHandler installed for the given class.
*
* @param type Class type
* @return True if there is a CoercionHandler
*/
public static boolean isInstalled(Class type) {
return handlers.containsKey(type);
}
/**
* Lookup the CoercionHandler for a given class.
*
* @param type Class type
* @return A CoercionHandler or null if there is no installed handler
*
* @throws NullArgumentException type
*/
public static CoercionHandler lookup(Class type) {
if (type == null)
throw new NullArgumentException("type");
return (CoercionHandler)handlers.get(type);
}
/**
* Create a CoercionHandler for the given class type.
*
* @param type Class type
* @return A CoercionHandler instance for the given class type.
*
* @throws CoercionException No installed handler for type
*/
public static CoercionHandler create(Class type) {
CoercionHandler handler = lookup(type);
if (handler == null)
throw new CoercionException
("no installed handler for type: " + type);
return handler;
}
}
1.1 jboss-common/src/main/org/jboss/util/coerce/FileHandler.java
Index: FileHandler.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.coerce;
import org.jboss.util.CoercionException;
import org.jboss.util.NotCoercibleException;
import java.io.File;
/**
* A <tt>java.io.File</tt> coercion handler.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class FileHandler
extends BoundCoercionHandler
{
/**
* Get the target class type for this <tt>CoercionHandler</tt>.
*
* @return <tt>Class</tt> type.
*/
public Class getType() {
return File.class;
}
/**
* Coerces the given value into the given type (which should be
* <tt>File</tt>).
*
* <p>This currently only support coercion from a <tt>String</tt>
*
* @param value Value to coerce.
* @param type <tt>File</tt>.
* @return Value coerced into a <tt>File</tt>.
*
* @throws CoercionException Failed to coerce.
*/
public Object coerce(Object value, Class type) throws CoercionException {
if (value.getClass().equals(String.class)) {
return coerce((String)value);
}
throw new NotCoercibleException(value);
}
/**
* Coerces the given String into a <tt>File</tt> by creating attempting
* to create a new file for the given filename.
*
* @param value The name of the file to create.
* @return <tt>File</tt>
*
* @throws NotCoercibleException Failed to create file.
*/
public Object coerce(String value) {
return new File(value);
}
}
1.1 jboss-common/src/main/org/jboss/util/coerce/package.html
Index: package.html
===================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- $Id: package.html,v 1.1 2002/02/15 06:14:22 user57 Exp $ -->
<!--
JBoss: The OpenSource J2EE WebOS
Distributable under LGPL license.
See terms of license at gnu.org.
-->
</head>
<body bgcolor="white">
<p>Object coercion helper classes.</p>
<p>Classes contained here are used internaly by the
<code>planet57.util.Objects</code> class, and are probably
not very very useful outside of that scope.<p>
<h2>Package Specification</h2>
<ul>
<li><a href="javascript: alert('not available')">Not Available</a>
</ul>
<h2>Related Documentation</h2>
<ul>
<li><a href="javascript: alert('not available')">Not Available</a>
</ul>
<h2>Package Status</h2>
<ul>
<li><font color="green"><b>STABLE</b></font>
</ul>
<h2>Todo</h2>
<ul>
<li>Change the handler loading mechanism to be more like a protocol
handler.
<li>Add more handlers
<ul>
<li>File handler
</ul>
</ul>
<!-- Put @see and @since tags down here. -->
</body>
</html>
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development