Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Java/Threema/app/src/main/java/ch/threema/app/utils/     Datei vom 25.3.2026 mit Größe 8 kB image not shown  

Quelle  CSVRow.java

  Sprache: JAVA
 

package ch.threema.app.utils;

import java.util.Date;

import androidx.annotation.NonNull;
import ch.threema.base.ThreemaException;

public class CSVRow {
    private CSVWriter writer;
    final private String[] header;
    final private String[] data;

    public CSVRow(CSVWriter writer, String[] header) {
        this.writer = writer;
        this.header = header;
        this.data = new String[this.header.length];
    }

    public CSVRow(String[] header, String[] data) {
        this.header = header;
        this.data = data;
    }

    public String getString(int pos) throws ThreemaException {
        if (this.data == null || this.data.length < pos) {
            throw new ThreemaException("invalid csv position");
        }

        return this.data[pos];
    }

    public @NonNull Integer getInteger(int pos) throws ThreemaException {
        return Integer.valueOf(this.getString(pos));
    }

    public @NonNull Long getLong(int pos) throws ThreemaException {
        return Long.valueOf(this.getString(pos));
    }

    public boolean getBoolean(int pos) throws ThreemaException {
        return TestUtil.compare("1"this.getString(pos));
    }

    public Date getDate(int pos) throws ThreemaException {
        String cell = this.getString(pos);
        if (cell != null && !cell.isEmpty()) {
            return new Date(Long.parseLong(cell));
        }

        return null;
    }

    public CSVRow write(String fieldName, Object v) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position");
        }

        return this.write(pos, v);
    }

    public CSVRow write(int pos, Object v) throws ThreemaException {
        if (this.data.length < pos) {
            throw new ThreemaException("invalid position to write [" + pos + "]");
        }

        this.data[pos] = this.escape(v);
        return this;
    }

    public CSVRow write(String fieldName, String v) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position");
        }

        return this.write(pos, v);
    }

    public CSVRow write(int pos, String v) throws ThreemaException {
        if (this.data.length < pos) {
            throw new ThreemaException("invalid position to write [" + pos + "]");
        }

        this.data[pos] = this.escape(v);
        return this;
    }

    public CSVRow write(String fieldName, boolean v) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position");
        }

        return this.write(pos, v);
    }

    public CSVRow write(int pos, int v) throws ThreemaException {
        if (this.data.length < pos) {
            throw new ThreemaException("invalid position to write [" + pos + "]");
        }

        this.data[pos] = this.escape(v);
        return this;
    }

    public CSVRow write(String fieldName, int v) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position");
        }

        return this.write(pos, v);
    }

    public CSVRow write(int pos, boolean v) throws ThreemaException {
        if (this.data.length < pos) {
            throw new ThreemaException("invalid position to write [" + pos + "]");
        }

        this.data[pos] = this.escape(v);
        return this;
    }

    public CSVRow write(String fieldName, Date v) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position");
        }

        return this.write(pos, v);
    }

    public CSVRow write(int pos, Date v) throws ThreemaException {
        if (this.data.length < pos) {
            throw new ThreemaException("invalid position to write [" + pos + "]");
        }

        this.data[pos] = this.escape(v);
        return this;
    }

    public CSVRow write(String fieldName, Object[] v) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position");
        }

        return this.write(pos, v);
    }

    public CSVRow write(int pos, Object[] v) throws ThreemaException {
        if (this.data.length < pos) {
            throw new ThreemaException("invalid position to write [" + pos + "]");
        }

        this.data[pos] = this.escape(v);
        return this;
    }

    public String[] getStrings(int pos) throws ThreemaException {
        String r = this.getString(pos);
        return TestUtil.isEmptyOrNull(r) ? new String[]{} : r.split(";");
    }

    public String getString(String fieldName) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position [" + fieldName + "]");
        }
        return this.getString(pos);
    }

    public @NonNull Integer getInteger(String fieldName) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position [" + fieldName + "]");
        }
        return this.getInteger(pos);
    }

    public @NonNull Long getLong(String fieldName) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position [" + fieldName + "]");
        }
        return this.getLong(pos);
    }

    public boolean getBoolean(String fieldName) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position [" + fieldName + "]");
        }
        return this.getBoolean(pos);
    }

    public Date getDate(String fieldName) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position [" + fieldName + "]");
        }
        return this.getDate(pos);

    }

    public String[] getStrings(String fieldName) throws ThreemaException {
        int pos = this.getValuePosition(fieldName);
        if (pos < 0) {
            throw new ThreemaException("invalid csv header position [" + fieldName + "]");
        }
        return this.getStrings(pos);

    }

    /**
     * Return the CSV column index for the specified column name.
     *
     * @return the index, or -1 if the column was not found.
     */

    public int getValuePosition(String fieldName) {
        if (this.header != null && fieldName != null) {
            for (int n = 0; n < this.header.length; n++) {
                if (fieldName.equals(this.header[n])) {
                    return n;
                }
            }
        }

        return -1;
    }

    public boolean hasField(@NonNull String fieldName) {
        return getValuePosition(fieldName) != -1;
    }

    public void write() {
        if (this.writer != null) {
            this.writer.writeNext(this.data);
        }
    }


    /**
     * return a csv well formed string
     */

    private String escape(Date date) {
        if (date == null) {
            return "";
        }
        return String.valueOf(date.getTime());
    }

    /**
     * return a csv well formed string
     */

    private String escape(boolean bool) {
        return bool ? "1" : "0";
    }

    /**
     * return a csv well formed string
     */

    private String escape(String ns) {
        if (ns == null) {
            return "";
        }
        return ns.replace("\\""\\\\");
    }

    private String escape(Object[] os) {
        String result = "";
        if (os != null) {
            for (Object o : os) {
                if (!result.isEmpty()) {
                    result += ';';
                }

                result += this.escape(o);
            }
        }

        return result;
    }

    /**
     * return a csv well formed string
     */

    private String escape(Object ns) {
        if (ns == null) {
            return "";
        }

        return this.escape(ns.toString());
    }
}

Messung V0.5 in Prozent
C=98 H=94 G=95

¤ Dauer der Verarbeitung: 0.1 Sekunden  (vorverarbeitet am  2026-04-27) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.