|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2023 European Spallation Source ERIC. |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License |
| 7 | + * as published by the Free Software Foundation; either version 2 |
| 8 | + * of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.epics.pva.data; |
| 21 | + |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.BitSet; |
| 25 | +import java.util.logging.Level; |
| 26 | + |
| 27 | +import static org.epics.pva.PVASettings.logger; |
| 28 | + |
| 29 | +/** PV Access structure |
| 30 | + * |
| 31 | + * <p>Holds one or more {@link PVAny} elements. |
| 32 | + */ |
| 33 | +public class PVAAnyArray extends PVADataWithID implements PVAArray{ |
| 34 | + |
| 35 | + /** |
| 36 | + * Empty array constructor |
| 37 | + * @param name Name |
| 38 | + */ |
| 39 | + public PVAAnyArray(String name) { |
| 40 | + super(name); |
| 41 | + } |
| 42 | + |
| 43 | + /** Basic constructor |
| 44 | + * |
| 45 | + * @param name Name |
| 46 | + * @param elements Initial elements |
| 47 | + */ |
| 48 | + public PVAAnyArray(String name, final PVAny... elements) { |
| 49 | + super(name); |
| 50 | + this.elements = elements; |
| 51 | + } |
| 52 | + |
| 53 | + /** Unmodifiable list of elements. |
| 54 | + * |
| 55 | + * <p>The value of each element may be updated, but |
| 56 | + * no elements can be added, removed, replaced. |
| 57 | + */ |
| 58 | + private volatile PVAny[] elements; |
| 59 | + |
| 60 | + @Override |
| 61 | + public void setValue(Object new_value) throws Exception { |
| 62 | + |
| 63 | + // Cannot set array, only individual elements |
| 64 | + throw new Exception("Cannot set " + getType() + " " + name + " to " + new_value); |
| 65 | + } |
| 66 | + |
| 67 | + /** @return Current value */ |
| 68 | + public PVAny[] get() |
| 69 | + { |
| 70 | + return elements; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public PVAData cloneType(String name) { |
| 75 | + return new PVAAnyArray(name); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public PVAAnyArray cloneData() { |
| 80 | + final PVAny[] safe = elements; |
| 81 | + final PVAny[] copy = new PVAny[safe.length]; |
| 82 | + // Deep copy |
| 83 | + for (int i=0; i<copy.length; ++i) |
| 84 | + copy[i] = safe[i].cloneData(); |
| 85 | + return new PVAAnyArray(name, copy); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void encodeType(ByteBuffer buffer, BitSet described) throws Exception { |
| 90 | + final short type_id = getTypeID(); |
| 91 | + if (type_id != 0) { |
| 92 | + final int u_type_id = Short.toUnsignedInt(type_id); |
| 93 | + if (described.get(u_type_id)) |
| 94 | + { // Refer to existing definition |
| 95 | + buffer.put(PVAFieldDesc.ONLY_ID_TYPE_CODE); |
| 96 | + buffer.putShort(type_id); |
| 97 | + // Done! |
| 98 | + return; |
| 99 | + } |
| 100 | + else |
| 101 | + { // (Re-)define this type |
| 102 | + buffer.put(PVAFieldDesc.FULL_WITH_ID_TYPE_CODE); |
| 103 | + buffer.putShort(type_id); |
| 104 | + described.set(u_type_id); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Encode 'any array' |
| 109 | + buffer.put((byte) (PVAComplex.FIELD_DESC_TYPE | PVAFieldDesc.Array.VARIABLE_SIZE.mask | PVAComplex.VARIANT_UNION)); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void decode(PVATypeRegistry types, ByteBuffer buffer) throws Exception { |
| 114 | + |
| 115 | + final int count = PVASize.decodeSize(buffer); |
| 116 | + // Try to re-use elements |
| 117 | + PVAny[] new_elements = elements; |
| 118 | + if (new_elements == null || new_elements.length != count) |
| 119 | + new_elements = new PVAny[count]; |
| 120 | + for (int i=0; i<count; ++i) |
| 121 | + { // Is this element non-null? |
| 122 | + final boolean non_null = PVABool.decodeBoolean(buffer); |
| 123 | + if (non_null) |
| 124 | + { // Try to update existing element |
| 125 | + PVAny element = new_elements[i]; |
| 126 | + if (element == null) |
| 127 | + element = new PVAny("any"); |
| 128 | + element.decode(types, buffer); |
| 129 | + new_elements[i] = element; |
| 130 | + } |
| 131 | + else |
| 132 | + new_elements[i] = null; |
| 133 | + } |
| 134 | + elements = new_elements; |
| 135 | + logger.log(Level.FINER, () -> "Decoded any[] element: " + elements); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void encode(ByteBuffer buffer) throws Exception { |
| 140 | + |
| 141 | + final PVAny[] copy = elements; |
| 142 | + PVASize.encodeSize(copy.length, buffer); |
| 143 | + for (PVAny pvAny : copy) { |
| 144 | + if (pvAny == null) |
| 145 | + PVABool.encodeBoolean(false, buffer); |
| 146 | + else { |
| 147 | + PVABool.encodeBoolean(true, buffer); |
| 148 | + pvAny.encode(buffer); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + protected int update(int index, PVAData new_value, BitSet changes) throws Exception { |
| 155 | + |
| 156 | + if (new_value instanceof PVAAnyArray) |
| 157 | + { |
| 158 | + final PVAAnyArray other = (PVAAnyArray) new_value; |
| 159 | + if (! Arrays.equals(other.elements, elements)) |
| 160 | + { |
| 161 | + // Deep copy |
| 162 | + final PVAny[] copy = new PVAny[other.elements.length]; |
| 163 | + for (int i=0; i<copy.length; ++i) |
| 164 | + copy[i] = other.elements[i].cloneData(); |
| 165 | + this.elements = copy; |
| 166 | + changes.set(index); |
| 167 | + } |
| 168 | + } |
| 169 | + return index + 1; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + protected void format(int level, StringBuilder buffer) { |
| 174 | + |
| 175 | + indent(level, buffer); |
| 176 | + buffer.append(getType()); |
| 177 | + buffer.append(name); |
| 178 | + for (PVAData element : elements) |
| 179 | + { |
| 180 | + buffer.append("\n"); |
| 181 | + element.format(level+1, buffer); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public String getType() { |
| 187 | + return "any[]"; |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public boolean equals(Object obj) { |
| 192 | + if (! (obj instanceof PVAAnyArray)) |
| 193 | + return false; |
| 194 | + final PVAAnyArray other = (PVAAnyArray) obj; |
| 195 | + return Arrays.equals(other.elements, elements); |
| 196 | + } |
| 197 | +} |
0 commit comments