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

package org.hecl;

import java.util.Enumeration;
import java.util.Vector;

/**
 * The GroupThing class is for Hecl "groups". For instance, "foo
 * $foo [foo]" must have its components kept together, and is not a proper list.
 *
 * @author David N. Welton 
 * @version 1.0
 */

public class GroupThing implements RealThing {

    protected Vector val = null;

    /**
     * Creates a new GroupThing instance from a vector.
     *
     * @param v a Vector value
     */
    public GroupThing(Vector v) {
        val = v;
    }

    /**
     * Creates a new GroupThing instance from a string.
     *
     * @param s a String value
     */
    public GroupThing(String s) {
        val.addElement(new Thing(new StringThing(s)));
    }

    /**
     * The create method takes a Vector of Things and
     * creates a Thing containing a GroupThing.
     *
     * @param v a Vector value
     * @return a Thing value
     */
    public static Thing create(Vector v) {
        return new Thing(new GroupThing(v));
    }

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


    /**
     * setGroupFromAny creates a group from another type of
     * Thing.
     *
     * @param thing a Thing value
     */
    private static void setGroupFromAny(Thing thing) throws HeclException {
        RealThing realthing = thing.getVal();

        if (!(realthing instanceof GroupThing)) {
            Vector group = new Vector();
            if (realthing instanceof CodeThing) {
                group.addElement(thing);
            } else {
                group.addElement(new Thing(thing.toString()));
            }
            thing.setVal(new GroupThing(group));
        }
    }

    /**
     * get returns a Vector containing other Things, representing
     * a group, from a Thing.
     *
     * @param thing a Thing value
     * @return a Vector value
     * @throws org.hecl.HeclException
     */
    public static Vector get(Thing thing) throws HeclException {
        setGroupFromAny(thing);
        return ((GroupThing)thing.getVal()).val;
    }

    /**
     * deepcopy makes a copy of a GroupThing and all its
     * elements.
     *
     * @return a RealThing value
     * @throws HeclException
     */
    @Override
    public RealThing deepcopy() throws HeclException {
        Vector newv = new Vector();
        for (Enumeration e = val.elements(); e.hasMoreElements();) {
            newv.addElement(((Thing) e.nextElement()).deepcopy());
        }

        return new GroupThing(newv);
    }

    /**
     * getStringRep returns a string representation of the group.
     *
     * @return a String value
     */
    @Override
    public String getStringRep() {
        StringBuffer resbuf = new StringBuffer("");
        int sz = val.size();
        Thing element = null;

        if (sz > 0) {
            for (int i = 0; i < sz; i++) {
                element = (Thing) val.elementAt(i);
                resbuf.append(element.toString());
            }
        }
        return resbuf.toString();
    }
}

 

Ads help maintain this website.