epgtools.dumpepgfromts.dataextractor.AbstractDataExtractor.java Source code

Java tutorial

Introduction

Here is the source code for epgtools.dumpepgfromts.dataextractor.AbstractDataExtractor.java

Source

/*
 * Copyright (C) 2016 normal
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package epgtools.dumpepgfromts.dataextractor;

import java.lang.invoke.MethodHandles;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import libepg.epg.section.Section;
import libepg.epg.section.SectionBody;
import libepg.epg.section.TABLE_ID;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 1?ID?1??????????????T??????
 *
 * @author normal
 * @param <T> s????
 */
public abstract class AbstractDataExtractor<T extends DataObject> {

    protected static final Log LOG;

    static {
        final Class<?> myClass = MethodHandles.lookup().lookupClass();
        LOG = LogFactory.getLog(myClass);
    }

    private final Set<TABLE_ID> tableIds;

    private final Set<T> dataSet = Collections.synchronizedSet(new HashSet<>());

    /**
     * <br>
     * ??ID???<br>
     *
     * @param tableId ????ID
     * @param tablsIds ????ID
     * @throws IllegalArgumentException<br>
     * 1:???
     */
    protected AbstractDataExtractor(TABLE_ID tableId, TABLE_ID... tablsIds) throws IllegalArgumentException {

        Set<TABLE_ID> tidset = Collections.synchronizedSet(new HashSet<>());
        if (tableId == null) {
            throw new IllegalArgumentException("ID?????");
        }
        tidset.add(tableId);

        int l = tablsIds.length;
        for (int x = 0; x < l; x++) {
            TABLE_ID y = tablsIds[x];
            if (y != null) {
                tidset.add(y);
            } else {
                LOG.error(
                        "ID??null???????null?????");
            }
        }
        this.tableIds = Collections.unmodifiableSet(tidset);

    }

    /**
     * @return ???ID?
     */
    public Set<TABLE_ID> getTableIds() {
        return tableIds;
    }

    protected final Set<T> getDataSet() {
        return dataSet;
    }

    public final Set<T> getUnmodifiableDataSet() {
        return Collections.unmodifiableSet(this.getDataSet());
    }

    public final void clearDataSet() {
        this.dataSet.clear();
    }

    /**
     * ????
     *
     * @param s 
     * @throws IllegalArgumentException<br>
     * 1:???????????????<br>
     * 2:?CRC??????<br>
     */
    protected final void checkSection(Section s) throws IllegalArgumentException {
        final String hexDump = Hex.encodeHexString(s.getData());
        if (s.checkCRC() != Section.CRC_STATUS.NO_CRC_ERROR) {
            throw new IllegalArgumentException("CRC??  = " + hexDump);
        }
        if (!this.tableIds.contains(s.getTable_id_const())) {
            StringBuilder sb = new StringBuilder();
            sb.append("??ID?????\n");
            sb.append("??ID\n");
            for (TABLE_ID id : this.tableIds) {
                sb.append(id).append("\n");
            }
            sb.append("?ID = ").append(s.getTable_id_const()).append("\n");
            sb.append(" = ").append(hexDump).append("\n");
            throw new IllegalArgumentException(sb.toString());
        }
    }

    /**
     * ??
     *
     * @throws IllegalArgumentException ID?????????
     */
    protected final void checkSectionBodyType(Section s) throws IllegalArgumentException {
        SectionBody b = s.getSectionBody();
        if (b.getClass() != s.getTable_id_const().getDataType()) {
            //?????????????
            throw new IllegalStateException(
                    "ID??????????ID = "
                            + s.getClass() + "  = " + b.getClass()
                            + " ? = " + Hex.encodeHexString(s.getData()));
        }
    }

    /**
     * ???????????????
     *
     * @param s
     */
    public abstract void makeDataSet(Section s) throws IllegalStateException;
}