whyclj
2021-01-05 64aba412c2b739d67795b14a3cae069d311697f9
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
package com.intelligt.modbus.jlibmodbus.data;
 
import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.exception.IllegalDataAddressException;
import com.intelligt.modbus.jlibmodbus.exception.IllegalDataValueException;
 
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Observable;
 
abstract public class ModbusValues<T> extends Observable implements Iterable<T> {
 
    abstract public int getQuantity();
 
    abstract public T get(int offset) throws IllegalDataAddressException;
 
    abstract public void setImpl(int offset, T value) throws IllegalDataAddressException, IllegalDataValueException;
 
    abstract public int getByteCount();
 
    abstract public byte[] getBytes();
 
    abstract public void setBytesBe(byte[] bytes);
 
    final public void set(int offset, T value) throws IllegalDataAddressException, IllegalDataValueException {
        setImpl(offset, value);
        setChanged();
        notifyObservers(new int[]{offset, 1});
    }
 
    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
 
            int index = 0;
 
            @Override
            public boolean hasNext() {
                return index < getQuantity();
            }
 
            @Override
            public T next() {
                if (hasNext()) {
                    try {
                        return get(index++);
                    } catch (IllegalDataAddressException e) {
                        Modbus.log().severe(this.getClass().getSimpleName() + " " + e.getLocalizedMessage() + ": quantity = " + getQuantity() + ", index = " + index);
                        throw new NoSuchElementException();
                    }
                } else {
                    throw new NoSuchElementException();
                }
            }
 
            @Override
            public void remove() {
 
            }
        };
    }
}