All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metafacture.statistics.Counter Maven / Gradle / Ivy

There is a newer version: 6.2.0
Show newest version
/*
 * Copyright 2013, 2014 Deutsche Nationalbibliothek
 *
 * Licensed under the Apache License, Version 2.0 the "License";
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metafacture.statistics;

import org.metafacture.framework.FluxCommand;
import org.metafacture.framework.StreamReceiver;
import org.metafacture.framework.annotations.Description;
import org.metafacture.framework.annotations.In;
import org.metafacture.framework.annotations.Out;
import org.metafacture.framework.helpers.DefaultStreamPipe;


/**
 * Counts the number of records and fields read. Used mainly for test cases and
 * debugging.
 *
 * @author Markus Michael Geipel
 * @author Christoph Böhme
 * @author Michael Büchner
 *
 */
@Description("Counts the number of records and fields read.")
@In(StreamReceiver.class)
@Out(StreamReceiver.class)
@FluxCommand("stream-count")
public final class Counter extends DefaultStreamPipe {

    private int numRecords;
    private int numEntities;
    private int numLiterals;

    /**
     * @return the numRecords
     */
    public int getNumRecords() {
        return numRecords;
    }

    /**
     * @return the numEntities
     */
    public int getNumEntities() {
        return numEntities;
    }

    /**
     * @return the numLiterals
     */
    public int getNumLiterals() {
        return numLiterals;
    }

    @Override
    public void startRecord(final String identifier) {
        assert !isClosed();
        ++numRecords;
        if(getReceiver() != null) {
            getReceiver().startRecord(identifier);
        }
    }

    @Override
    public void startEntity(final String name) {
        assert !isClosed();
        ++numEntities;
        if(getReceiver() != null) {
            getReceiver().startEntity(name);
        }
    }

    @Override
    public void literal(final String name, final String value) {
        assert !isClosed();
        ++numLiterals;
        if(getReceiver() != null) {
            getReceiver().literal(name, value);
        }
    }

    @Override
    public void endRecord() {
        if(getReceiver() != null) {
            getReceiver().endRecord();
        }
    }

    @Override
    public void endEntity() {
        if(getReceiver() != null) {
            getReceiver().endEntity();
        }
    }

    @Override
    public void onResetStream() {
        numRecords = 0;
        numEntities = 0;
        numLiterals = 0;
    }

    @Override
    public String toString() {
        String streamClosed = "";
        if (isClosed()) {
            streamClosed =" Stream has been closed.";
        }

        return "counted " + numRecords + " records, " + numEntities + " entities, " + numLiterals + " literals." + streamClosed;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy