001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024import java.io.PrintStream;
025import java.util.Collections;
026import java.util.Map;
027
028/**
029 * implements a <b>Component</b>
030 * within a <b>Composite pattern</b>
031 *
032 * See 
033 * <a href="/doc/javadoc/overview-summary.html">Overview</a> for details.
034 *
035 * @author apr@cs.com.uy
036 * @version $Id$
037 * @see ISOMsg
038 * @see ISOField
039 * @see ISOException
040 */
041public abstract class ISOComponent implements Cloneable {
042    /**
043     * Set a field within this message
044     * @param c - a component
045     * @exception ISOException
046     */
047    public void set (ISOComponent c) throws ISOException {
048        throw new ISOException ("Can't add to Leaf");
049    }
050    /**
051     * Unset a field
052     * @param fldno - the field number
053     * @exception ISOException
054     */
055    public void unset (int fldno) throws ISOException {
056        throw new ISOException ("Can't remove from Leaf");
057    }
058    /**
059     * In order to interchange <b>Composites</b> and <b>Leafs</b> we use
060     * getComposite(). A <b>Composite component</b> returns itself and
061     * a Leaf returns null. The base class ISOComponent provides
062     * <b>Leaf</b> functionality.
063     *
064     * @return ISOComponent
065     */
066    public ISOComponent getComposite() {
067        return null;
068    }
069    /**
070     * valid on Leafs only.
071     * The value returned is used by ISOMsg as a key
072     * to this field.
073     *
074     * @return object representing the field number
075     * @exception ISOException
076     */
077    public Object getKey() throws ISOException {
078        throw new ISOException ("N/A in Composite");
079    }
080    /**
081     * valid on Leafs only.
082     * @return object representing the field value
083     * @exception ISOException
084     */
085    public Object getValue() throws ISOException {
086        throw new ISOException ("N/A in Composite");
087    }
088    /**
089     * get Value as bytes (when possible)
090     * @return byte[] representing this field
091     * @exception ISOException
092     */
093    public byte[] getBytes() throws ISOException {
094        throw new ISOException ("N/A in Composite");
095    }
096    /**
097     * a Composite must override this function
098     * @return the max field number associated with this message
099     */
100    public int getMaxField() {
101        return 0;
102    }
103    /**
104     * dummy behaviour - return empty map
105     * @return children (in this case 0 children)
106     */
107    public Map getChildren() {
108        return Collections.emptyMap();
109    }
110    /**
111     * changes this Component field number<br>
112     * Use with care, this method does not change
113     * any reference held by a Composite.
114     * @param fieldNumber new field number
115     */
116    public abstract void setFieldNumber (int fieldNumber);
117    public abstract int getFieldNumber ();
118    public abstract void setValue(Object obj) throws ISOException;
119    public abstract byte[] pack() throws ISOException;
120    public abstract int unpack(byte[] b) throws ISOException;
121    public abstract void dump (PrintStream p, String indent);
122    public void pack (OutputStream out) throws IOException, ISOException {
123        out.write (pack ());
124    }
125    public abstract void unpack (InputStream in) throws IOException, ISOException;
126}