<commit_before>package no.ssb.vtl.model;

/*-
 * ========================LICENSE_START=================================
 * Java VTL
 * %%
 * Copyright (C) 2016 - 2017 <NAME>
 * %%
 * 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.
 * =========================LICENSE_END==================================
 */

import com.google.common.annotations.Beta;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.Preconditions.*;
import static no.ssb.vtl.model.Order.Direction.*;

/**
 * Represent the ordering the {@link DataPoint}s in a Dataset.
 */
public final class Order extends ForwardingMap<Component, Order.Direction> implements Comparator<DataPoint> {

    public static final Comparator<Comparable> NULLS_FIRST = Comparator.<Comparable>nullsFirst(Comparator.naturalOrder());

    public static final Comparator<Entry<String, Component>> BY_ROLE = Comparator.comparing(
            entry -> entry.getValue().getRole(),
            Ordering.explicit(
                    Component.Role.IDENTIFIER,
                    Component.Role.MEASURE,
                    Component.Role.ATTRIBUTE
            )
    );

    public static final Comparator<Entry<String, Component>> BY_NAME = Comparator.comparing(Entry::getKey);

    private final DataStructure structure;
    private final ImmutableMap<Component, Direction> delegate;

    Order(DataStructure structure, ImmutableMap<Component, Direction> orders) {
        this.delegate = ImmutableMap.copyOf(orders);
        this.structure = checkNotNull(structure);
    }

    /**
     * Create a new Order.Builder instance.
     *
     * @param structure the structure this Order will operate on.
     */
    public static Builder create(DataStructure structure) {
        return new Builder(structure);
    }

    /**
     * Create a copy of the order.
     * <p>
     * Useful to change data structure.
     */
    public static Order.Builder createCopyOf(Order order) {
        return create(order.structure);
    }

    /**
     * Return the default Order for the given DataStructure.
     */
    public static Order createDefault(DataStructure dataStructure) {
        Set<Entry<String, Component>> sortedEntrySet = Sets.newTreeSet(BY_ROLE.thenComparing(BY_NAME));
        sortedEntrySet.addAll(dataStructure.entrySet());

        ImmutableMap.Builder<Component, Direction> order = ImmutableMap.builder();
        for (Entry<String, Component> entry : sortedEntrySet) {
            order.put(entry.getValue(), ASC);
        }
        return new Order(dataStructure, order.build());
    }

    @Override
    public int compare(DataPoint o1, DataPoint o2) {
        int result;

        // TODO migrate to Map<Component, Direction> and remove the DataStructure dependency.
        Map<Component, ? extends Comparable> m1 = structure.asMap(o1), m2 = structure.asMap(o2);
        for (Entry<Component, Direction> order : delegate.entrySet()) {
            result = NULLS_FIRST.compare(m1.get(order.getKey()), m2.get(order.getKey()));
            if (result != 0) {
                return order.getValue() == ASC ? result : -result;
            }
        }

        // TODO build an index?
//        Comparable[] c1 = new Comparable[1], c2 = new Comparable[1];
//        ImmutableMap<Integer, Direction> index = ImmutableMap.copyOf(Collections.emptyMap());<commit_msg>Remove measures in the default order<commit_after>package no.ssb.vtl.model;

/*-
 * ========================LICENSE_START=================================
 * Java VTL
 * %%
 * Copyright (C) 2016 - 2017 <NAME>
 * %%
 * 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.
 * =========================LICENSE_END==================================
 */

import com.google.common.annotations.Beta;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.*;
import static no.ssb.vtl.model.Order.Direction.*;

/**
 * Represent the ordering the {@link DataPoint}s in a Dataset.
 */
public final class Order extends ForwardingMap<Component, Order.Direction> implements Comparator<DataPoint> {

    public static final Comparator<Comparable> NULLS_FIRST = Comparator.<Comparable>nullsFirst(Comparator.naturalOrder());

    public static final Comparator<Entry<String, Component>> BY_ROLE = Comparator.comparing(
            entry -> entry.getValue().getRole(),
            Ordering.explicit(
                    Component.Role.IDENTIFIER,
                    Component.Role.MEASURE,
                    Component.Role.ATTRIBUTE
            )
    );

    public static final Comparator<Entry<String, Component>> BY_NAME = Comparator.comparing(Entry::getKey);

    private final DataStructure structure;
    private final ImmutableMap<Component, Direction> delegate;

    Order(DataStructure structure, ImmutableMap<Component, Direction> orders) {
        this.delegate = ImmutableMap.copyOf(orders);
        this.structure = checkNotNull(structure);
    }

    /**
     * Create a new Order.Builder instance.
     *
     * @param structure the structure this Order will operate on.
     */
    public static Builder create(DataStructure structure) {
        return new Builder(structure);
    }

    /**
     * Create a copy of the order.
     * <p>
     * Useful to change data structure.
     */
    public static Order.Builder createCopyOf(Order order) {
        return create(order.structure);
    }

    /**
     * Return the default Order for the given DataStructure.
     */
    public static Order createDefault(DataStructure dataStructure) {
        Map<Component, Direction> order = dataStructure.entrySet().stream()
                .filter(e -> e.getValue().isIdentifier())
                .sorted(BY_ROLE.thenComparing(BY_NAME))
                .collect(Collectors.toMap(o -> o.getValue(), o -> ASC));
        return new Order(dataStructure, ImmutableMap.copyOf(order));
    }

    @Override
    public int compare(DataPoint o1, DataPoint o2) {
        int result;

        // TODO migrate to Map<Component, Direction> and remove the DataStructure dependency.
        Map<Component, ? extends Comparable> m1 = structure.asMap(o1), m2 = structure.asMap(o2);
        for (Entry<Component, Direction> order : delegate.entrySet()) {
            result = NULLS_FIRST.compare(m1.get(order.getKey()), m2.get(order.getKey()));
            if (result != 0) {
                return order.getValue() == ASC ? result : -result;
            }
        }

        // TODO build an index?
//        Comparable[] c1 = new Comparable[1], c2 = new Comparable[1];
//        ImmutableMap<Integer, Direction> index = ImmutableMap.copyOf(Collections.emptyMap());
{'fraction_non_alphanumeric': 0.1295496607032696, 'fraction_numerical': 0.006169031462060457, 'mean_word_length': 3.112633181126332, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2010751', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2120, 'n_words': 611}