/* Copyright 2005-2006 David N. Welton

 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.
 */

/* $Id$ */

package org.hecl;

/**
 * The ObjectThing class provides a wrapper for objects
 * that are not directly representable as strings.  FIXME - I am
 * uncertain if this will actually work or not.
 *
 * @author David N. Welton 
 * @version 1.0
 */
public class ObjectThing implements RealThing {
    private Object val;

    /**
     * Creates a new ObjectThing instance equal to 0.
     *
     */
    public ObjectThing() {
//        val = new Object();
    }

    /**
     * Creates a new ObjectThing instance with value i.
     *
     * @param o
     *            an Object.
     */
    public ObjectThing(Object o) {
        val = o;
    }

    @Override
    public String thingclass() {
	return "object";
    }

    /**
     * Retrieve the object associated with this instance.
     */
    public Object get() {return this.val;}

    /**
     * The create method creates and returns a newly allocated
     * Thing with an ObjectThing internal representation.
     *
     * @param o
     *            an Object
     * @return a Thing value
     */
    public static Thing create(Object o) {
        return new Thing(new ObjectThing(o));
    }


    
    /**
     * get attempts to fetch an Object from the Thing.
     *
     * @param thing a Thing value
     * @return an Object value
     * @exception HeclException if an error occurs
     */
    public static Object get(Thing thing) throws HeclException {
        RealThing realthing = thing.getVal();

	if (!(realthing instanceof ObjectThing)) {
	    throw new HeclException("cannot transform " + thing.toString() +
				    " into an ObjectThing");
	}
	return ((ObjectThing)realthing).val;
    }

    /**
     * deepcopy makes a copy.
     *
     * @return a RealThing value
     */
    @Override
    public RealThing deepcopy() {
        return new ObjectThing(val);
    }

    /**
     * getStringRep creates a string representation of the
     * ObjectThing.
     *
     * @return a String value
     */
    @Override
    public String getStringRep() {
	/* FIXME! */
	return val != null ? val.toString() : "[null]";
        //return val.toString();
    }
}

 

Ads help maintain this website.