⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 type.java

📁 JAVA的加密库之一
💻 JAVA
字号:
/* $Id: Type.java,v 1.3 2001/05/24 16:52:49 raif Exp $
 *
 * Copyright (C) 1997-2001 The Cryptix Foundation Limited. All rights reserved.
 *
 * Use, modification, copying and distribution of this software is subject to
 * the terms and conditions of the Cryptix General Licence. You should have
 * received a copy of the Cryptix General Licence along with this library; if
 * not, you can download a copy from http://www.cryptix.org/
 */
package cryptix.asn1.lang;

import cryptix.asn1.io.ASNReader;
import cryptix.asn1.io.ASNWriter;

import org.apache.log4j.Category;

import java.io.EOFException;
import java.io.IOException;

/**
 * The basic implementation of any ASN.1 type; the superclass of all concrete
 * implementation of ASN.1 types in this package.<p>
 *
 * @version $Revision: 1.3 $
 * @author  Raif S. Naffah
 */
public abstract class Type implements IType {

	// Constants and vars
	// .......................................................................

	static Category cat = Category.getInstance(Type.class.getName());

	/**
	 * The Name of this instance.
	 */
	protected String name;

	/**
	 * If/when known, the tag object of this instance.
	 */
	protected Tag tag;

	/**
	 * The current value of this object.
	 */
	protected Object value;

	/**
	 * The default value, if any, of this object.
	 */
	protected Object defaultValue;

	/**
	 * Flag indicating if this type (when used in a compound type) is optional or
	 * not.
	 */
	protected boolean optional; // default is false

	// Constructor(s)
	// .......................................................................

	/**
	 * A constructor for a subclass of ANY with a known Tag.
	 *
	 * @param name the name of this instance.
	 * @param tag the tag object for the concrete instance.
	 */
	protected Type(String name, Tag tag) {
		super();

		this.name = new String(name);
		if (tag != null)
			this.tag = (Tag) tag.clone();
	}

	// IType interface methods
	// .......................................................................

	public String name() {
		return this.name;
	}

	public void name(String name) {
		this.name = new String(name);
	}

	public Tag tag() {
		return this.tag;
	}

	/**
	 * Decodes an instance from an input stream. This method always throws a
	 * <tt>java.lang.RuntimeException</tt>. Subclasses should override this
	 * method.
	 *
	 * @param is the ASN.1 stream to read from.
	 * @exception RuntimeException always.
	 */
	public void decode(ASNReader is) throws IOException {
		String cn = this.getClass().getName();
		RuntimeException x = new RuntimeException("Subclass should override decode() method");
		cat.error(cn+".decode()", x);
		throw x;
	}

	/**
	 * Encodes an instance of this object to an output stream. This method
	 * always throws a <tt>java.lang.RuntimeException</tt>. Subclasses should
	 * override this method.
	 *
	 * @param is the ASN.1 stream to write to.
	 * @exception RuntimeException always.
	 */
	public void encode(ASNWriter os) throws IOException {
		String cn = this.getClass().getName();
		RuntimeException x = new RuntimeException("Subclass should override encode() method");
		cat.error(cn+".encode()", x);
		throw x;
	}

	public void reset() {
		this.value = this.defaultValue;
	}

	public Object value() {
		return this.value;
	}

	public void value(Object value) {
		this.value = value;
	}

	public Object defaultValue() {
		return this.defaultValue;
	}

	public void defaultValue(Object defaultValue) {
		this.defaultValue = defaultValue;
	}

	public boolean isOptional() {
		return this.optional;
	}

	public void optional(boolean optional) {
		this.optional = optional;
	}

	public boolean isBlank() {
		return (value == null);
	}

	// Overloaded java.lang.Object methods
	// .......................................................................

   /**
    * Indicates whether some other object is "equal to" this one.<p>
    *
    * The equals method implements an equivalence relation:
    * <ul>
    *    <li>It is <i>reflexive</i>: for any reference value <tt>x</tt>,
    *    <tt>x.equals(x)</tt> should return <tt>true</tt>.</li>
    *    <li>It is <i>symmetric</i>: for any reference values <tt>x</tt> and
    *    <tt>y</tt>, <tt>x.equals(y)</tt> should return <tt>true</tt> if and
    *    only if <tt>y.equals(x)</tt> returns <tt>true</tt>.</li>
    *    <li>It is <i>transitive</i>: for any reference values <tt>x</tt>,
    *    <tt>y</tt>, and <tt>z</tt>, if <tt>x.equals(y)</tt> returns <tt>true</tt>
    *    and <tt>y.equals(z)</tt> returns <tt>true</tt>, then <tt>x.equals(z)</tt>
    *    should return <tt>true</tt>.</li>
    *    <li>It is <i>consistent</i>: for any reference values <tt>x</tt> and
    *    <tt>y</tt>, multiple invocations of <tt>x.equals(y)</tt> consistently
    *    return <tt>true</tt> or consistently return <tt>false</tt>, provided
    *    no information used in equals comparisons on the object is modified.</li>
    *    <li>For any non-null reference value <tt>x</tt>, <tt>x.equals(null)</tt>
    *    should return <tt>false</tt>.</li>
    * </ul>
    *
    * While the <tt>equals()</tt> method for class <tt>Object</tt> implements
    * the most discriminating possible equivalence relation on objects; that
    * is, for any reference values <tt>x</tt> and <tt>y</tt>, this method
    * returns <tt>true</tt> if and only if <tt>x</tt> and <tt>y</tt> refer to
    * the same object (<tt>x == y</tt> has the value <tt>true</tt>), this
    * method, on the other hand, returns <tt>true</tt> iff the designated
    * object (a) is a subclass of this class, and (b) has an equal <tt>Tag</tt>
    * and content values.

    * @param obj the reference object with which to compare.
    * @return <tt>true</tt> if this object is the same as the obj argument;
    * <tt>false</tt> otherwise.
    */
   public boolean equals(Object obj) {
      if (obj == null)
         return false;

      if (!(obj instanceof Type))
         return false;

      Type that = (Type) obj;
      if (that.tag == null || !that.tag.equals(this.tag))
         return false;

      return this.sameValue(that.value);
   }

	// other instance methods
	// .......................................................................

   /**
    * Returns <tt>true</tt> if the designated value is equal to the value of
    * this instance.<p>
    *
    * This default implementation invokes the {@link java.lang.Object.equals(java.lang.Object)}
    * method of class {@link java.lang.Object}. Concrete subclasses should
    * override this method to carry on a more meaningful test than an identity
    * test.
    *
    * @param obj a value to compare with the value of this instance.
    * @return <tt>true</tt> if this instance has an equal, non-null value to
    * the designated one; <tt>false</tt> otherwise.
    */
   protected boolean sameValue(Object obj) {
      if (this.value == null || obj == null)
         return false;

      return this.value.equals(obj);
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -