Repository navigation
Expand file tree
/
Copy pathHRow.java
More file actions
107 lines (92 loc) · 2.69 KB
/
Copy pathHRow.java
File metadata and controls
107 lines (92 loc) · 2.69 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
//
// 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.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Map.Entry;
/**
* HRow is a row in a HGrid. It implements the HDict interface also.
*
* @see <a href='http://project-haystack.org/doc/Grids'>Project Haystack</a>
*/
public class HRow extends HDict
{
/** Package private constructor */
HRow(HGrid grid, HVal[] cells)
{
this.grid = grid;
this.cells = cells;
}
/** Get the grid associated with this row */
public HGrid grid() { return grid; }
/** Number of columns in grid (which may map to null cells) */
public int size() { return grid.cols.length; }
/** Get a cell by column name. If the column is undefined or
the cell is null then raise UnknownNameException or return
null based on checked flag. */
public HVal get(String name, boolean checked)
{
HCol col = grid.col(name, false);
if (col != null)
{
HVal val = cells[col.index];
if (val != null) return val;
}
if (checked) throw new UnknownNameException(name);
return null;
}
/** Get a cell by column. If cell is null then raise
UnknownNameException or return null based on checked flag. */
public HVal get(HCol col, boolean checked)
{
HVal val = cells[col.index];
if (val != null) return val;
if (checked) throw new UnknownNameException(col.name());
return null;
}
/** Return Map.Entry name/value iterator which only includes
non-null cells */
public Iterator iterator()
{
return new RowIterator();
}
//////////////////////////////////////////////////////////////////////////
// RowIterator
//////////////////////////////////////////////////////////////////////////
class RowIterator implements Iterator
{
RowIterator()
{
for (; col < grid.cols.length; ++col)
if (cells[col] != null) break;
}
public boolean hasNext()
{
return col < grid.cols.length;
}
public Object next()
{
if (col >= grid.cols.length) throw new NoSuchElementException();
String name = grid.col(col).name();
HVal val = cells[col];
for (col++; col < grid.cols.length; ++col) if (cells[col] != null) break;
return new MapEntry(name, val);
}
public void remove()
{
throw new UnsupportedOperationException();
}
private int col = 0;
}
//////////////////////////////////////////////////////////////////////////
// Fields
//////////////////////////////////////////////////////////////////////////
private HGrid grid;
private HVal[] cells;
}