/*
 * @(#)DnsRecord.java
 *
 * Copyright (c) 2001-2002 JScape
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JScape. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JScape.
 */

package com.jscape.inet.dns.record;


import com.jscape.inet.dns.DnsInputStream;

import java.io.IOException;
import java.io.Serializable;


/**
 * Represents a resource record retrieved from a Dns server.
 */
public abstract class DnsRecord
        implements Serializable {
    private String recordName;
    private int recordType;
    private int recordClass;
    private long recordTTL;
    private long recordCreated;

    /**
     * Initializes this DnsRecord.
     *
     * @param recordName  the record name
     * @param recordType  the record TYPE
     * @param recordClass the record CLASS
     * @param recordTTL   the time-to-live for this record
     * @param inputStream the input stream containing data for this record
     *
     * @throws IOException if an I/O error occurs
     */
    public void init(String recordName, int recordType, int recordClass, long recordTTL, DnsInputStream inputStream)
            throws IOException {
        this.recordName = recordName;
        this.recordType = recordType;
        this.recordClass = recordClass;
        this.recordTTL = recordTTL;
        decode(inputStream);
    }

    protected abstract void decode(DnsInputStream inputStream)
            throws IOException;

    /**
     * Gets name for this DnsRecord.
     *
     * @return name
     */
    public String getRecordName() {
        return recordName;
    }

    /**
     * Gets TYPE for this DnsRecord.
     *
     * @return TYPE
     *
     * @see com.jscape.inet.dns.Dns
     */
    public int getRecordType() {
        return recordType;
    }

    /**
     * Gets CLASS for this DnsRecord.
     *
     * @return CLASS
     *
     * @see com.jscape.inet.dns.Dns
     */
    public int getRecordClass() {
        return recordClass;
    }

    /**
     * Gets time-to-live for this DnsRecord.
     *
     * @return time-to-live
     */
    public long getRecordTTL() {
        return recordTTL;
    }

    /**
     * Checks to see if this DnsRecord is valid.
     *
     * @return true if time-to-live has not expired
     */
    public boolean isValid() {
        return recordTTL * 1000 > System.currentTimeMillis() - recordCreated;
    }
}

