Repository navigation
Expand file tree
/
Copy pathHGrid.java
More file actions
194 lines (157 loc) · 5 KB
/
Copy pathHGrid.java
File metadata and controls
194 lines (157 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// Copyright (c) 2012, Brian Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 24 Sep 2012 Brian Frank Creation
//
package org.projecthaystack;
import java.io.PrintWriter;
import java.util.*;
import org.projecthaystack.io.*;
/**
* HGrid is an immutable two dimension data structure of cols and rows.
* Use HGridBuilder to construct a HGrid instance.
*
* @see <a href='http://project-haystack.org/doc/Grids'>Project Haystack</a>
*/
public class HGrid extends HVal
{
//////////////////////////////////////////////////////////////////////////
// Package private constructor
//////////////////////////////////////////////////////////////////////////
/** Empty grid with one column called "empty" and zero rows */
public static final HGrid EMPTY = new HGrid(
HDict.EMPTY,
new HCol[] { new HCol(0, "empty", HDict.EMPTY) },
new ArrayList());
/** Package private constructor */
HGrid(HDict meta, HCol[] cols, ArrayList rowList)
{
this.meta = meta;
this.cols = cols;
if (meta == null)
throw new IllegalStateException("metadata cannot be null");
this.rows = new HRow[rowList.size()];
for (int i=0; i<rows.length; ++i)
{
HVal[] cells = (HVal[])rowList.get(i);
if (cols.length != cells.length)
throw new IllegalStateException("Row cells size != cols size");
this.rows[i] = new HRow(this, cells);
}
this.colsByName = new HashMap();
for (int i=0; i<cols.length; ++i)
{
HCol col = cols[i];
String colName = col.name;
if (colsByName.get(colName) != null)
throw new IllegalStateException("Duplicate col name: " + colName);
colsByName.put(colName, col);
}
}
//////////////////////////////////////////////////////////////////////////
// Access
//////////////////////////////////////////////////////////////////////////
/** Return grid level meta */
public HDict meta() { return meta; }
/** Error grid have the meta.err marker tag */
public boolean isErr() { return meta.has("err"); }
/** Return if number of rows is zero */
public boolean isEmpty() { return numRows() == 0; }
/** Return number of rows */
public int numRows() { return rows.length; }
/** Get a row by its zero based index */
public HRow row(int row) { return rows[row]; }
/** Get number of columns */
public int numCols() { return cols.length; }
/** Get a column by its index */
public HCol col(int index) { return cols[index]; }
/** Convenience for "col(name, true)" */
public HCol col(String name) { return col(name, true); }
/** Get a column by name. If not found and checked if false then
return null, otherwise throw UnknownNameException */
public HCol col(String name, boolean checked)
{
HCol col = (HCol)colsByName.get(name);
if (col != null) return col;
if (checked) throw new UnknownNameException(name);
return null;
}
/** Create iteratator to walk each row */
public Iterator iterator()
{
return new GridIterator();
}
//////////////////////////////////////////////////////////////////////////
// HVal
//////////////////////////////////////////////////////////////////////////
public String toZinc()
{
return HZincWriter.gridToString(this);
}
public String toJson()
{
throw new UnsupportedOperationException();
}
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HGrid hGrid = (HGrid) o;
if (!meta.equals(hGrid.meta)) return false;
if (!Arrays.equals(cols, hGrid.cols)) return false;
if (!Arrays.equals(rows, hGrid.rows)) return false;
return true;
}
public int hashCode()
{
int result = Arrays.hashCode(rows);
result = 31 * result + Arrays.hashCode(cols);
result = 31 * result + meta.hashCode();
return result;
}
//////////////////////////////////////////////////////////////////////////
// Debug
//////////////////////////////////////////////////////////////////////////
/** Convenience for "dump(stdout)". */
public void dump()
{
dump(new PrintWriter(System.out));
}
/** Debug dump - this is Zinc right now. */
public void dump(PrintWriter out)
{
out.println(HZincWriter.gridToString(this));
out.flush();
}
//////////////////////////////////////////////////////////////////////////
// GridIterator
//////////////////////////////////////////////////////////////////////////
class GridIterator implements Iterator
{
public boolean hasNext()
{
return pos < rows.length;
}
public Object next()
{
if (hasNext())
return rows[pos++];
else
throw new NoSuchElementException();
}
public void remove()
{
throw new UnsupportedOperationException();
}
private int pos = 0;
}
//////////////////////////////////////////////////////////////////////////
// Rows
//////////////////////////////////////////////////////////////////////////
final HRow[] rows;
final HCol[] cols;
final HashMap colsByName;
final HDict meta;
}