reader.CollectionUtils.java Source code

Java tutorial

Introduction

Here is the source code for reader.CollectionUtils.java

Source

/*******************************************************************************
 * Copyright 2015
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universitt Darmstadt
 *
 * 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 reader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Iterables;

import de.tudarmstadt.ukp.dkpro.core.api.frequency.util.FrequencyDistribution;

/**
 * 
 * @author Roland Kluge
 * 
 */
public final class CollectionUtils {

    public static <T> T firstOf(final Iterable<T> iterable) {
        return nthOf(iterable, 0);
    }

    public static <T> T secondOf(final Iterable<T> iterable) {
        return nthOf(iterable, 1);
    }

    public static <T> T thirdOf(final Iterable<T> iterable) {
        return nthOf(iterable, 2);
    }

    public static <T> T forthOf(final Iterable<T> iterable) {
        return nthOf(iterable, 3);
    }

    /**
     * Retrieves the nth element by linear search.
     * 
     * The first element has index 0.
     * 
     * @param iterable the iterable
     * @param n the 0-based index
     * @return the item
     */
    public static <T> T nthOf(final Iterable<T> iterable, final int n) {
        final Iterator<T> iter = iterable.iterator();
        for (int i = 0; i < n; ++i) {
            iter.next();
        }
        return iter.next();
    }

    public static <T> T lastOf(final Collection<T> collection) {
        if (collection instanceof List) {
            final List<T> list = (List<T>) collection;
            final int lastIndex = list.size() - 1;
            return list.get(lastIndex);
        } else {
            return nthOf(collection, collection.size() - 1);
        }
    }

    public static <T> Collection<T> intersection(final Collection<T> collection1, final Collection<T> collection2) {
        final Set<T> set = new HashSet<T>(collection1);
        set.retainAll(collection2);
        return set;
    }

    public static <T> int intersectionCount(final Collection<T> collection1, final Collection<T> collection2) {
        return intersection(collection1, collection2).size();
    }

    public static <T> Collection<T> union(final Collection<T> collection1, final Collection<T> collection2) {
        final Set<T> set = new HashSet<T>(collection1);
        set.addAll(collection2);
        return set;
    }

    public static <T> int unionCount(final Collection<T> collection1, final Collection<T> collection2) {
        return collection1.size() + collection2.size() - intersectionCount(collection1, collection2);
    }

    public static <T extends Number> double average(final Iterable<T> values) {
        final double sum = sumUp(values);
        final int size = Iterables.size(values);
        return sum / size;
    }

    public static <T extends Number> double sumUp(final Iterable<T> values) {
        double sum = 0.0;

        for (final T value : values) {
            sum += value.doubleValue();
        }
        return sum;
    }

    /**
     * Converts a comma-separated string to a list of integers
     * 
     * @param indexList the string
     * @return a list of integers
     */
    public static List<Integer> parseIntList(final String indexList, final String separator) {
        final List<String> indicesAsString = Arrays.asList(indexList.split(separator));
        final List<Integer> indices = new ArrayList<Integer>();
        for (final String indexAsString : indicesAsString) {
            indices.add(Integer.valueOf(indexAsString));
        }
        return indices;
    }

    public static <T> boolean areAllEqual(final Iterable<T> elements) {
        final T reference = firstOf(elements);
        for (final T element : elements) {
            if (!reference.equals(element)) {
                return false;
            }
        }
        return true;

    }

    public static <T> T getMajorityClassLabel(final Iterable<T> items) {
        final FrequencyDistribution<T> freqDist = new FrequencyDistribution<T>(items);
        return freqDist.getSampleWithMaxFreq();
    }

    public static <T> long getMajorityClassCount(final Iterable<T> items) {
        final FrequencyDistribution<T> freqDist = new FrequencyDistribution<T>(items);
        return freqDist.getMaxFreq();
    }

    public static List<Double> difference(final List<? extends Object> minuend,
            final List<? extends Object> subtrahend) {
        final List<Double> result = new ArrayList<Double>();

        for (int i = 0; i < minuend.size(); ++i) {
            final Object o1 = minuend.get(i);
            final Object o2 = subtrahend.get(i);
            if (areBothNumbers(o1, o2)) {
                subtract(result, o1, o2);
            } else {
                result.add(Double.NaN);
            }
        }

        return result;
    }

    private static boolean areBothNumbers(final Object o1, final Object o2) {
        return o1 instanceof Number && o2 instanceof Number;
    }

    private static void subtract(final List<Double> result, final Object o1, final Object o2) {
        final Number val1 = (Number) o1;
        final Number val2 = (Number) o2;

        result.add(val1.doubleValue() - val2.doubleValue());
    }

    public static <T> double calculateJaccardMeasure(final Collection<T> s1, final Collection<T> s2) {
        if (s1.isEmpty() || s2.isEmpty()) {
            return 0.0;
        }

        return 1.0 * intersectionCount(s1, s2) / unionCount(s1, s2);
    }

    public static double average(final Double... values) {
        return average(Arrays.asList(values));
    }

    public static <T> List<T> nCopies(final int n, final T value) {
        return new ArrayList<T>(Collections.nCopies(n, value));
    }
}