OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
AbstractIterator.java
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2024 ADLINK
5  * Technology Limited, its affiliated companies and licensors. All rights
6  * reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 package org.opensplice.dds.sub;
22 
23 import java.lang.reflect.Field;
24 
25 import org.omg.dds.sub.Sample;
29 
30 import DDS.SampleInfoSeqHolder;
31 
32 public abstract class AbstractIterator<TYPE> implements Sample.Iterator<TYPE> {
35  protected final SampleInfoSeqHolder infoSeqHolder;
36  protected final Object sampleSeqHolder;
37  protected final Field dataSeqHolderValue;
38  private final SampleImpl<TYPE>[] samples;
39  private int currentIndex;
40  private int initUntil;
41 
43  AbstractDataReader<TYPE> reader, Object sampleSeqHolder,
44  Field dataSeqHolderValue, SampleInfoSeqHolder infoSeqHolder) {
45  this.environment = environment;
46  this.reader = reader;
47  this.infoSeqHolder = infoSeqHolder;
48  this.sampleSeqHolder = sampleSeqHolder;
49  this.dataSeqHolderValue = dataSeqHolderValue;
50  this.currentIndex = 0;
51  this.initUntil = -1;
52  this.samples = this.setupSampleList();
53  this.reader.registerIterator(this);
54  }
55 
56  protected abstract SampleImpl<TYPE>[] setupSampleList();
57 
58  protected abstract TYPE getData(int index);
59 
60  @Override
61  public boolean hasNext() {
62  if (this.currentIndex == -1) {
63  throw new AlreadyClosedExceptionImpl(this.environment,
64  "Iterator already closed.");
65  }
66  if (infoSeqHolder.value.length > this.currentIndex) {
67  return true;
68  }
69  return false;
70  }
71 
72  @Override
73  public boolean hasPrevious() {
74  if (this.currentIndex == -1) {
75  throw new AlreadyClosedExceptionImpl(this.environment,
76  "Iterator already closed.");
77  }
78  if ((this.currentIndex - 1) > 0) {
79  return true;
80  }
81  return false;
82  }
83 
84  @Override
85  public int nextIndex() {
86  if (this.currentIndex == -1) {
87  throw new AlreadyClosedExceptionImpl(this.environment,
88  "Iterator already closed.");
89  }
90  return this.currentIndex;
91  }
92 
93  @Override
94  public int previousIndex() {
95  if (this.currentIndex == -1) {
96  throw new AlreadyClosedExceptionImpl(this.environment,
97  "Iterator already closed.");
98  }
99  return this.currentIndex - 1;
100  }
101 
102  @Override
103  public void close() {
104  if (this.currentIndex == -1) {
105  throw new AlreadyClosedExceptionImpl(this.environment,
106  "Iterator already closed.");
107  }
108  this.currentIndex = -1;
109  this.reader.returnLoan(this.sampleSeqHolder, this.infoSeqHolder);
110  this.reader.deregisterIterator(this);
111  }
112 
113  @Override
114  public void remove() {
115  throw new UnsupportedOperationExceptionImpl(this.environment,
116  "Cannot remove() from Sample.Iterator.");
117  }
118 
119  @Override
120  public void set(Sample<TYPE> o) {
121  throw new UnsupportedOperationExceptionImpl(this.environment,
122  "Cannot set() in Sample.Iterator.");
123  }
124 
125  @Override
126  public void add(Sample<TYPE> o) {
127  throw new UnsupportedOperationExceptionImpl(this.environment,
128  "Cannot add() to Sample.Iterator.");
129  }
130 
131  @Override
132  public Sample<TYPE> next() {
133  if (this.currentIndex == -1) {
134  throw new AlreadyClosedExceptionImpl(this.environment,
135  "Iterator already closed.");
136  }
137  int index = this.currentIndex++;
138 
139  if (this.initUntil < index) {
140  this.samples[index] = new SampleImpl<TYPE>(this.environment,
141  this.getData(index), this.infoSeqHolder.value[index]);
142  this.initUntil++;
143  }
144  return this.samples[index];
145  }
146 
147  @Override
149  if (this.currentIndex == -1) {
150  throw new AlreadyClosedExceptionImpl(this.environment,
151  "Iterator already closed.");
152  }
153  return this.samples[--this.currentIndex];
154  }
155 }
final OsplServiceEnvironment environment
A Sample represents an atom of data information (i.e., one value for one instance) as returned by a o...
Definition: Sample.java:116
abstract SampleImpl< TYPE > [] setupSampleList()
abstract TYPE getData(int index)
final AbstractDataReader< TYPE > reader
void deregisterIterator(AbstractIterator< TYPE > iterator)
AbstractIterator(OsplServiceEnvironment environment, AbstractDataReader< TYPE > reader, Object sampleSeqHolder, Field dataSeqHolderValue, SampleInfoSeqHolder infoSeqHolder)
void returnLoan(Object sampleSeqHolder, DDS.SampleInfoSeqHolder infoSeqHolder)
void registerIterator(AbstractIterator< TYPE > iterator)