Example usage for com.google.common.collect Maps difference

List of usage examples for com.google.common.collect Maps difference

Introduction

In this page you can find the example usage for com.google.common.collect Maps difference.

Prototype

@Beta
public static <K, V> MapDifference<K, V> difference(Map<? extends K, ? extends V> left,
        Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence) 

Source Link

Document

Computes the difference between two maps.

Usage

From source file:cpw.mods.fml.client.modloader.ModLoaderClientHelper.java

public static void handleFinishLoadingFor(ModLoaderModContainer mc, Minecraft game) {
    FMLLog.log(mc.getModId(), Level.FINE, "Handling post startup activities for ModLoader mod %s",
            mc.getModId());/*  w w w .j av a2  s . com*/
    BaseMod mod = (BaseMod) mc.getMod();

    Map<Class<? extends Entity>, Render> renderers = Maps.newHashMap(RenderManager.field_78727_a.field_78729_o);

    try {
        FMLLog.log(mc.getModId(), Level.FINEST, "Requesting renderers from basemod %s", mc.getModId());
        mod.addRenderer(renderers);
        FMLLog.log(mc.getModId(), Level.FINEST, "Received %d renderers from basemod %s", renderers.size(),
                mc.getModId());
    } catch (Exception e) {
        FMLLog.log(mc.getModId(), Level.SEVERE, e,
                "A severe problem was detected with the mod %s during the addRenderer call. Continuing, but expect odd results",
                mc.getModId());
    }

    MapDifference<Class<? extends Entity>, Render> difference = Maps
            .difference(RenderManager.field_78727_a.field_78729_o, renderers, Equivalence.identity());

    for (Entry<Class<? extends Entity>, Render> e : difference.entriesOnlyOnLeft().entrySet()) {
        FMLLog.log(mc.getModId(), Level.WARNING,
                "The mod %s attempted to remove an entity renderer %s from the entity map. This will be ignored.",
                mc.getModId(), e.getKey().getName());
    }

    for (Entry<Class<? extends Entity>, Render> e : difference.entriesOnlyOnRight().entrySet()) {
        FMLLog.log(mc.getModId(), Level.FINEST, "Registering ModLoader entity renderer %s as instance of %s",
                e.getKey().getName(), e.getValue().getClass().getName());
        RenderingRegistry.registerEntityRenderingHandler(e.getKey(), e.getValue());
    }

    for (Entry<Class<? extends Entity>, ValueDifference<Render>> e : difference.entriesDiffering().entrySet()) {
        FMLLog.log(mc.getModId(), Level.FINEST,
                "Registering ModLoader entity rendering override for %s as instance of %s",
                e.getKey().getName(), e.getValue().rightValue().getClass().getName());
        RenderingRegistry.registerEntityRenderingHandler(e.getKey(), e.getValue().rightValue());
    }

    try {
        mod.registerAnimation(game);
    } catch (Exception e) {
        FMLLog.log(mc.getModId(), Level.SEVERE, e,
                "A severe problem was detected with the mod %s during the registerAnimation call. Continuing, but expect odd results",
                mc.getModId());
    }
}

From source file:org.obm.push.mail.EmailChangesComputerImpl.java

@Override
public EmailChanges computeChanges(Iterable<Email> before, Iterable<Email> actual) {
    MapDifference<Long, Email> difference = Maps.difference(iterableToMap(before), iterableToMap(actual),
            new EmailEquivalence());

    return EmailChanges.builder().deletions(Sets.newHashSet(difference.entriesOnlyOnLeft().values()))
            .changes(Sets.newHashSet(rightValueDifferences(difference.entriesDiffering().values())))
            .additions(Sets.newHashSet(difference.entriesOnlyOnRight().values())).build();
}

From source file:org.openhealthtools.mdht.mdmi.CmdLineApp.java

public static String transform(String srcMap, String srcMdl, String srcMsg, String trgMap, String trgMdl,
        String trgMsg, String cnvElm) {
    File rootDir = new File(System.getProperties().getProperty("user.dir"));
    System.out.println("  rootDir = " + rootDir.getAbsolutePath());
    System.out.println("");

    // initialize the runtime, using the current folder as the roo folder
    Mdmi.INSTANCE.initialize(rootDir);//from  ww w .jav a 2s  .  c om
    Mdmi.INSTANCE.start();

    String retVal = null;
    try {
        // 1. check to make sure the maps and messages exist
        File f = Mdmi.INSTANCE.fileFromRelPath(srcMap);
        if (!f.exists() || !f.isFile()) {
            System.out.println("Source map file '" + srcMap + "' does not exist!");
            usage();
        }

        f = Mdmi.INSTANCE.fileFromRelPath(trgMap);
        if (!f.exists() || !f.isFile()) {
            System.out.println("Target map file '" + trgMap + "' does not exist!");
            usage();
        }

        f = Mdmi.INSTANCE.fileFromRelPath(srcMsg);
        if (!f.exists() || !f.isFile()) {
            System.out.println("Source message file '" + srcMsg + "' does not exist!");
            usage();
        }

        f = Mdmi.INSTANCE.fileFromRelPath(trgMsg);
        if (!f.exists() || !f.isFile()) {
            System.out.println("Target message file '" + trgMsg + "' does not exist!");
            usage();
        }

        // 2. make sure the qualified message names are spelled right
        String[] a = srcMdl.split("\\.");
        if (a == null || a.length != 2) {
            System.out
                    .println("Invalid source model '" + srcMdl + "', must be formatted as MapName.MessageName");
            usage();
        }
        String srcMapName = a[0];
        String srcMsgMdl = a[1];

        a = trgMdl.split("\\.");
        if (a == null || a.length != 2) {
            System.out
                    .println("Invalid target model '" + trgMdl + "', must be formatted as MapName.MessageName");
            usage();
        }
        String trgMapName = a[0];
        String trgMsgMdl = a[1];

        // 3. parse the elements array
        final ArrayList<String> elements = new ArrayList<String>();
        String[] ss = cnvElm.split(";");
        for (String s : ss) {
            if (s != null && s.trim().length() > 0) {
                elements.add(s);
            }
        }

        // 4. load the maps into the runtime.
        Mdmi.INSTANCE.getConfig().putMapInfo(new MdmiConfig.MapInfo(srcMapName, srcMap));
        Mdmi.INSTANCE.getConfig().putMapInfo(new MdmiConfig.MapInfo(trgMapName, trgMap));
        Mdmi.INSTANCE.getResolver().resolveConfig(Mdmi.INSTANCE.getConfig());

        // 5. Construct the parameters to the call based on the values passed in
        MdmiModelRef sMod = new MdmiModelRef(srcMapName, srcMsgMdl);
        MdmiMessage sMsg = new MdmiMessage(Mdmi.INSTANCE.fileFromRelPath(srcMsg));
        MdmiModelRef tMod = new MdmiModelRef(trgMapName, trgMsgMdl);
        MdmiMessage tMsg = new MdmiMessage(Mdmi.INSTANCE.fileFromRelPath(trgMsg));

        Map<String, MdmiBusinessElementReference> left = sMod.getModel().getBusinessElementHashMap();

        Map<String, MdmiBusinessElementReference> right = tMod.getModel().getBusinessElementHashMap();

        Equivalence<MdmiBusinessElementReference> valueEquivalence = new Equivalence<MdmiBusinessElementReference>() {

            @Override
            protected boolean doEquivalent(MdmiBusinessElementReference a, MdmiBusinessElementReference b) {
                return a.getUniqueIdentifier().equals(b.getUniqueIdentifier());
            }

            @Override
            protected int doHash(MdmiBusinessElementReference t) {
                return t.getUniqueIdentifier().hashCode();
            }
        };

        MapDifference<String, MdmiBusinessElementReference> differences = Maps.difference(left, right,
                valueEquivalence);

        Predicate<MdmiBusinessElementReference> predicate = new Predicate<MdmiBusinessElementReference>() {

            @Override
            public boolean apply(MdmiBusinessElementReference input) {

                if (!elements.isEmpty()) {
                    for (String element : elements) {
                        if (input.getName().equalsIgnoreCase(element)) {
                            return true;
                        }

                    }
                    return false;
                }
                return true;

            }
        };
        ;

        ArrayList<MdmiBusinessElementReference> bers = new ArrayList<MdmiBusinessElementReference>();
        bers.addAll(Collections2.filter(differences.entriesInCommon().values(), predicate));

        MdmiTransferInfo ti = new MdmiTransferInfo(sMod, sMsg, tMod, tMsg, bers);
        ti.useDictionary = true;

        // 6. call the runtime
        Mdmi.INSTANCE.executeTransfer(ti);

        // 7. set the return value
        retVal = tMsg.getDataAsString();

        saveResults(retVal);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    Mdmi.INSTANCE.stop();
    return retVal; // return the target message transformed
}

From source file:org.eclipse.mdht.uml.common.util.ModelCompare.java

static public void compare(NamedElement element1, NamedElement element2, CompareResultVisitor compareResults) {

    compareResults.startElement(element1);

    HashMap<String, Element> umlPackage1HashMap = createElementHashMap(element1);

    HashMap<String, Element> umlPackage2HashMap = createElementHashMap(element2);

    MapDifference<String, Element> diff = Maps.difference(umlPackage1HashMap, umlPackage2HashMap,
            ValueEquivalence.ElementEquivalence);

    ArrayList<Element> leftElements = getSortedElements(diff.entriesOnlyOnLeft().values());
    for (Element eee : leftElements) {
        compareResults.addedElement(element1, eee);
        if (eee instanceof Package || eee instanceof org.eclipse.uml2.uml.Class) {
            compare((NamedElement) eee, null, compareResults);
        }/*from   w w  w .  ja v  a  2 s.  com*/

    }

    ArrayList<Element> rightElements = getSortedElements(diff.entriesOnlyOnRight().values());
    for (Element eee : rightElements) {
        compareResults.deletedElement(element1, eee);
    }

    ArrayList<ValueDifference<Element>> differences = getSortedValueDifference(
            diff.entriesDiffering().values());
    for (ValueDifference<Element> valueDifference : differences) {
        compareResults.changedElement(element1, valueDifference.leftValue(), valueDifference.rightValue());
        if ((valueDifference.leftValue() instanceof Class)
                || (valueDifference.leftValue() instanceof Package)) {
            compare((NamedElement) valueDifference.leftValue(), (NamedElement) valueDifference.rightValue(),
                    compareResults);
        }
    }

    compareResults.endElement(element1);

}

From source file:org.opentestsystem.authoring.testauth.service.impl.BlueprintElementServiceImpl.java

private List<BlueprintElement> buildListOfDifferences(final String blueprintId,
        final List<BlueprintElement> coreStandardsElementList,
        final List<BlueprintElement> blueprintElementList, final Map<String, Segment> segmentMap,
        final String[] assessmentGrades) {
    List<BlueprintElement> blueprintElementListToSave = Lists.newArrayList();
    final Map<String, BlueprintElement> blueprintElementMap = Maps.uniqueIndex(blueprintElementList,
            BLUEPRINT_ELEMENT_UNIQUE_KEY_FUNCTION);
    final Map<String, BlueprintElement> coreStandardsElementMap = Maps.uniqueIndex(coreStandardsElementList,
            BLUEPRINT_ELEMENT_UNIQUE_KEY_FUNCTION);

    final Map<String, ValueDifference<BlueprintElement>> entriesDiffering = Maps
            .difference(blueprintElementMap, coreStandardsElementMap, BLUEPRINT_ELEMENT_EQUIVALENCE)
            .entriesDiffering();/*from w w w  .  j  ava  2s .  com*/
    final Map<String, BlueprintElement> entriesOnlyOnLeft = Maps
            .difference(blueprintElementMap, coreStandardsElementMap).entriesOnlyOnLeft();
    final Map<String, BlueprintElement> entriesOnlyOnRight = Maps
            .difference(blueprintElementMap, coreStandardsElementMap).entriesOnlyOnRight();

    blueprintElementListToSave = Lists
            .newArrayList(Iterables.transform(entriesDiffering.values(), BLUEPRINT_ELEMENT_VALUE_DIFF_TRANS));

    // subsequent time around, default active booleans for the additions from core standards only to correspond to the grades already in use
    Iterables.addAll(blueprintElementListToSave,
            Iterables.transform(entriesOnlyOnRight.values(),
                    BLUEPRINT_ELEMENT_INJECT_DEFAULT_VALUES_TRANSFORMER.getInstance(blueprintId,
                            convertExistingBpElementGradesIntoDistinctGradeArray(blueprintElementList,
                                    assessmentGrades),
                            segmentMap)));

    Iterables.addAll(blueprintElementListToSave,
            Iterables.transform(entriesOnlyOnLeft.values(), BLUEPRINT_ELEMENT_REMOVE_TRANSFORMER));
    return blueprintElementListToSave;
}