Source code: jsdsi/Sequence.java
1 /*
2 * Copyright 2002 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this program for any
5 * purpose and without fee is hereby granted, provided that this
6 * copyright and permission notice appear on all copies and supporting
7 * documentation, the name of M.I.T. not be used in advertising or
8 * publicity pertaining to distribution of the program without specific
9 * prior permission, and notice be given in supporting documentation that
10 * copying and distribution is by permission of M.I.T. M.I.T. makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 */
14 package jsdsi;
15
16 import jsdsi.sexp.*;
17 import java.util.*;
18
19 /**
20 * A sequence of SPKI/SDSI objects, typically used to present certs and
21 * validators that prove a particular statement.
22 *
23 * @see Proof
24 *
25 * @author Sameer Ajmani
26 * @version $Revision: 1.2 $ $Date: 2003/05/07 19:36:41 $
27 */
28 public class Sequence extends Obj {
29 /**
30 * Elements of this <code>Sequence</code>.
31 */
32 private final Element[] elements;
33
34 /**
35 * Creates a new <code>Sequence</code> from a given array of elements.
36 *
37 * @param e array of elements to create the <code>Sequence</code> from.
38 */
39 public Sequence(Element[] e) {
40 assert(e != null) : "null elements";
41 elements = e;
42 }
43
44 /**
45 * Returns the elements of this <code>Sequence</code>.
46 *
47 * @return the elements of this <code>Sequence</code>.
48 */
49 public Element[] getElements() {
50 return elements;
51 }
52
53 /**
54 * Concatenates this <code>Sequence</code> with a given one.
55 *
56 * @param s sequence to concat this sequence with.
57 * @return the sequence containing the elements of this sequence
58 * and the elements of <code>s</code>.
59 */
60 public Sequence concat(Sequence s) {
61 Element[] els = new Element[elements.length + s.elements.length];
62 System.arraycopy(elements, 0, els, 0, elements.length);
63 System.arraycopy(
64 s.elements,
65 0,
66 els,
67 elements.length,
68 s.elements.length);
69 return new Sequence(els);
70 }
71
72 /**
73 * @see java.lang.Object#equals(Object)
74 */
75 public boolean equals(Object o) {
76 return (o instanceof Sequence)
77 && Util.equals(elements, ((Sequence) o).elements);
78 }
79
80 /**
81 * @see java.lang.Object#hashCode()
82 */
83 public int hashCode() {
84 return Util.hashCode(elements);
85 }
86
87 public SexpList toSexp() {
88 Sexp[] ss = new Sexp[elements.length];
89 for (int i = 0; i < elements.length; i++) {
90 ss[i] = elements[i].toSexp();
91 }
92 return SexpUtil.toSexp("sequence", ss);
93 }
94
95 static Sequence parseSequence(SexpList l) throws SexpParseException {
96 Iterator sbody = SexpUtil.getBody(l);
97 Element[] elems = new Element[l.size() - 1];
98 for (int i = 0; i < elems.length; i++) {
99 elems[i] =
100 Element.Default.parseElement(
101 SexpUtil.getNextList(sbody, "sequence element"));
102 }
103 SexpUtil.checkDone(sbody, "sequence"); // sanity check
104 return new Sequence(elems);
105 }
106 }