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

org.metafacture.triples.TripleCount 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.triples;

import java.util.Comparator;

import org.metafacture.framework.FluxCommand;
import org.metafacture.framework.annotations.Description;
import org.metafacture.framework.annotations.In;
import org.metafacture.framework.annotations.Out;
import org.metafacture.framework.objects.Triple;

/**
 * Counts triples.
 *
 * @author markus geipel
 *
 */
@Description("Counts triples")
@In(Triple.class)
@Out(Triple.class)
@FluxCommand("count-triples")
public final class TripleCount extends AbstractTripleSort {

    public static final String DEFAULT_COUNTP_REDICATE = "count";

    private static final Triple INIT = new Triple("", "", "");

    private Triple current = INIT;
    private int count;
    private String countPredicate = DEFAULT_COUNTP_REDICATE;
    private Comparator comparator;

    @Override
    protected void sortedTriple(final Triple triple) {

        if(current==INIT){
            current = triple;
            comparator = createComparator();
        }

        if(comparator.compare(current, triple)==0){
            ++count;
        }else{
            writeResult();
            current = triple;
            count = 1;
        }
    }

    public void setCountPredicate(final String countPredicate) {
        this.countPredicate = countPredicate;
    }

    @Override
    protected void onFinished() {
        writeResult();
    }

    private void writeResult() {
        final Compare compareBy = getCompare();
        switch (compareBy) {
        case ALL:
            getReceiver().process(new Triple(current.toString(), countPredicate , String.valueOf(count)));
            break;
        case OBJECT:
            getReceiver().process(new Triple(current.getObject(), countPredicate, String.valueOf(count)));
            break;
        case PREDICATE:
            getReceiver().process(new Triple(current.getPredicate(), countPredicate, String.valueOf(count)));
            break;
        case SUBJECT:
        default:
            getReceiver().process(new Triple(current.getSubject(), countPredicate, String.valueOf(count)));
            break;
        }
    }

    public void setCountBy(final Compare countBy){
        setCompare(countBy);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy