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.util.Arrays;
022
023/**
024 * Holds one decoded element inside a dataset.
025 */
026public class DatasetElement {
027    private final int id;
028    private final ISOComponent component;
029    private final boolean constructed;
030
031    /**
032     * Creates a primitive dataset element.
033     *
034     * @param id element identifier, either a TLV tag or DBM bit number
035     * @param component backing ISO component
036     */
037    public DatasetElement(int id, ISOComponent component) {
038        this(id, component, false);
039    }
040
041    /**
042     * Creates a dataset element.
043     *
044     * @param id element identifier, either a TLV tag or DBM bit number
045     * @param component backing ISO component
046     * @param constructed whether the source TLV tag was constructed
047     */
048    public DatasetElement(int id, ISOComponent component, boolean constructed) {
049        if (component == null) {
050            throw new IllegalArgumentException("component cannot be null");
051        }
052        this.id = id;
053        this.component = component;
054        this.constructed = constructed;
055    }
056
057    /**
058     * Returns the element identifier.
059     *
060     * @return element identifier
061     */
062    public int getId() {
063        return id;
064    }
065
066    /**
067     * Returns the backing ISO component.
068     *
069     * @return ISO component
070     */
071    public ISOComponent getComponent() {
072        return component;
073    }
074
075    /**
076     * Indicates whether the element originated from a constructed TLV tag.
077     *
078     * @return {@code true} for constructed TLV elements
079     */
080    public boolean isConstructed() {
081        return constructed;
082    }
083
084    /**
085     * Returns the component value.
086     *
087     * @return element value
088     * @throws ISOException on component access errors
089     */
090    public Object getValue() throws ISOException {
091        return component.getValue();
092    }
093
094    /**
095     * Returns a defensive copy of the element bytes.
096     *
097     * @return encoded element value bytes, or {@code null}
098     * @throws ISOException on component access errors
099     */
100    public byte[] getBytes() throws ISOException {
101        byte[] bytes = component.getBytes();
102        return bytes != null ? Arrays.copyOf(bytes, bytes.length) : null;
103    }
104}