Skip to content

Instantly share code, notes, and snippets.

@Salivala
Last active May 7, 2019 22:26
Show Gist options
  • Save Salivala/4c6082064d480a6ca89df52a17fcb612 to your computer and use it in GitHub Desktop.
Save Salivala/4c6082064d480a6ca89df52a17fcb612 to your computer and use it in GitHub Desktop.
//uhh here's an example since it's hard to explain
public static <T> void invokeOnChildElements(Element elem, T subject, BiConsumer<Element, T> consumer) {
NodeList nodes = elem.getChildNodes();
for ( int i = 0; i < nodes.getLength(); i++) {
if (nodes.item(i) instanceof Element) {
Element subjectElem = ((Element) nodes.item(i));
consumer.accept(subjectElem, subject);
}
}
}
//and then i can call it with this
invokeOnChildElements(elem, panel, (currElem, currPanel) -> {
currPanel.add(delegates.router.routeToContainer(currElem, xsp, delegates));
});
//or like this
invokeOnChildElements(elem, panel, (currElem, currContainer) -> {
if (currElem.getTagName().endsWith("NORTH"))
addToLayout(currContainer, currElem, xsp, delegates, BorderLayout.NORTH);
else if (currElem.getTagName().endsWith("SOUTH"))
addToLayout(currContainer, currElem, xsp, delegates, BorderLayout.SOUTH);
else if (currElem.getTagName().endsWith("WEST"))
addToLayout(currContainer, currElem, xsp, delegates, BorderLayout.WEST);
else if (currElem.getTagName().endsWith("EAST"))
addToLayout(currContainer, currElem, xsp, delegates, BorderLayout.EAST);
else if (currElem.getTagName().endsWith("CENTER"))
addToLayout(currContainer, currElem, xsp, delegates, BorderLayout.CENTER);
});
/*
It kinda lets me use the same block of code for different contexts
the magic thing is the BiConsumer which is just any function that takes two things and does stuff
there's notation in java 11 i think that makes it so i don't have to literally instantiate and implement something that uses the interface, but i can pass something like a lambda
i'm assuming in kotlin you don't have to fuck around with these diff interfaces, but whenever i generate kotlin from an existing project this is usually where it fails to generate working code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment