Example usage for org.apache.commons.lang3.tuple MutablePair of

List of usage examples for org.apache.commons.lang3.tuple MutablePair of

Introduction

In this page you can find the example usage for org.apache.commons.lang3.tuple MutablePair of.

Prototype

public static <L, R> MutablePair<L, R> of(final L left, final R right) 

Source Link

Document

Obtains an immutable pair of from two objects inferring the generic types.

This factory allows the pair to be created using inference to obtain the generic types.

Usage

From source file:org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor.java

private void determineGlobalShadowOrder() {
    // Topological sorting with Kahn's algorithm
    List<Pair<ShadowVariableDescriptor<Solution_>, Integer>> pairList = new ArrayList<>();
    Map<ShadowVariableDescriptor<Solution_>, Pair<ShadowVariableDescriptor<Solution_>, Integer>> shadowToPairMap = new HashMap<>();
    for (EntityDescriptor<Solution_> entityDescriptor : entityDescriptorMap.values()) {
        for (ShadowVariableDescriptor<Solution_> shadow : entityDescriptor
                .getDeclaredShadowVariableDescriptors()) {
            int sourceSize = shadow.getSourceVariableDescriptorList().size();
            Pair<ShadowVariableDescriptor<Solution_>, Integer> pair = MutablePair.of(shadow, sourceSize);
            pairList.add(pair);//from  w w w . j ava2 s . c  om
            shadowToPairMap.put(shadow, pair);
        }
    }
    for (EntityDescriptor<Solution_> entityDescriptor : entityDescriptorMap.values()) {
        for (GenuineVariableDescriptor<Solution_> genuine : entityDescriptor
                .getDeclaredGenuineVariableDescriptors()) {
            for (ShadowVariableDescriptor<Solution_> sink : genuine.getSinkVariableDescriptorList()) {
                Pair<ShadowVariableDescriptor<Solution_>, Integer> sinkPair = shadowToPairMap.get(sink);
                sinkPair.setValue(sinkPair.getValue() - 1);
            }
        }
    }
    int globalShadowOrder = 0;
    while (!pairList.isEmpty()) {
        pairList.sort(Comparator.comparingInt(Pair::getValue));
        Pair<ShadowVariableDescriptor<Solution_>, Integer> pair = pairList.remove(0);
        ShadowVariableDescriptor<Solution_> shadow = pair.getKey();
        if (pair.getValue() != 0) {
            if (pair.getValue() < 0) {
                throw new IllegalStateException("Impossible state because the shadowVariable ("
                        + shadow.getSimpleEntityAndVariableName()
                        + ") can not be used more as a sink than it has sources.");
            }
            throw new IllegalStateException("There is a cyclic shadow variable path"
                    + " that involves the shadowVariable (" + shadow.getSimpleEntityAndVariableName()
                    + ") because it must be later than its sources (" + shadow.getSourceVariableDescriptorList()
                    + ") and also earlier than its sinks (" + shadow.getSinkVariableDescriptorList() + ").");
        }
        for (ShadowVariableDescriptor<Solution_> sink : shadow.getSinkVariableDescriptorList()) {
            Pair<ShadowVariableDescriptor<Solution_>, Integer> sinkPair = shadowToPairMap.get(sink);
            sinkPair.setValue(sinkPair.getValue() - 1);
        }
        shadow.setGlobalShadowOrder(globalShadowOrder);
        globalShadowOrder++;
    }
}

From source file:therian.operator.copy.MapCopier.java

@Override
public boolean perform(TherianContext context, Copy<? extends Map, ? extends Map> copy) {
    final Map<TypeVariable<?>, Type> sourceArgs = TypeUtils.getTypeArguments(copy.getSourceType().getType(),
            Map.class);
    final Map<TypeVariable<?>, Type> targetArgs = TypeUtils.getTypeArguments(copy.getTargetType().getType(),
            Map.class);

    final Type sourceKeyType = TypeUtils.unrollVariables(sourceArgs, KEY);
    final Type sourceValueType = TypeUtils.unrollVariables(sourceArgs, VALUE);
    final Type targetKeyType = TypeUtils.unrollVariables(targetArgs, KEY);
    final Type targetValueType = TypeUtils.unrollVariables(targetArgs, VALUE);

    final Type targetEntryType;
    if (targetKeyType == null || targetValueType == null) {
        targetEntryType = Map.Entry.class;
    } else {// w  ww  .  ja v  a 2s  .co m
        targetEntryType = TypeUtils.parameterize(Map.Entry.class, targetKeyType, targetValueType);
    }

    final Map<?, ?> sourceMap = copy.getSourcePosition().getValue();
    for (Map.Entry<?, ?> e : sourceMap.entrySet()) {
        final Position.ReadWrite<?> targetKey = Positions.readWrite(targetKeyType);
        final Position.ReadWrite<?> targetValue = Positions.readWrite(targetValueType);

        if (!context.evalSuccess(Convert.to(targetKey, Positions.readOnly(sourceKeyType, e.getKey())))) {
            return false;
        }
        if (!context.evalSuccess(Convert.to(targetValue, Positions.readOnly(sourceValueType, e.getValue())))) {
            return false;
        }

        final MutablePair<?, ?> newEntry = MutablePair.of(targetKey.getValue(), targetValue.getValue());
        if (!context.evalSuccess(
                Add.to(copy.getTargetPosition(), Positions.<Map.Entry>readOnly(targetEntryType, newEntry)))) {
            return false;
        }
    }
    return true;
}