| /*=============================================================================# |
| # Copyright (c) 2009, 2017 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.r.rdata; |
| |
| import java.io.IOException; |
| import java.util.Collections; |
| import java.util.List; |
| |
| import org.eclipse.statet.r.core.model.IRLangElement; |
| import org.eclipse.statet.r.core.model.RElementName; |
| import org.eclipse.statet.rj.data.RJIO; |
| import org.eclipse.statet.rj.data.RObjectFactory; |
| import org.eclipse.statet.rj.data.RStore; |
| import org.eclipse.statet.rj.data.RVector; |
| import org.eclipse.statet.rj.data.impl.ExternalizableRObject; |
| |
| |
| public class RVectorVar<DataType extends RStore> extends CombinedElement |
| implements RVector<DataType>, ExternalizableRObject { |
| |
| |
| private final DataType data; |
| private final long length; |
| |
| private String className1; |
| |
| |
| public RVectorVar(final DataType data, final long length, final String className1, |
| final CombinedElement parent, final RElementName name) { |
| super(parent, name); |
| if (data == null || className1 == null) { |
| throw new NullPointerException(); |
| } |
| this.data = data; |
| this.length = length; |
| this.className1 = className1; |
| } |
| |
| public RVectorVar(final RJIO io, final RObjectFactory factory, |
| final CombinedElement parent, final RElementName name) throws IOException { |
| super(parent, name); |
| |
| //-- options |
| final int options = io.readInt(); |
| //-- special attributes |
| if ((options & RObjectFactory.O_CLASS_NAME) != 0) { |
| this.className1 = io.readString(); |
| } |
| this.length = io.readVULong((byte) (options & RObjectFactory.O_LENGTHGRADE_MASK)); |
| assert ((options & RObjectFactory.O_WITH_NAMES) == 0); |
| //-- data |
| this.data = (DataType) factory.readStore(io, this.length); |
| if ((options & RObjectFactory.O_CLASS_NAME) == 0) { |
| this.className1 = this.data.getBaseVectorRClassName(); |
| } |
| } |
| |
| @Override |
| public void writeExternal(final RJIO io, final RObjectFactory factory) throws IOException { |
| //-- options |
| int options = io.getVULongGrade(this.length); |
| if (!this.className1.equals(this.data.getBaseVectorRClassName())) { |
| options |= RObjectFactory.O_CLASS_NAME; |
| } |
| io.writeInt(options); |
| //-- special attributes |
| if ((options & RObjectFactory.O_CLASS_NAME) != 0) { |
| io.writeString(this.className1); |
| } |
| io.writeVULong((byte) (options & RObjectFactory.O_LENGTHGRADE_MASK), this.length); |
| //-- data |
| factory.writeStore(this.data, io); |
| } |
| |
| |
| @Override |
| public byte getRObjectType() { |
| return TYPE_VECTOR; |
| } |
| |
| @Override |
| public String getRClassName() { |
| return this.className1; |
| } |
| |
| @Override |
| public long getLength() { |
| return this.length; |
| } |
| |
| @Override |
| public RStore getNames() { |
| return null; |
| } |
| |
| |
| @Override |
| public DataType getData() { |
| return this.data; |
| } |
| |
| |
| @Override |
| public int getElementType() { |
| return R_GENERAL_VARIABLE; |
| } |
| |
| @Override |
| public boolean hasModelChildren(final Filter filter) { |
| return false; |
| } |
| |
| @Override |
| public List<? extends IRLangElement> getModelChildren(final Filter filter) { |
| return Collections.emptyList(); |
| } |
| |
| |
| @Override |
| public String toString() { |
| final StringBuilder sb = new StringBuilder(); |
| sb.append("RObject type=vector, class=").append(getRClassName()); |
| sb.append("\n\tlength=").append(getLength()); |
| sb.append("\n\tdata: "); |
| sb.append(this.data.toString()); |
| return sb.toString(); |
| } |
| |
| } |