Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 39be994e670d8806173a8129166cfc3b0efae8bb (plain) (blame)
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
195
196
197
198
/*
 * Copyright (c) 2014 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.oomph.internal.util.table;

import org.eclipse.oomph.internal.util.table.Cell.Visitor;

import java.text.Format;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author Eike Stepper
 */
public abstract class AbstractRange implements Range
{
  public abstract Table table();

  public abstract Iterator<Cell> iterator();

  public Set<Cell> set()
  {
    Set<Cell> set = new HashSet<Cell>();
    fillCells(set);
    return set;
  }

  public List<Cell> list()
  {
    List<Cell> list = new ArrayList<Cell>();
    fillCells(list);
    return list;
  }

  public int accept(Visitor visitor) throws Exception
  {
    int n = 0;
    for (Cell cell : this)
    {
      if (!visitor.visit(cell, n++))
      {
        break;
      }
    }

    return n;
  }

  public boolean contains(int col, int row)
  {
    for (Cell cell : this)
    {
      if (cell.col == col && cell.row == row)
      {
        return true;
      }
    }

    return false;
  }

  public boolean contains(Coordinate coordinate)
  {
    return contains(coordinate.col, coordinate.row);
  }

  public boolean contains(Cell cell)
  {
    return contains(cell.col, cell.row);
  }

  public boolean contains(Range range)
  {
    for (Cell cell : range)
    {
      if (!contains(cell))
      {
        return false;
      }
    }

    return true;
  }

  public Range offset(int cols, int rows)
  {
    return new OffsetRange(this, cols, rows);
  }

  public Range addRange(Coordinate coordinate1, Coordinate coordinate2)
  {
    return addRanges(table().range(coordinate1, coordinate2));
  }

  public Range addRange(int col1, int row1, int col2, int row2)
  {
    return addRange(new Coordinate(col1, row1), new Coordinate(col2, row2));
  }

  public Range addRanges(Range... ranges)
  {
    Range result = new ComposedRange(table(), this);
    return result.addRanges(ranges);
  }

  public Range subtractRange(Coordinate coordinate1, Coordinate coordinate2)
  {
    return subtractRanges(table().range(coordinate1, coordinate2));
  }

  public Range subtractRange(int col1, int row1, int col2, int row2)
  {
    return subtractRange(new Coordinate(col1, row1), new Coordinate(col2, row2));
  }

  public Range subtractRanges(Range... ranges)
  {
    Range result = new ComposedRange(table(), this);
    return result.subtractRanges(ranges);
  }

  public Range value(Object value)
  {
    for (Cell cell : this)
    {
      cell.value(value);
    }

    return this;
  }

  public Range format(Format format)
  {
    for (Cell cell : this)
    {
      cell.format(format);
    }

    return this;
  }

  public Range alignment(Alignment alignment)
  {
    for (Cell cell : this)
    {
      cell.alignment(alignment);
    }

    return this;
  }

  @Override
  public int hashCode()
  {
    return set().hashCode();
  }

  @Override
  public boolean equals(Object obj)
  {
    if (this == obj)
    {
      return true;
    }

    if (obj == null)
    {
      return false;
    }

    if (getClass() != obj.getClass())
    {
      return false;
    }

    Range other = (Range)obj;
    return set().equals(other.set());
  }

  private void fillCells(Collection<Cell> cells)
  {
    for (Cell cell : this)
    {
      cells.add(cell);
    }
  }
}

Back to the top