KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > Timestamp


1 /*
2  * @(#)Timestamp.java 1.2 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7  
8 package java.security;
9
10 import java.io.Serializable JavaDoc;
11 import java.security.cert.CertPath JavaDoc;
12 import java.security.cert.X509Extension JavaDoc;
13 import java.util.Date JavaDoc;
14
15 /**
16  * This class encapsulates information about a signed timestamp.
17  * It is immutable.
18  * It includes the timestamp's date and time as well as information about the
19  * Timestamping Authority (TSA) which generated and signed the timestamp.
20  *
21  * @since 1.5
22  * @version 1.2, 12/19/03
23  * @author Vincent Ryan
24  */

25
26 public final class Timestamp implements Serializable JavaDoc {
27
28     private static final long serialVersionUID = -5502683707821851294L;
29
30     /**
31      * The timestamp's date and time
32      *
33      * @serial
34      */

35     private Date JavaDoc timestamp;
36
37     /**
38      * The TSA's certificate path.
39      *
40      * @serial
41      */

42     private CertPath JavaDoc signerCertPath;
43
44     /*
45      * Hash code for this timestamp.
46      */

47     private transient int myhash = -1;
48
49     /**
50      * Constructs a Timestamp.
51      *
52      * @param timestamp is the timestamp's date and time. It must not be null.
53      * @param signerCertPath is the TSA's certificate path. It must not be null.
54      * @throws NullPointerException if timestamp or signerCertPath is null.
55      */

56     public Timestamp(Date JavaDoc timestamp, CertPath JavaDoc signerCertPath) {
57     if (timestamp == null || signerCertPath == null) {
58         throw new NullPointerException JavaDoc();
59     }
60     this.timestamp = new Date JavaDoc(timestamp.getTime()); // clone
61
this.signerCertPath = signerCertPath;
62     }
63
64     /**
65      * Returns the date and time when the timestamp was generated.
66      *
67      * @return The timestamp's date and time.
68      */

69     public Date JavaDoc getTimestamp() {
70     return new Date JavaDoc(timestamp.getTime()); // clone
71
}
72
73     /**
74      * Returns the certificate path for the Timestamping Authority.
75      *
76      * @return The TSA's certificate path.
77      */

78     public CertPath JavaDoc getSignerCertPath() {
79     return signerCertPath;
80     }
81
82     /**
83      * Returns the hash code value for this timestamp.
84      * The hash code is generated using the date and time of the timestamp
85      * and the TSA's certificate path.
86      *
87      * @return a hash code value for this timestamp.
88      */

89     public int hashCode() {
90     if (myhash == -1) {
91         myhash = timestamp.hashCode() + signerCertPath.hashCode();
92     }
93     return myhash;
94     }
95
96     /**
97      * Tests for equality between the specified object and this
98      * timestamp. Two timestamps are considered equal if the date and time of
99      * their timestamp's and their signer's certificate paths are equal.
100      *
101      * @param obj the object to test for equality with this timestamp.
102      *
103      * @return true if the timestamp are considered equal, false otherwise.
104      */

105     public boolean equals(Object JavaDoc obj) {
106     if (obj == null || (!(obj instanceof Timestamp JavaDoc))) {
107         return false;
108     }
109     Timestamp JavaDoc that = (Timestamp JavaDoc)obj;
110
111     if (this == that) {
112         return true;
113     }
114     return (timestamp.equals(that.getTimestamp()) &&
115         signerCertPath.equals(that.getSignerCertPath()));
116     }
117
118     /**
119      * Returns a string describing this timestamp.
120      *
121      * @return A string comprising the date and time of the timestamp and
122      * its signer's certificate.
123      */

124     public String JavaDoc toString() {
125     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
126     sb.append("(");
127     sb.append("timestamp: " + timestamp);
128     sb.append("TSA: " + signerCertPath.getCertificates().get(0));
129     sb.append(")");
130     return sb.toString();
131     }
132 }
133
Popular Tags