View Javadoc
1 2 package jsdsi.sexp; 3 4 import java.io.IOException; 5 import java.io.ObjectInputStream; 6 import java.io.ObjectOutputStream; 7 import java.io.OutputStream; 8 import java.io.Writer; 9 import java.util.ArrayList; 10 import java.util.Iterator; 11 import java.util.List; 12 13 /*** 14 * A parenthesized list of S-expressions, where the first element is 15 * always a SexpString and is called the list's "type". 16 * 17 * @see Sexp 18 * @see SexpString 19 * 20 * @author Sameer Ajmani 21 * @version $Revision: 1.2.2.1 $ $Date: 2005/11/08 03:12:52 $ 22 */ 23 public class SexpList extends Sexp { 24 25 private static final long serialVersionUID = 232350493156018658L; 26 27 private List elements; 28 29 // implements serializable 30 SexpList() { 31 } 32 33 // implements serializable 34 private void writeObject(ObjectOutputStream out) throws IOException { 35 writeCanonical(out); 36 } 37 38 // implements serializable 39 private void readObject(ObjectInputStream in) 40 throws IOException, ClassNotFoundException { 41 try { 42 SexpList l = (SexpList) (new SexpInputStream(in)).readSexp(); 43 this.elements = l.elements; 44 } catch (SexpException e) { 45 throw (IOException) new IOException().initCause(e); 46 } catch (ClassCastException e) { 47 throw (IOException) new IOException().initCause(e); 48 } 49 } 50 51 /*** 52 * Creates a new list from a type string and an array of 53 * S-expressions. 54 * 55 * @param type the list type string. 56 * @param expressions the elements of the list. 57 */ 58 public SexpList(SexpString type, Sexp[] expressions) { 59 elements = new ArrayList(expressions.length + 1); 60 elements.add(type); 61 for (int i = 0; i < expressions.length; i++) { 62 elements.add(expressions[i]); 63 } 64 } 65 66 /*** 67 * Returns the type of this list. 68 * 69 * @return the type of this list. 70 */ 71 public String getType() { 72 return ((SexpString) elements.get(0)).toString(); 73 } 74 75 /*** 76 * Returns the size of this list, including its type. 77 * 78 * @return the size of this list, including its type. 79 */ 80 public int size() { 81 return elements.size(); 82 } 83 84 /*** 85 * Returns an iterator over this list, including its type. 86 * Each element is an Sexp. 87 * 88 * @return an iterator over this list, including its type. 89 */ 90 public Iterator iterator() { 91 return elements.iterator(); 92 } 93 94 /*** 95 * Returns <code>true</code> if <code>this</code> list is empty 96 * (i.e., only has a type), <code>false</code> otherwise. 97 * 98 * @return <code>true</code> if <code>this</code> list is empty 99 * (i.e., only has a type), <code>false</code> otherwise. 100 */ 101 public boolean isEmpty() { 102 return elements.size() == 1; 103 } 104 105 public void writeCanonical(OutputStream out) throws IOException { 106 out.write('('); 107 Iterator i = iterator(); 108 while (i.hasNext()) { 109 ((Sexp) i.next()).writeCanonical(out); 110 } 111 out.write(')'); 112 } 113 114 public void writeReadable(Writer out, int offset, int width, int last) 115 throws IOException { 116 out.write('('); 117 boolean newLines = false; 118 if (getReadableLen() > width - offset - last) { 119 newLines = true; 120 offset += 2; 121 if (offset >= width) 122 offset = 0; 123 } 124 Iterator i = iterator(); 125 boolean doneFirst = false; 126 while (i.hasNext()) { 127 Sexp s = (Sexp) i.next(); 128 if (doneFirst) { 129 if (newLines) { 130 out.write('\n'); 131 for (int j = 0; j < offset; j++) { 132 out.write(' '); 133 } 134 } else { 135 out.write(' '); 136 } 137 } else { 138 doneFirst = true; 139 } 140 if (i.hasNext()) { 141 s.writeReadable(out, offset, width, 0); 142 } else { 143 s.writeReadable(out, offset, width, last + 1); 144 } 145 } 146 out.write(')'); 147 } 148 149 int getReadableLenImpl() { 150 int len = 1; // the opening paren 151 for (Iterator i = iterator(); i.hasNext();) { 152 len += ((Sexp) i.next()).getReadableLen(); 153 len++; // a space or the closing paren 154 } 155 return len; 156 } 157 }

This page was automatically generated by Maven